Skip to content

@phpstan-sealed leads to wrong errors being reported when used on a non-abstract class #13734

@stof

Description

@stof

Bug report

Sealing a non-abstract class removes some benefits of the sealing (we cannot do exhaustive checks on child classes to cover all cases), but it is still a valid use case: allowing inheritance only for my own classes and not for third-party classes.
However, phpstan does not handle this properly. When migrating the scssphp/scssphp codebase from the jiripudil/phpstan-sealed-classes extension to the @phpstan-sealed annotation, some errors were reported for such usage: https://github.com/scssphp/scssphp/actions/runs/18893747531/job/53926840383?pr=815#step:5:10

Code snippet that reproduces the problem

https://phpstan.org/r/51419802-fa2a-459d-8079-744ec3452857

Expected output

Type assertions should pass.
This snippet is a port of https://github.com/jiripudil/phpstan-sealed-classes/blob/main/tests/data/allowed-subtypes-non-abstract-class.php from the testsuite of jiripudil/phpstan-sealed-classes, with the only change being migrating from their Sealed attribute to the @phpstan-sealed annotation.

See https://github.com/jiripudil/phpstan-sealed-classes/blob/2a577dac4afae03dd9271b7e2978cc5aa50c0df9/src/SealedClassAllowedSubTypesClassReflectionExtension.php#L49-L51 for the logic handling it in jiripudil/phpstan-sealed-classes

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

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions