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

Scoped allowCount to fully qualified class name #90

Closed
wants to merge 4 commits into from

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Oct 27, 2021

Fixes #89

See the updated readme. That should explain how the feature works now. Does it make sense?

For me it does, as I now have the following configuration in my project:

parameters:
    disallowedMethodCalls:
        -
            method: 'Generated\GraphQL\Query\*Query::execute*()'
            message: 'do not reuse GraphQL queries.'
            allowCount: 1

This now works with the ±20 generated Query classes we have. And it correctly errors 5 times where 1 Generated\GraphQL\Query\SomeQuery was used 6 times.

@ruudk
Copy link
Contributor Author

ruudk commented Oct 27, 2021

After running this on my project, I think what I'm trying to do is not possible at all:

  1. PHPStan runs in parallel by default, that means that holding any state inside the DisallowedCall is not synced through all processes. This leads to weird situations where sometimes it errors and sometime it doesn't.

I can solve this by running without parallel processing using --debug. But that's just super slow.

  1. PHPStan has a cache, and only analyzes files that changed. That means that already cached errors don't update the $timesAllowed property in DisallowedCall.

To solve this, I think we need to directly use the Cache provided by PHPStan. This could solve problem 2, but I don't know if it will work with parallel processing.

It feels that I'm trying to solve a problem that PHPStan was not created for.

Maybe @ondrejmirtes has an insight? 🙏

@ondrejmirtes
Copy link
Contributor

Hi, I think you can approximate the same feature by using count in ignoreErrors (which is used by the baseline but you can also use it in manually written config).

Let a rule report every error, and then tell PHPStan that X reports per file are okay with ignoreErrors.

@ruudk
Copy link
Contributor Author

ruudk commented Oct 27, 2021

The problem is, I want the opposite.

I want to allow GeneratedClass::execute to only be used in 1 file. I don't care where. If it's used in 2 (or more) files, it should error.

Maybe I should have a Rule that puts the error on the source file (GeneratedClass.php) instead of the file that calls it. That would allow me to use the count property.

But, that means that whenever a new class is generated, a new entry should be added to the ignoreErrors.

@ruudk
Copy link
Contributor Author

ruudk commented Oct 27, 2021

After thinking more about this problem and how I'm trying to solve it, I came to the conclusion that PHPStan is not the best tool to do this type of blocking.

I decided to solve it differently.

Thanks for the feedback @ondrejmirtes

@ruudk ruudk closed this Oct 27, 2021
@ruudk ruudk deleted the scoped-allow-count branch October 27, 2021 16:25
@ruudk ruudk mentioned this pull request Oct 27, 2021
@ruudk
Copy link
Contributor Author

ruudk commented Jul 21, 2022

I think this could now be solved by using PHPStan's new collectors feature. First collect all usages and counts over multiple threads. And then have another rule that uses those consolidated counts to report the errors.

@ondrejmirtes
Copy link
Contributor

Yes, definitely! The collectors are really easy :)

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

Successfully merging this pull request may close these issues.

allowCount should be scoped to FQCN
2 participants