phpstan-strict-rules icon indicating copy to clipboard operation
phpstan-strict-rules copied to clipboard

PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule is not

Open VincentLanglet opened this issue 2 years ago • 25 comments

Configuration was introduced for rules in https://github.com/phpstan/phpstan-strict-rules/pull/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% ?

VincentLanglet avatar Sep 02 '22 09:09 VincentLanglet

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

MartinMystikJonas avatar Sep 02 '22 10:09 MartinMystikJonas

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.

VincentLanglet avatar Sep 02 '22 10:09 VincentLanglet

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

MartinMystikJonas avatar Sep 02 '22 11:09 MartinMystikJonas

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?

ondrejmirtes avatar Sep 02 '22 16:09 ondrejmirtes

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

MartinMystikJonas avatar Sep 02 '22 17:09 MartinMystikJonas

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

MartinMystikJonas avatar Sep 02 '22 17:09 MartinMystikJonas

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

MartinMystikJonas avatar Sep 02 '22 18:09 MartinMystikJonas

Can you imagine doing it with a conditionalIncludes compiler extension?

ondrejmirtes avatar Sep 02 '22 18:09 ondrejmirtes

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.

MartinMystikJonas avatar Sep 02 '22 18:09 MartinMystikJonas

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

ondrejmirtes avatar Sep 03 '22 07:09 ondrejmirtes

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

MartinMystikJonas avatar Sep 03 '22 07:09 MartinMystikJonas

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

ondrejmirtes avatar Sep 03 '22 07:09 ondrejmirtes

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.

MartinMystikJonas avatar Sep 03 '22 08:09 MartinMystikJonas

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?

ondrejmirtes avatar Sep 03 '22 08:09 ondrejmirtes

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.

MartinMystikJonas avatar Sep 03 '22 08:09 MartinMystikJonas

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.

ondrejmirtes avatar Sep 03 '22 08:09 ondrejmirtes

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 avatar Sep 03 '22 08:09 MartinMystikJonas

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.

MartinMystikJonas avatar Sep 03 '22 08:09 MartinMystikJonas

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

ondrejmirtes avatar Sep 03 '22 09:09 ondrejmirtes

OK I will prepere PRs

MartinMystikJonas avatar Sep 03 '22 09:09 MartinMystikJonas

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

MartinMystikJonas avatar Sep 03 '22 18:09 MartinMystikJonas

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().

ondrejmirtes avatar Sep 03 '22 19:09 ondrejmirtes

Frist PR ready for review here: https://github.com/phpstan/phpstan-src/pull/1697 Then I will make change in strict-rules

MartinMystikJonas avatar Sep 05 '22 08:09 MartinMystikJonas

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

MartinMystikJonas avatar Sep 05 '22 08:09 MartinMystikJonas

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 :)

herndlm avatar Sep 05 '22 11:09 herndlm

Solved by https://github.com/phpstan/phpstan-strict-rules/pull/191 I think

VincentLanglet avatar Oct 22 '22 21:10 VincentLanglet

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[bot] avatar Nov 23 '22 00:11 github-actions[bot]