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

Introduce feature flag for extension-installer #11714

Closed
ruudk opened this issue Sep 18, 2024 · 8 comments
Closed

Introduce feature flag for extension-installer #11714

ruudk opened this issue Sep 18, 2024 · 8 comments

Comments

@ruudk
Copy link
Contributor

ruudk commented Sep 18, 2024

Feature request

Currently, PHPStan automatically tries to load PHPStan\ExtensionInstaller\GeneratedConfig via the autoloader. When it exists, it must be because phpstan/extension-installer was installed. This works great.

Except for when you want to run PHPStan conditionally, without it picking up all extensions.

For example, in TwigStan I execute PHPStan to analyze compiled Twig templates. But there I don't want phpstan-strict-rules to be picked up automatically. Especially since compiled Twig templates don't follow these strict rules. If it was only used on my project, I could have turned off the extension-installer and went for a manual approach. But since this will be used by others as well, they most likely will be using the extension-installer.

Can we introduce a feature flag, that's enabled by default, but can be turned off when needed? I can create the PR obviously.

Currently I deal with it in a very naive way:
https://github.com/twigstan/twigstan/blob/a5a746a4bec2c735ff1fc2dbbf6feb8d877f0f6c/src/Application/AnalyzeCommand.php#L303-L307
https://github.com/twigstan/twigstan/blob/a5a746a4bec2c735ff1fc2dbbf6feb8d877f0f6c/src/Application/AnalyzeCommand.php#L379-L383

Did PHPStan help you today? Did it make you happy in any way?

No response

@staabm
Copy link
Contributor

staabm commented Sep 18, 2024

in our projects we also do some ugly 'dance' to enable/disable extensions in some cases.
in our setup we "disable" the whole extension-installer with composer remove phpstan/extension-installer on a by-case basis and enable extensions manually with prepared dedicated phpstan.neon configs.

did you realize you can put extensions on a ignore list ?

@ruudk
Copy link
Contributor Author

ruudk commented Sep 18, 2024

did you realize you can put extensions on a ignore list ?

Yes but given that TwigStan runs within the project of the user, it won't make any difference. It would require people to disable the extension for normal PHPStan analysis as well.

@ondrejmirtes
Copy link
Member

Hey, I thought about this but I think it's against your interests.

PHPStan extensions are not just about extra rules. They are also about making analysis smarter (more precise type inference). I think that Twigstan should include user's PHPStan config and just override some stuff it needs. Otherwise you're at risk collecting information from PHP code (controllers etc.) with dumber analysis than PHPStan is usually capable of when analysing the cobase.

Because with the above in mind, you're now at risk of having some errors reported besides the things Twigstan itself reports (I mean the final CollectedDataNode rule that provides metadata to the error formatter), you should be a little bit more defensive and filter the errors reported by your own identifier.

If you do that then there's basically no harm in extra rules being run while Twigstan is doing its job collecting data for further execution.

@ruudk
Copy link
Contributor Author

ruudk commented Sep 19, 2024

Thanks. You're right, extensions have more benefits than just adding errors.

I will try to support the extension-installer out of the box, and manually ignore the errors reported by phpstan-strict-rules.

I can use this snippet to extract those identifiers (from time to time):

curl -s https://raw.githubusercontent.com/phpstan/phpstan/2.0.x/website/src/errorsIdentifiers.json |  jq -r '[ to_entries[] | select(.value | .[] | has("phpstan/phpstan-strict-rules")) | .key ] | unique[]'

@ruudk ruudk closed this as not planned Won't fix, can't repro, duplicate, stale Sep 19, 2024
@ondrejmirtes
Copy link
Member

@ruudk You should allow-list only your known identifiers, not exclude errors from phpstan-strict-rules. You'll get many more errors reported by other extensions as well.

@ruudk
Copy link
Contributor Author

ruudk commented Sep 19, 2024

It's not that simple. I want all errors to be reported. TwigStan does not have any custom errors at the moment. The compiled Twig templates are just PHP after all.

This is perfectly fine Twig code:

{{ user.id == object.id ? 'a' : 'b' }}

But it produces this error:

Loose comparison via "==" is not allowed.
💡 Use strict comparison via "===" instead.
🔖 equal.notAllowed

This is because Twig is not as strict as PHP (can be). So the compiled code is also not as strict.

Maybe I should just ignore these type of identifiers when I spot them. Instead of trying to ignore all identifiers from phpstan-strict-rules.

@ondrejmirtes
Copy link
Member

I get it, I thought you were talking about the collector run, not the final "error run" 😊

@ruudk
Copy link
Contributor Author

ruudk commented Sep 19, 2024

Yes indeed.

The collector run works great with:

level: null
customRulesetUsed: true

Now that the extension-installer is no longer disabled, this does give the type improvement, but does not produce any errors, only collected data.

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

No branches or pull requests

3 participants