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 on mixed attribute usage on promoted property #10298
Comments
The reason for this seems to be quite simple. This because I think the code can be simplified to just one run, with |
Hi, PHPStan is right here. See this example in PHP runtime: https://3v4l.org/cg0Uj |
Hmm. My actual code is using And I believe that PHPStan is still slightly wrong then? Because it states that Furthermore the code now thus seems to support a case which isn't supported by PHP (i.e.: the code I changed was incorrect anyway). Which is also confirmed by running the test file: https://3v4l.org/Vr19G While PHP itself gives more errors, for every usage of
So I guess the code I changed, should actually be dropped altogether? (/the conditional promoted property stuff should be dropped). |
I don't know, you can try sending a PR and we'll see what happens, either it makes sense or it doesn't 😊 |
Well, it would mean simply dropping these lines: But... I think that's wrong too, and even more wrong then the current code. So then we can return to my original issue / PR 😃 So to conclude I think this issue still is valid, and the PR is fine. But there should be a feature (request) for an additional rule which checks that in case when one does #[Attribute(Attribute::TARGET_CLASS)]
class Attr {}
function test() {}
$f = new ReflectionFunction('test');
$f->getAttributes(Attr::class); So something like "Attr attribute is not a valid attribute for a function". |
Attributes with property target cannot be used above promoted properties. Pure and simple. It's PHPStan's job to report this. You have the following options: * Ignore this error if you think it's safe to have such code: https://phpstan.org/user-guide/ignoring-errors
|
So to conclude, you would say that phpstan/phpstan-src#528 shouldn't have been merged before, and that #4418 should have been closed, and stay closed? In other words: you already came back on the "PHP doesn't allow this, and neither should PHPStan" before, so are you now reverting back to the original "PHPStan shouldn't support this"? If so I will create a PR to revert phpstan/phpstan-src#528 And to be clear: no, I don't like the PHP behavior either. But it does work, and it clearly is used (as others already requested this feature well over 2 years ago, and my issue and PR just report a minor defect in the current implementation where applying both incorrectly reports an error, while applying either of them works just fine). |
@ondrejmirtes could you please clarify your position in this? You mentioned earlier that PHPStan should report an error when a Furthermore, as I mentioned earlier. If this is your test case to show that PHP doesn't allow property attributes on promoted properties: $t = new ReflectionClass(Test::class);
$a = $t->getConstructor()->getParameters()[0]->getAttributes();
foreach ($a as $aa){
$aa->newInstance();
} (as per your comment: #10298 (comment)) This is the case because of phpstan/phpstan-src#528, which fixes the in 2021, by @dbrekelmans reported, issue #4418 to add support for property attributes on parameters. While initially you took the same stance: "Not true, PHPStan is right. Runtime proof: https://3v4l.org/tBjj2". After later comments by @dbrekelmans, @dktapps, @michaelzangerle, @derrabus and @iquito you reconsidered the issue and came to the conclusion that it should be supported. So I really don't understand where the current stance on "PHP doesn't support this, and neither should PHPStan" comes from. As @iquito wrote:
And returning to my example. When only reading the attributes for their intended target, everything works fine without any errors: https://3v4l.org/5HXZD $paramAttr = $t->getConstructor()->getParameters()[0]->getAttributes(ParamAttr::class)[0];
$propAttr = $t->getProperty('test')->getAttributes(PropAttr::class)[0];
var_dump($paramAttr->newInstance());
var_dump($propAttr->newInstance()); Where the parameter targeted property is fetched using So the only case were PHP will actually throw an error is either the contrived example where you're iterating over all attributes (and instantiating all of them), or the (unlikely) case where the (library) developer makes an incorrect |
You're right, some errors were missed. The next release is going to include them (if you enable bleeding edge): phpstan/phpstan-src@25d1552 Thanks. Otherwise my points from this comment stand: #10298 (comment) Yeah, sure, if the user is careful and does not access something that would throw an error, your code might be fine. But this point is true for the whole field of static analysis. If you're careful and only pass classes that have method |
Not sure why I got notified about this, but my two cents is that it should be reported at the point of accessing the parameter attributes, and not at the declaration site. IMO, the way this should have been implemented in |
I think this is already deployed on the demo / try page, correct? I haven't gotten the time/means to go over the change yet. But when I rerun my example from the original report and enabling bleeding edge it will error on the
I'm sorry, but this completly contrived example doesn't make any sense to me. Of course this should result in an error because there is no guarantee such method exits.
And I'm reporting an issue with 1., and you keep coming back to 2.. |
We can agree to disagree. |
Bug report
When mixing both attributes that target parameters and attributes target properties on a promoted property an (incorrect) error will be reported for the attribute targeted at properties, that it isn't targeting properties and parameters.
(Found when mixing
#[SensitiveParameter]
with Symfony validator constraints on a promoted property)Code snippet that reproduces the problem
https://phpstan.org/r/302d5bf8-304b-49aa-b013-7757886fcf4c
Expected output
No errors, instead it fails on
PropAttr
not having property or parameter targetDid PHPStan help you today? Did it make you happy in any way?
No response
The text was updated successfully, but these errors were encountered: