Skip to content

Using #[Required] on traits won't be recognized correctly since v2.0.11 #468

@mvhirsch

Description

@mvhirsch

Hi,

PHPStan rly helps me fixing bugs in my code. I'm always eager to update it! However, the latest phpstan-symfony extensions seems to introduce a bug when using #[Required] on traits:

Class HelloWorld has an uninitialized readonly property $query. Assign it in the constructor.

Reproducible

https://phpstan.org/r/f7e42743-2ab6-44cc-ad55-ffae9b30dcc0

<?php

use Symfony\Contracts\Service\Attribute\Required;

interface MyQueryInterface {
    public function execute(): array;
}

trait GenreFetcherTrait
{
    private readonly MyQueryInterface $query; 

    protected function dosomething(): array
    {
        return $this->query->execute();
    }

    #[Required]
    public function setQuery(MyQueryInterface $query): void {
        $this->query = $query;
    }
}


class HelloWorld
{
    use GenreFetcherTrait;
}

Expected results

No issues. Used to work with v2.0.10.

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