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

Attribute class does not have the parameter target for promoted properties #10385

Closed
darenas31415 opened this issue Jan 5, 2024 · 8 comments
Closed

Comments

@darenas31415
Copy link

darenas31415 commented Jan 5, 2024

Bug report

Version affected: 1.10.51 with bleeding edge

Given an attribute with property target
When the attribute is applied to a promoted property
Then PHPStan reports the attribute does not have parameter target

It seems PHPStan doesn't recognise the attribute is applied to a promoted property but instead it thinks it's function parameter.

Code snippet that reproduces the problem

https://phpstan.org/r/b407f06b-e3c6-419a-9d32-aca23a616fef

Expected output

PHPStan should not report any issues.

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

No response

@ondrejmirtes
Copy link
Member

Promoted property is a parameter and property at the same time. This is expected behaviour.

@darenas31415
Copy link
Author

darenas31415 commented Jan 5, 2024

I've opened the issue because I've got the error while using Symfony's constraints alongside promoted properties.

For example, the Uuid constraint only applies to property or method as docs explain.

With this definition:

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Uuid extends Constraint
{
}

The following code works but PHPStan now reports an error:

class Author
{
    public function __construct(
        #[Assert\Uuid]
        private string $id
    ) {}
}

I could open an issue in Symfony repo but I doubt they will be willing to add Attribute::TARGET_PARAMETER to the list of allowed targets as it would allow the attribute to be applied to function parameters.

Do you think the issue is on Symfony side?

@ondrejmirtes
Copy link
Member

The behaviour is going to change back in 1.10.54. The conclusion in PHP internals is that this should not be reported: https://externals.io/message/111942#112021

@darenas31415
Copy link
Author

Thanks @ondrejmirtes

@RobertMe
Copy link

RobertMe commented Jan 6, 2024

@ondrejmirtes does this mean you reverted the behavior introduced following #10298 ? And doesn't that mean that the issue I reported in that ticket is a valid issue? (for which there is the closed / unmerged PR phpstan/phpstan-src#2825)

@ondrejmirtes
Copy link
Member

@RobertMe After seeing your PR with a fresh set of eyes and the newly aquired knowledge I decided to merge it: phpstan/phpstan-src@2e08c9f

Thank you.

@RobertMe
Copy link

RobertMe commented Jan 6, 2024

@ondrejmirtes thank you 👍🏻

Copy link

github-actions bot commented Feb 7, 2024

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 Feb 7, 2024
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

3 participants