Have you ever written Laravel code that works… but feels messy, hard to test, or hard to maintain?
That’s where Object Calisthenics comes in — a set of 9 simple rules that help you write cleaner, more focused, and maintainable object-oriented code.
What is Object Calisthenics?
Created by Jeff Bay, Object Calisthenics is a list of 9 rules to train good object-oriented habits.
Think of it as a workout for your code — maybe you won't follow all the rules every time, but practicing helps you improve.
The 9 Rules with PHP (Laravel) Examples.
Only One Level of Indentation per Method
Avoid deeply nested if
statements. Extract logic into methods.
if (! $user->isActive()) return;
if (! $user->hasRole('admin')) return;
$this->grantAccess();
Wrap All Primitives and Strings
Avoid passing raw values. Create Value Objects.
public function updateStatus(Status $status)
Don’t Use the ELSE Keyword
Make your code easier to read by removing else
.
if ($user->isActive()) return 'Active';
return 'Inactive';
One Dot per Line
Avoid calling multiple methods in a chain — it creates tight coupling.
// Instead of
return $user->profile->address->city;
// Do
return $user->getCity();
First Class Collections
Don’t use plain arrays — wrap them in a class.
class Order {
public OrderItems $items;
}
Don’t Abbreviate
Use clear and complete names.
// Bad
$usr = new UsrSvc();
// Good
$userService = new UserService();
Keep All Entities Small
If a class is too long, it’s doing too much. Split it into smaller classes.
No Getters/Setters
Avoid exposing internals. Use intention-revealing methods.
// bad
$user->setName('John');
// good
$user->renameTo('John');
No More Than Two Instance Variables per Class
Limit how much a class depends on.
If your class needs 4+ services, split the responsibilities.
Helpful Tools
Pint – for auto formatting.
PHP CS Fixer – enforce style rules.
Conclusion
Object Calisthenics isn’t a strict rule it’s a discipline.
By using these techniques in your PHP/Laravel projects, your code will become more:
- Readable
- Testable
- Cohesive
- Maintainable
Do you already follow any of these rules? Which one helped you the most?
Drop a comment below!
Top comments (2)
As mentioned the rules are guides but I think they need more explanation.
There are some rules that are very sensible like thinking about the name of the methods and return as soon as possible. (rules 1, 3, 7, 9)
Then there is the rule to wrap arrays in a class. That is not always feasible, but if an array has a defined structure a class is a good way to validate that structure.
Keep classes small and limit dependencies. While the idea is good, any arbitrary limit is not going to work in reality.
And then there are the problematic rules:
The reason why those two rules are problematic, besides the mentioned reasons, is that they create too much abstraction for the task the code performs.
or just use: plugins.jetbrains.com/plugin/7973-...