Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
luxemate opened this issue Oct 26, 2023 · 5 comments
Closed

Comments

@luxemate
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor Author

luxemate commented Oct 30, 2023

@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
Copy link
Member

@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
Copy link
Contributor Author

luxemate commented Oct 30, 2023

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

Copy link

github-actions bot commented Dec 1, 2023

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants