rector-laravel icon indicating copy to clipboard operation
rector-laravel copied to clipboard

Prefer Callback over String Key for Static Analysis

Open simbig opened this issue 4 months ago • 2 comments

Feature Request

Is there a way to achieve the following transformation in Rector? Even if the type of $foo cannot be fully inferred, encouraging this pattern would provide significant benefits.

In general, methods that enable static analysis and error detection SHOULD be preferred over those that rely on stringly-typed or otherwise unsafe approaches.

A concrete example:

Diff

-$foos->sortBy('foo_prperty') // Typo not caught
+$foos->sortBy(static fn (Foo $foo): string => $foo->foo_property) // Typo caught by static analysis

The callback version makes use of language-level guarantees (like auto-completion and type checking), which help catch typos and invalid access early—something that's not possible when using string keys.

Would it be possible for Rector to:

Detect stringly-typed property access in methods like sortBy()

Suggest or apply a refactor to the safer callback form?

This would be especially useful for Laravel collections, but the idea could be generalized to other similar patterns.

Thanks for considering this!

simbig avatar Jul 31 '25 09:07 simbig