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

False positive: PHP8 property promotion attribute target #4418

Closed
dbrekelmans opened this issue Jan 24, 2021 · 14 comments · Fixed by phpstan/phpstan-src#528
Closed

False positive: PHP8 property promotion attribute target #4418

dbrekelmans opened this issue Jan 24, 2021 · 14 comments · Fixed by phpstan/phpstan-src#528
Labels
Milestone

Comments

@dbrekelmans
Copy link

dbrekelmans commented Jan 24, 2021

Bug report

When using a PHP8 attribute with TARGET_PROPERTY, you should be able to use it on constructor parameters if you're using constructor property promotion.

Instead, you get the error: Attribute class Foo does not have the parameter target.

Code snippet that reproduces the problem

https://phpstan.org/r/17f00d68-0912-4f7f-84fc-6c69fa3683e3

Expected output

No error

@mergeable
Copy link

mergeable bot commented Jan 24, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@dbrekelmans dbrekelmans changed the title False positive: property promotion attribute target False positive (PHP8): property promotion attribute target Jan 24, 2021
@dbrekelmans dbrekelmans changed the title False positive (PHP8): property promotion attribute target False positive: PHP8 property promotion attribute target Jan 24, 2021
@ondrejmirtes
Copy link
Member

Not true, PHPStan is right. Runtime proof: https://3v4l.org/tBjj2

@dbrekelmans
Copy link
Author

Strange, https://wiki.php.net/rfc/constructor_promotion#attributes implies that it should work. Maybe this was overlooked when implementing attributes.

Thanks either way for looking into this!

@dktapps
Copy link
Contributor

dktapps commented Jan 24, 2021

To my understanding, this is intentionally not allowed because php-src developers couldn't decide whether attributes on promoted properties should apply to the parameter, the property or both. They chose to disallow it to avoid the problem for the time being. https://externals.io/message/111942

@michaelzangerle
Copy link

@ondrejmirtes but it works like this https://3v4l.org/RqJLk. As it's not just a simple parameter but a promoted property it kind of makes sense I guess. WDYT?

@ondrejmirtes
Copy link
Member

From attributes RFC:

validation of attributes is only performed during ReflectionAttribute::newInstance()

@michaelzangerle
Copy link

@ondrejmirtes I think that I am exactly calling that function (as you did in your example) in the loop? I copied your example and only changed the way you are getting the attributes -> from the properties instead of the parameters. What am I missing?

@ondrejmirtes ondrejmirtes reopened this Jan 29, 2021
@ondrejmirtes
Copy link
Member

Alright, didn't notice that, time to rethink this a bit maybe :)

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 18, 2021
@derrabus
Copy link

I ran into this issue today as well. I think, when using constructor property promotion, PHPStan should only check if the annotation is allowed on at least one of the targets "property" and "parameter". If both targets are disallowed, the error is valid.

When using CPP, I cannot specify that the attribute should only be applied to the property. If I wanted to please PHPStan, I would have to specify the property in the traditional way. That would be unfortunate, imho.

@iquito
Copy link

iquito commented Apr 28, 2021

I have run into this error as well when I recently started using constructor property promotion for validation classes, and the missing parameter target is never a big deal for the code/application itself - even when using parameter attributes in constructors with constructor property promotion (or intermixing them wildly, if that would be useful in the future), any library would only instantiate their own attributes and can easily avoid this fatal error, so it seems unlikely that a real problem would occur in the specific case of constructor property promotion. Tolerating this in PHPStan would be great.

@dbrekelmans
Copy link
Author

@ondrejmirtes Would you be able to provide some pointers? Maybe knowing where to look I might be able to submit a PR for this

@ondrejmirtes
Copy link
Member

@dbrekelmans The logic is here: https://github.com/phpstan/phpstan-src/blob/master/src/Rules/AttributesCheck.php

Check the places where the check method is called from. I expect a test to be added to one of the rules that calls this class.

@ondrejmirtes
Copy link
Member

Fixed by: phpstan/phpstan-src#528

@github-actions
Copy link

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 Jun 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants