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
Add always used method extension #2927
Add always used method extension #2927
Conversation
@ondrejmirtes is this something you would consider adding to PHPStan? If something is missing or needs clarification please let me know, happy to look at it! PS I noticed 2 actions are failing, but I don't know why or if it even related to this PR |
@axlon Don't worry, I really like this PR, it's well thought-out, I just didn't have much time to look into this thorougly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides one little thing this works for me :) Thank you!
|
||
use PHPStan\Reflection\MethodReflection; | ||
|
||
interface AlwaysUsedMethodExtension |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs @api
and also description similar to https://apiref.phpstan.org/1.11.x/source-src.PhpDoc.StubFilesExtension.html#5.
d3215b6
to
216033b
Compare
Perfect, thank you! |
I like the use case, I ran into something similar in the past. But I was wondering: Wouldn't an annotation be more suitable for this job rather than a plugin type? /**
* @phpstan-always-used
*/
private function my_method(): void {
} |
You can write an annotation - an ignore annotation :) The thing with extensions is that you can describe the logic generally. Instead of adding thousands of annotations in your code, you can write a single extension that describes the logic to mark the right methods as always used. |
That's surely a solution, but it would ignore every possible error, not only the intended one. I think I solved with a ignoreErrors template in the config file, like a regexp ignoring all kind of private unused errors, targeting the specific directory where the problematic classes reside.
That's very true. Thx! |
This is going to be solved in PHPStan 1.11 with error identifiers and new |
This PR adds a new extension type to PHPStan. This new extension type allows developers to tell PHPStan when a private method is used.
My personal use case:
I make use of a library that exposes a trait that dynamically calls methods on my objects, since I have no other uses for these methods I typically make them private. Since there are no direct references to these methods PHPStan reports them as unused.
I used #495 as a blueprint for this PR.
Lastly, one thing I'm somewhat unsure about is the positioning of the extension check within the rule, maybe moving it up or down somewhat could be beneficial to performance.