Skip to content

Aliasing a constructor causes false positives #9577

@TravisCarden

Description

@TravisCarden

Bug report

I have an unusual design arising from some tangly requirements: I have a bunch of exception classes that need to reuse a constructor, but they can't extend a common class because they each need to extend a core PHP exception. Hence, I put the shared constructor in a trait. However, one of the exceptions needs to override the constructor to add an argument. If the common constructor were defined on a class, I would just call parent::__construct(). But of course the trait isn't the parent, so I found a different way of calling it: by aliasing the method. The result works perfectly--PHP interprets it as expected, and my tests pass--but it confuses PHPStan. I'll probably just ignore the errors and try to refactor the code to something less weird later, but I report it here in case it seems worth addressing.

I was only able to reproduce three of four errors in the playground, so I include them all here. The actual code can be seen at https://github.com/php-tuf/composer-stager/tree/91491a4852055a2cfa5cbbf098faafc61a639858/src/API/Exception.

------ --------------------------------------------------------------------------------------
 Line   src/test.php
------ --------------------------------------------------------------------------------------
 10     Static call to __construct() is only allowed on a parent class in the constructor.
 19     Method SpecializedException::__traitConstruct() has no return type specified.
 19     Promoted properties can be in constructor only.
 25     SpecializedException::__construct() does not call parent constructor from Exception.
------ --------------------------------------------------------------------------------------

Code snippet that reproduces the problem

https://phpstan.org/r/2b396aba-efa9-40fd-9682-18bb6af7d68c

Expected output

None. 🙂️

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