Suppose you need to change when an order can ship. You open the method and find this:

final class ShipOrders { /** @param list<Order> $orders */ public function ship(array $orders): void { foreach ($orders as $order) { if ($order->isPaid()) { if ($order->isReady()) { if ($order->hasAddress()) { $order->ship(); } } } } } } 

Assume the three questions return bool and ship() returns void. The types are consistent. Every call exists. PHPStan accepts this code, including at its highest built-in rule level.

Now add another requirement: orders on hold must wait.

You have to find the right place inside that staircase, understand which conditions have already run, and preserve what happens when one fails. None of this is particularly difficult. You will probably get it right.

You will also do the same small piece of reasoning the next time someone changes this method.

That repeated effort is worth paying attention to.

Keep the green check

PHPStan catches problems I want caught before review: invalid calls, incompatible types, unsafe nullable access, unreachable code, and more. Its built-in rule levels describe the checks being applied.

A green result tells us those checks passed. We still have to read the program.

In the shipping method, the useful work sits inside four levels of control flow: the loop and three conditions. We can express the same behavior with a guard:

final class ShipOrders { /** @param list<Order> $orders */ public function ship(array $orders): void { foreach ($orders as $order) { if (! $order->isPaid() || ! $order->isReady() || ! $order->hasAddress()) { continue; } $order->ship(); } } } 

The shipping call now follows one rejection point. Another eligibility condition can join the existing guard.

The details matter here. PHP evaluates || from left to right and stops when an operand is true. An unpaid order still skips isReady() and hasAddress(). An order that is not ready still skips hasAddress(). An exception still leaves the method at the same operation.

I checked the two versions against all eight combinations of the three boolean results, including exceptions thrown by each operation. The call sequences and outcomes match.

Both versions also pass PHPStan at --level=max.

The change earns its place because it makes the method easier to follow while preserving its behavior. A lower nesting count is useful evidence for that decision.

A smaller method can still make a worse program

There is an easy way to satisfy a method-length limit: move some lines into another class.

Then move a few more lines into another class.

Eventually, each file looks respectable on its own, and reading one operation requires opening six of them.

Consider this:

final class OrderSaver { public function __construct( private OrderRepository $orders, ) {} public function save(Order $order): void { $this->orders->save($order); } } 

With a typed repository contract, this passes PHPStan too.

But what does OrderSaver let us understand that we could not understand from OrderRepository::save()?

Every operation added to the repository may now need another forwarding method. To inspect what saving does, we follow an extra call. The class introduces another name without explaining another decision.

At this call site:

$saver->save($order); 

using the repository directly may be enough:

$orders->save($order); 

Before deleting the wrapper, inspect its purpose. It may preserve a published boundary. It may adapt another system's contract. It may be where a transaction or authorization policy belongs.

Those are concrete reasons to keep an object. They deserve more weight than a rule saying a class is too small.

What I want from a refactor is a better answer to the next maintenance question. Where does this decision live? What must change together? Which behavior will this edit affect?

Counting files cannot answer those questions.

A finding has to earn the interruption

This is where automated design checks become interesting—and where they can become irritating.

Detecting four levels of nesting is straightforward. Proving that a particular extraction improves the program is a much harder claim.

A useful tool needs to distinguish the two.

For the shipping example, I am comfortable with a check saying:

This method has nesting depth 4. Your selected limit is 3.

That gives the reviewer a location and a reason to look. We can inspect the guard version and decide whether it improves the method.

I am much less interested in a tool that insists every long method needs another object. That advice can produce the forwarding classes we just looked at.

A design rule should survive a reasonable counterexample. When the suggested fix makes the calling code harder to follow, we should question the rule.

We should also be willing to remove it.

What I am building with Soda

I am working on Soda for this part of PHP code review: recurring observations about how code is organized.

The nesting check above is one example. With a configured limit of three, Soda reports the nested shipping method at depth four. The guard version stays below that limit.

You can try that single policy without adopting a large collection of opinions.

Install the package:

composer require --dev cosmira/soda 

Create a soda.php file:

<?php use Cosmira\Soda\Config\Soda; use Cosmira\Soda\Rules\Complexity\MaxControlNesting; return Soda::configure() ->withPaths(['src/']) ->with([ new MaxControlNesting(3), ]); 

Use your application's source directory in withPaths(), then run:

vendor/bin/soda 

This configuration selects only the nesting rule. You can inspect its findings before choosing other checks.

Three is a policy you have selected. There is no universal law under which a fourth level turns working PHP into bad software.

Review the refactor too

A passing build is valuable. So is a reviewer who notices that understanding a small change now requires following a chain of unnecessary calls.

Before accepting a cleanup, read the original calling code again. Try to answer the same maintenance question using the new version.

Did the decision become easier to find? Did we preserve evaluation order and effects? Did the new name explain something, or give us another file to open?

Those are the conversations I want Soda to help start.

If you try it, bring a finding you disagree with. Include the code and explain what would get worse if you followed the advice. That is a useful way to improve a tool that makes judgments about other people's code.