Skip to content

Undefined static property ignoring property_exists() calls #2861

@gregor-tb

Description

@gregor-tb

Bug report

In Strict mode it is not allowed to overwrite properties already defined in Traits.
For this reason the Trait method checks if the property exists before accessing it.

Stan reports here

Access to an undefined static property
Foo::$default.

https://phpstan.org/r/63d8e0bc-78b4-41ab-b077-5244e2e218b0

Code snippet that reproduces the problem


trait EnumTrait {
    protected $value;

    final public function __construct($value) {
        $this->value = $value;
    }

    public static function getDefault() {
        if (property_exists(static::class, 'default') && null !== static::$default) {
            $obj = static::$default;
            return new static($obj);
        }
        return null;
    }
}

Classes we don't want to have a default are reporting

class Foo {
    use EnumTrait;
    public const BLA = 'bla';
}

This is ok

class Foo {
    use EnumTrait;
    public static $default = 'bla';
    public const BLA = 'bla';
}

Expected output

Should not report undefined property when property_exists() was called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions