Skip to content

[symfony] Skip enums, closures and exception classes in "NoNullableServiceInConstructorRule" - #333

Merged
TomasVotruba merged 1 commit into
symplify:mainfrom
bmdevel:skip-enum-closure-exception-in-nullable-service-rule
Oct 2, 2026
Merged

TomasVotruba merged 1 commit into
symplify:mainfrom
bmdevel:skip-enum-closure-exception-in-nullable-service-rule

Conversation

@bmdevel

@bmdevel bmdevel commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

NoNullableServiceInConstructorRule reports some nullable constructor parameters that are values, not container services. This PR skips three more of them, in the same way the rule already skips Throwable and DateTimeInterface parameters.

1. Enum parameters

An enum is never a container service, so ?SomeEnum $status is an optional value:

final class Booking
{
    public function __construct(
        private readonly ?PaymentType $paymentType, // was reported
    ) {
    }
}

2. Closure parameters

An optional callback such as ?Closure $handler = null is a value too.

3. Exception classes

An exception is built with new at the throw site, never by the container. Its constructor carries error context, for example a nullable HTTP response:

final class ResponseErrorException extends RuntimeException
{
    public function __construct(
        private readonly ?ResponseInterface $response = null, // was reported
    ) {
        parent::__construct('...');
    }
}

The rule now skips a class that is itself a Throwable, in the same way it skips the \Exception\ namespace.

Each case has a fixture in NoNullableServiceInConstructorRuleTest. All three were reported before the change. The full suite, ECS, PHPStan and Rector pass.

…rviceInConstructorRule"

A nullable enum or Closure parameter is a value, not a container service,
so it is left alone like DateTimeInterface and Throwable already are.

An exception class is built with "new" at the throw site, so its
constructor carries error context (for example a nullable response),
not services; it is skipped whole.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TomasVotruba

Copy link
Copy Markdown
Member

Legit, thank you

@TomasVotruba
TomasVotruba enabled auto-merge (squash) October 2, 2026 09:01
@TomasVotruba
TomasVotruba merged commit 921811a into symplify:main Oct 2, 2026
8 checks passed
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.

2 participants