Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Flaky errors when narrowing generic abstract class to values of constant array #14501

@derrabus

Description

@derrabus

Bug report

First of all, I'm sorry I cannot give you a clean reproducer for this bug. It started appearing with PHPStan 2.1.48 and it can still be observed with 2.1.50. PHPStan 2.1.47 was fine.

The problem is not only about false-positive errors, but about them not being deterministic. I can observe the errors in one run, then clear the PHPStan cache, run PHPStan again and the errors are either gone or occur again on different classes.

Our project is a Symfony application that has a couple of authorization voters that extend Symfony's helper class:

https://github.com/symfony/symfony/blob/21cac7f5545a840a1f5d7e704ea34dff58c14ddc/src/Symfony/Component/Security/Core/Authorization/Voter/Voter.php#L16-L25

The Voter class has two generic parameters, TAttribute of string and TSubject of mixed. The problematic implementations on our side look like this:

/** @extends Voter<value-of<self::ATTRIBUTES>, SomeInterface> */
final class SomeVoter extends Voter
{
    public const string SOME_ATTRIBUTE = 'SOME_ATTRIBUTE';
    public const string ANOTHER_ATTRIBUTE = 'ANOTHER_ATTRIBUTE';

    private const array ATTRIBUTES = [
        self::SOME_ATTRIBUTE,
        self::ANOTHER_ATTRIBUTE,
    ];

    // …
}

On these classes, PHPStan might report an error like this one:

Type mixed in generic type Symfony\Component\Security\Core\Authorization\Voter\Voter<mixed, MyApp\SomeInterface> in PHPDoc tag @extends is not subtype of template type TAttribute of string of class Symfony\Component\Security\Core\Authorization\Voter\Voter.
🪪  generics.notSubtype

It looks like the expression value-of<self::ATTRIBUTES> is for some reason resolved to mixed which is of course not correct.

I hope I provided you with enough information to track down the issue. I'm losing my mind on this one. 😅

Code snippet that reproduces the problem

No response

Expected output

No errors, my code is fine! 😎

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

⭐⭐⭐⭐⭐

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions