phpstan / phpstan-symfony

Symfony extension for PHPStan
MIT License
698 stars 89 forks source link

Method MyVoter::supports() has parameter $attribute with no type specified #367

Closed luxemate closed 10 months ago

luxemate commented 10 months ago

After update from 1.1.8 to 1.3.4 all custom voters started producing an error, although Symfony's abstract Voter has type annotation for $attribute:

Method MyVoter::supports() has parameter $attribute with no type specified.

Consider the following simple Voter implementation:

use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class MyVoter extends Voter
{
    protected function supports($attribute, mixed $subject): bool
    {
        return true;
    }

    protected function voteOnAttribute($attribute, mixed $subject, TokenInterface $token): bool
    {
        return true;
    }
}

The funny thing is it only gets upset by the supports method, but not the voteOnAttribute method.

image

ondrejmirtes commented 10 months ago

Hi, this is not a bug. Your method should be declared such as:

protected function supports(string $attribute, mixed $subject): bool

Same as the parent. If you widen the type by omitting the parameter type on purpose, you should say in the PHPDoc what the type is supposed to be, such as @param mixed $attribute.

luxemate commented 10 months ago

@ondrejmirtes Symfony 4.4 doesn't have native types set, thus I also can't use native type in supports (because of the signature mismatch). But what symfony does have is the @param string $attribute set, which is ignored in the supports method, but is not ignored in the voteOnAttribute method (probably because of asserts in the stub).

But maybe I misinterpret the situation and with treatPhpDocTypesAsCertain: false it's required to add @param string $attribute to the supports method in a custom Voter. Though it seems a little bit redundant, as annotation was already added by the third-party vendor.

ondrejmirtes commented 10 months ago

@luxemate You've left out the information about Symfony 4.4 in your issue. I'd accept a PR that adds @param string $attribute into the stub.

luxemate commented 10 months ago

@ondrejmirtes I've created a new PR and added only @param string $attribute as you've suggested.

github-actions[bot] commented 9 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.