Skip to content

Unsafe usage of new static() when constructor is defined in a parent class #6007

@rhertogh

Description

@rhertogh

Bug report

When a class implements an interface which defines a constructor but that constructor is defined in the class' parent, PHPStan reports a Unsafe usage of new static() error.

Code snippet that reproduces the problem

https://phpstan.org/r/77e24aff-9e5b-487e-97b6-d5fa1d95864a

<?php
interface FooInterface
{
    public function __construct();
}

class VendorFoo {
    public function __construct() {}
}

class Foo extends VendorFoo implements FooInterface
{
    static function build():static {
        return new static();
    }
}

When the child class also defines the constructor no error is reported:
https://phpstan.org/r/e54ff651-0397-4910-a5fb-534705ccddd5

Expected output

No error should be reported.

Did PHPStan help you today? Did it make you happy in any way?

Yes, it did find valid bugs 👍😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions