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

PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule is not #189

Closed
VincentLanglet opened this issue Sep 2, 2022 · 27 comments
Closed

Comments

@VincentLanglet
Copy link
Contributor

Configuration was introduced for rules in #182 (cc @MartinMystikJonas)
But not for PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule

Is there a reason ?

When using bleedingEdge (for other phpstan feature) it would be nice to still be able to disable the DisallowedLooseComparisonRule.

Should we create a new config parameter which default to %featureToggles.bleedingEdge% ?

@MartinMystikJonas
Copy link
Contributor

I did not touched it because it was already configurable. But I think it is good idea to add it condigurable and defaulted to bleeding edge. @ondrejmirtes Do you agree? I will make PR

@VincentLanglet
Copy link
Contributor Author

So far all the PHPStan\Rules\DisallowedConstructs rules were under strictRules.disallowedConstructs

Here I wonder if we will have an issue since if we add it under the same config, it will be a bc break
but if we don't, we will loose consistency.

We might also decide to split the strictRules.disallowedConstructs config.

For instance, DisallowedEmptyRule and DisallowedShortTernaryRule are rules which requires a lot of changes in a legacy code base. The DisallowedLooseComparisonRule too. And if I want to disable one of them, I have to disable such easier rules like DisallowedImplicitArrayCreationRule which is kinda sad.

@MartinMystikJonas
Copy link
Contributor

MartinMystikJonas commented Sep 2, 2022

I would preffer to keep BC and just add strictRules.disallowedLooseComparsion.

You can disable strictRules.disallowedConstructs and then add handpicked rules using:

rules:
  - PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule

@ondrejmirtes
Copy link
Member

I think it's a bit tricky to set up the configuration correctly.

So we want to have a new option to turn on/off DisallowedLooseComparisonRule comfortably. It needs to behave this way:

By default:

  • if bleedingEdge is not enabled and allRules is enabled, the rule CANNOT be enabled
  • if bleedingEdge is enabled and allRules is enabled, the rule MUST be enabled
  • if bleedingEdge is not enabled and allRules is not enabled, the rule CANNOT be enabled
  • if bleedingEdge is enabled and allRules is not enabled, the rule CANNOT be enabled

With this new option set to true:

  • if bleedingEdge is not enabled and allRules is enabled, the rule MUST be enabled
  • if bleedingEdge is enabled and allRules is enabled, the rule MUST be enabled
  • if bleedingEdge is not enabled and allRules is not enabled, the rule MUST be enabled
  • if bleedingEdge is enabled and allRules is not enabled, the rule MUST be enabled

With this new option set to false:

  • if bleedingEdge is not enabled and allRules is enabled, the rule CANNOT be enabled
  • if bleedingEdge is enabled and allRules is enabled, the rule CANNOT be enabled
  • if bleedingEdge is not enabled and allRules is not enabled, the rule CANNOT be enabled
  • if bleedingEdge is enabled and allRules is not enabled, the rule CANNOT be enabled

Does this make sense? Can this be achieved in neon config?

@MartinMystikJonas
Copy link
Contributor

@ondrejmirtes I am not sure it is possible in neon. It would require logical operations in config. I will look at it.

@MartinMystikJonas
Copy link
Contributor

MartinMystikJonas commented Sep 2, 2022

@ondrejmirtes Basically we would need a way to do:
DisallowedLooseComparisonRule = strictRule.disallowLooseComparsion ?? (bleedingEdge && strictRules.allRules)
Correct?

@MartinMystikJonas
Copy link
Contributor

It seems impossible in neon. But I have idea. What about make conditonalTags extension accept not only bool but also some struct with advanved conditions? I will think about some resonable format for that

@ondrejmirtes
Copy link
Member

Can you imagine doing it with a conditionalIncludes compiler extension?

@MartinMystikJonas
Copy link
Contributor

MartinMystikJonas commented Sep 2, 2022

What about this?

parameters:
  strictRules:
    allRules: true
    disallowedLooseComparsion: [%strictRules.allRules%, %featureToggles.bleedingEdge%]
    # ...

parametersSchema:
  strictRules: structure([
    allRules: bool(),
    disallowedLooseComparsion: anyOf(bool(), arrayOf(bool()))
    # ...
  ])

conditionalTags:
  PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule:
    phpstan.rules.rule: %strictRules.disallowedLooseComparsion%

And extends ConditionalTagsExtension so it accepts not only bool but also array of bools that all have to be true for tag to be added.

@ondrejmirtes
Copy link
Member

I'd like disallowedLooseComparsion: and(%strictRules.allRules%, %featureToggles.bleedingEdge%) if it's possible...

@MartinMystikJonas
Copy link
Contributor

Not sure of it is possible and it definetly would be way more complicated.

@ondrejmirtes
Copy link
Member

My issue with [%strictRules.allRules%, %featureToggles.bleedingEdge%] is that it's not obvious whether it's && or ||...

@MartinMystikJonas
Copy link
Contributor

I know it is not ideal but there is no easy way to do it. Problem is that it needs to be evaluated compile-time and nette/di does not support this. So it would (as far as I know) require writing own NeonAdapter and ParametersExtension. Otherwise everything that is not primitive type is converted to Statement and transformed to PHP code evaluated run-time.

Only other option I can think about is [%strictRules.allRules%, '&&' , %featureToggles.bleedingEdge%] and writing own expression evaluation in ConditionalTagsExtension. It can be done (I implemented similar thing for https://github.com/dermatthes/text-template) but it is lots of work and I do not like this format with "random" strings.

@ondrejmirtes
Copy link
Member

We already have custom NeonAdapter to expand relative paths based on the directory where the config file is: https://github.com/phpstan/phpstan-src/blob/1.8.x/src/DependencyInjection/NeonAdapter.php

Do you see a way to adapt that?

@MartinMystikJonas
Copy link
Contributor

I need to look at it. It woudl require to detect and() as special type of construct that is transformed to something else than Statement (and I am not sure it would be accepted by later processing in nette/di) and then in ConditionalTagsExtension write code to evaluate these special objects.

@ondrejmirtes
Copy link
Member

Alright, but don't spend too much time on it :) We don't need to solve this at all as these rules are gonna have a standard parameter associated in 2.0.

@MartinMystikJonas
Copy link
Contributor

I am checking what could be done. At this point it seems that easiest and best option would be PR to nette/di that will add this possibility there. It would require many changes in many files and maintainig own versions of all these files would be burden for PHPStan.

@MartinMystikJonas
Copy link
Contributor

Or we can just use that option with array and document it somewhere that it is always AND. As far as I can see ConditionalTagsExtension is more or less internal thing anyways.

@ondrejmirtes
Copy link
Member

Alright, array it is. Please send the pull requests :)

@MartinMystikJonas
Copy link
Contributor

OK I will prepere PRs

@MartinMystikJonas
Copy link
Contributor

@ondrejmirtes There are no tests for ConditionalTagsExtension? I finished implementation but I wanted to add tests for it and I did not found any existing test for it.

@ondrejmirtes
Copy link
Member

Probably not, it's tested by the fact that the configuration works as expected.

You could test it by extending PHPStanTestCase, providing different neon files in overriden getAdditionalConfigFiles(), and inspecting what's in self::getContainer()->getParameters().

@MartinMystikJonas
Copy link
Contributor

MartinMystikJonas commented Sep 5, 2022

Frist PR ready for review here: phpstan/phpstan-src#1697 Then I will make change in strict-rules

@MartinMystikJonas
Copy link
Contributor

We would need phpstan release so we can bump required phpstan version to version with multiple conditions support first.

@herndlm
Copy link
Contributor

herndlm commented Sep 5, 2022

The phpstan release is most likely not necessary. After it was merged you should the able to require the next patch version and it will pull the current dev branch :)

@VincentLanglet
Copy link
Contributor Author

Solved by #191 I think

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants