phpinspectionsea icon indicating copy to clipboard operation
phpinspectionsea copied to clipboard

False-positive: Initializing private property of trait in constructor

Open TAS-EW-02 opened this issue 2 years ago • 0 comments

Subject Details
Plugin Php Inspections (EA Extended) for PhpStorm
Language level PHP >=7.4 (personally tested on 7.4, 8.0 and 8.1)

Current behaviour

When using a trait with a uninitialized private property, the initialization in the constructor causes

"'Classname' and 'Traitname' define the same property ($'propertyname')"

Check example code below.

Expected behaviour

This is a false-positive: No warning.

Environment details

Following code is valid PHP code. It runs without further notices in my PHP environment (e.g. 8.0.21). Even if this case is not explicitly mentioned in the PHP docs, this is accepted code. I tried different variations to find out what PHP itself allows me to do or not to do. So I would like to categorize this as false-positive.

Please confirm or disprove this case:

trait HasProperty
{
    private string $property;
    // Getter, Validators, ...
}

class HasTrait
{
    use HasProperty;

    public function __construct(string $property, ...)
    {
        $this->property = $property; // <- here message is thrown (to be exact: on "property" after $this)
        // ...
    }
}

Thank you :)

TAS-EW-02 avatar Aug 05 '22 13:08 TAS-EW-02