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

Add always used method extension #2927

Merged
merged 1 commit into from Mar 20, 2024

Conversation

axlon
Copy link
Contributor

@axlon axlon commented Feb 20, 2024

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.

@axlon
Copy link
Contributor Author

axlon commented Mar 12, 2024

@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

@ondrejmirtes
Copy link
Member

@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.

Copy link
Member

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@axlon axlon force-pushed the always-used-method-extension branch from d3215b6 to 216033b Compare March 20, 2024 17:37
@axlon axlon requested a review from ondrejmirtes March 20, 2024 17:38
@ondrejmirtes ondrejmirtes merged commit 030bd55 into phpstan:1.10.x Mar 20, 2024
437 of 441 checks passed
@ondrejmirtes
Copy link
Member

Perfect, thank you!

@thg2k
Copy link
Contributor

thg2k commented Mar 21, 2024

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 {
}

@ondrejmirtes
Copy link
Member

Wouldn't an annotation be more suitable for this job

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.

@thg2k
Copy link
Contributor

thg2k commented Mar 21, 2024

You can write an annotation - an ignore annotation :)

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.

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 very true.

Thx!

@ondrejmirtes
Copy link
Member

but it would ignore every possible error, not only the intended one

This is going to be solved in PHPStan 1.11 with error identifiers and new @phpstan-ignore tag. See: https://phpstan.org/user-guide/ignoring-errors#ignoring-in-code-using-phpdocs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants