Skip to content

[symfony] Add NoNullableServiceInConstructorRule - #288

Merged
TomasVotruba merged 1 commit into
mainfrom
tv/no-nullable-service-in-constructor
Sep 21, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
tv/no-nullable-service-in-constructor

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds NoNullableServiceInConstructorRule.

A constructor service dependency must not be nullable. A service is always provided by the container, so ?SomeService $service or SomeService|null $service only hides that the dependency is really required - and forces every use site into a null check that can never hit.

Left alone, as generic exceptions:

  • abstract classes - a base may leave a dependency optional for a child to fill,
  • values that are not services: nullable scalars and arrays, exceptions (?Throwable $previous is nullable by PHP convention) and date value objects (DateTimeInterface),
  • a nullable param whose type a non-nullable sibling param already provides (a genuine optional extra),
  • anonymous classes.

Covered by RuleTestCase with a report fixture (?Service + Service|null) and skip fixtures for each exception above.

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

@TomasVotruba
TomasVotruba force-pushed the tv/no-nullable-service-in-constructor branch from 9eee395 to 42204a6 Compare September 21, 2026 08:34
@TomasVotruba
TomasVotruba merged commit cafd73e into main Sep 21, 2026
8 checks passed
@TomasVotruba
TomasVotruba deleted the tv/no-nullable-service-in-constructor branch September 21, 2026 08:35
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