Skip to content

[symfony] Add NoServiceJugglingRule - #287

Merged
TomasVotruba merged 1 commit into
mainfrom
tv/no-service-juggling-rule
Sep 21, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv/no-service-juggling-rule

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds NoServiceJugglingRule.

A service injected in __construct() or an autowire*() method must not be handed over to another method call of the same class. Passing an own dependency around is service juggling - the service travels through a parameter list instead of being injected where it is used. When the called method belongs to the same class, it has a constructor of its own and can take the service directly.

The rule is deliberately conservative:

  • only injected services (constructor, autowire*(), or #[Required]) count,
  • only calls to a method the class itself owns are reported - inherited-via-$this and trait methods are shared by every child, so the service is a genuine argument there,
  • an argument position filled differently by another call is treated as a real parameter, not a fixed dependency.

Covered by RuleTestCase with report fixtures (promoted + autowired) and skip fixtures (value arg, own-parameter, trait method, varying argument, inherited method).

Note: the Rector job may show one unrelated finding on src/Rules/Enum/SymfonyAttribute.php that comes from main; it is fixed in #286.

Green locally: ECS, PHPStan, Rector, composer-dependency-analyser, tests.

@TomasVotruba
TomasVotruba merged commit 8363e94 into main Sep 21, 2026
8 checks passed
@TomasVotruba
TomasVotruba deleted the tv/no-service-juggling-rule branch September 21, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant