Skip to content

[symfony] Add PreferClassServiceReferenceRule for config closures - #289

Merged
TomasVotruba merged 1 commit into
mainfrom
tv/prefer-class-service-reference
Sep 21, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv/prefer-class-service-reference

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds PreferClassServiceReferenceRule (Symfony PHP config closures), plus its two collectors.

When a config closure points a string service id at a class:

$services->alias('some.helper', SomeHelper::class);

then a service('some.helper') reference names the very same service the class does. The rule asks for service(SomeHelper::class) instead, so the reference carries the type. Once every reference names the class, the string alias nothing else asks for can be dropped.

Implementation:

  • ClassTargetServiceAliasCollector collects alias('id', X::class) pairs,
  • ServiceStringReferenceCollector collects service('id') string references,
  • the rule cross-references the two over a CollectedDataNode and reports each string reference an alias already covers with a class.

A service(X::class) reference and a service('id') no alias covers are left alone.

Registered in config/symfony-config-rules.neon (rule + both collectors), covered by a RuleTestCase with config-closure fixtures.

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

@TomasVotruba
TomasVotruba force-pushed the tv/prefer-class-service-reference branch from c35739b to 5043412 Compare September 21, 2026 08:37
@TomasVotruba
TomasVotruba enabled auto-merge (squash) September 21, 2026 08:37
@TomasVotruba
TomasVotruba merged commit 5597908 into main Sep 21, 2026
8 checks passed
@TomasVotruba
TomasVotruba deleted the tv/prefer-class-service-reference branch September 21, 2026 08:38
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