Skip to content

Required attribute should remove readonly properties errors #360

Closed
@SVillette

Description

@SVillette

Description

Currently, PHPStan reports an error when a readonly property is not initialized and another error when not initialized in a constructor.

  • Class App\Foo has an uninitialized readonly property $validator. Assign it in the constructor.
  • Readonly property App\Foo::$validator is assigned outside of the constructor.

But with the attribute #[Required] the DIC automatically call this method to initialize the property.

I think it could be a nice feature to remove these errors in that case.

Example

trait ValidationAwareCommandTrait
{
    private readonly ValidatorInterface $validator;

    #[Required]
    public function setValidator(ValidatorInterface $validator): void
    {
        $this->validator = $validator;
    }
}

Playground

https://phpstan.org/r/2ba9746d-bafd-4433-bf13-bcf3b15fc940

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions