Skip to content

PHPStan Rules 14.14

Choose a tag to compare

@TomasVotruba TomasVotruba released this 21 Sep 08:44
· 82 commits to main since this release
5597908

A batch of 9 new rules extracted from a real Symfony/Doctrine app, plus a CI bugfix.

Full set install:

includes:
    - vendor/symplify/phpstan-rules/config/symfony-rules.neon
    - vendor/symplify/phpstan-rules/config/symfony-config-rules.neon
    - vendor/symplify/phpstan-rules/config/static-rules.neon
    - vendor/symplify/phpstan-rules/config/code-complexity-rules.neon

🎉 New rules

CommandMustHaveAsCommandAttributeRule

A class extending Command must declare #[AsCommand].

// ❌
final class ReportCommand extends Command {}

// ✅
#[AsCommand('app:report')]
final class ReportCommand extends Command {}

ConstraintMustHaveAttributeRule

A class extending Constraint must declare #[\Attribute], so it works as a property attribute.

// ❌
final class UniqueEmail extends Constraint {}

// ✅
#[\Attribute]
final class UniqueEmail extends Constraint {}

PreferInterfaceInConstructorRule

A constructor dependency typed as a concrete Symfony/Doctrine class that has a same-named *Interface should use the interface (keeps decoration open).

// ❌
public function __construct(private Router $router) {}

// ✅
public function __construct(private RouterInterface $router) {}

NoNullableServiceInConstructorRule

A constructor service must not be nullable - the container always provides it. Nullable stays allowed on abstract classes and on non-services (scalars, arrays, ?Throwable $previous, date value objects).

// ❌
public function __construct(private readonly ?SomeService $someService) {}

// ✅
public function __construct(private readonly SomeService $someService) {}

NoServiceJugglingRule

A service injected in __construct() / an autowire*() method must not be passed to another method of the same class - that method has its own constructor and can inject it directly.

// ❌
public function run(): void
{
    $this->handle($this->userHelper);
}

// ✅ inject UserHelper in the constructor, then
public function run(): void
{
    $this->handle();
}

PreferClassServiceReferenceRule

In a Symfony PHP config closure, when an alias points a string id at a class, reference the service by that class instead of the string.

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

// ❌
->args([service('some.helper')]);

// ✅
->args([service(SomeHelper::class)]);

NoDuplicateNonRepeatableAttributeRule

An attribute repeated on the same class/method/property must declare \Attribute::IS_REPEATABLE.

// ❌
#[SomeAttribute]
#[SomeAttribute]
private string $name;

NoPropertyToPropertyAssignRule

$this->a = $this->b keeps the same service under two names - use the original directly.

// ❌
$this->repository = $this->someRepository;

RequireArrayShapeReturnRule

A method returning a packed keyed array of 2-3 named values should declare its shape.

// ❌
public function run(): array
{
    return ['name' => $name, 'age' => $age];
}

// ✅
/** @return array{name: string, age: int} */
public function run(): array
{
    return ['name' => $name, 'age' => $age];
}

🐛 Bugfixes

  • Fixed the Rector skip path so StringClassNameToClassConstantRector no longer rewrites the intentional class-name strings in src/Rules/Enum (this had turned the Rector CI job red). #286
  • CommandMustHaveAsCommandAttributeRule and ConstraintMustHaveAttributeRule now bail out when the framework class (AsCommand, Constraint) is not installed in the analysed project - no false positive without that Symfony component. #286