Skip to content

Conversation

@nagmat84
Copy link
Contributor

This rule disallows using weak comparison via == or !=. Instead strong comparison via === or !== should be used. Also see phpstan/phpstan#7459.

@herndlm
Copy link
Contributor

herndlm commented Jun 12, 2022

Dedicated tests for that rule would be good. Even if it is that simple :)

@nagmat84
Copy link
Contributor Author

You mean two rule classes: one for equality and one for inequality?

@herndlm
Copy link
Contributor

herndlm commented Jun 12, 2022

No, I mean basically that I would expect this PR also to add a file DisallowedWeakComparisonRuleTest where this rule is tested with a data file. Similar to the already existing rules I guess

@nagmat84
Copy link
Contributor Author

I have added a test class.

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

class DisallowedWeakComparisonRuleTest extends RuleTestCase
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can get rid of the new phpstan errors added to the baseline if you specify the generic type in the same fashion as it is done in e.g. https://github.com/phpstan/phpstan-strict-rules/blob/1.2.3/tests/Rules/DisallowedConstructs/DisallowedShortTernaryRuleTest.php#L9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I don't mind which variant I use. I just took an example from the existing test classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by 02152d2

{
if ($node instanceof Equal || $node instanceof NotEqual) {
return [
'Weak comparison via "' . $node->getOperatorSigil() . '" is not allowed. Use strong comparison instead.',
Copy link
Member

Choose a reason for hiding this comment

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

The correct nomenclature isn't weak/strong, but loose/strict.

Also please use the correct alternative in the error message. == should have === in the second part of the message, != should have !== in the second part of the message.

Copy link
Member

Choose a reason for hiding this comment

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

Also: Please use RuleErrorBuilder here.

Copy link
Member

Choose a reason for hiding this comment

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

Also: Please use sprintf instead of string concatenation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be fixed by 56a7475. I only took the others rules in the directory as examples which don't use RuleErrorBuilder either.

Copy link
Contributor

@herndlm herndlm Jun 14, 2022

Choose a reason for hiding this comment

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

sorry, I don't want to hijack this too much, but I noticed that the class / rule name is still using the other terms. does it make sense to adapt them too for consistency?

Copy link
Member

Choose a reason for hiding this comment

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

Yes please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nicely spotted. Fixed that, too.

rules.neon Outdated
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
- PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule
- PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule
- PHPStan\Rules\DisallowedConstructs\DisallowedWeakComparisonRule
Copy link
Member

Choose a reason for hiding this comment

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

This rule has to be enabled only with bleedingEdge right now. Do a similar thing that was done here: phpstan/phpstan-doctrine@00e7f8f

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should also be fixed by 56a7475. But honestly, I don't know if I got it right. I am still lacking understanding of which configuration options are available, what they mean and how the interact with each other.


conditionalTags:
PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%
Copy link
Contributor

@staabm staabm Jun 16, 2022

Choose a reason for hiding this comment

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

@ondrejmirtes We are using bleeding edge to test @readonly and stuff in phpstan, but this rule would create thousands of errors for us in legacy apps.

wouldn‘t it make sense to have a additional feature flag, so one can enable bleeding edge but don‘t enable this rule?

Copy link
Member

Choose a reason for hiding this comment

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

Either we want to have this enabled for everyone when PHPStan 2.0 + phpstan-strict-rules 2.0 are released, or we don't.

In the first case (enable for everyone), the additional feature toggle doesn't make sense. Bleeding edge is a preview of what's to come in the next major version.

I made a Twitter poll about this: https://twitter.com/OndrejMirtes/status/1537418180888514560

Copy link
Member

Choose a reason for hiding this comment

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

BTW - instead of setting a feature toggle to false or whatever, you can always write a regex to ignore these problems.

@ondrejmirtes ondrejmirtes merged commit 484a051 into phpstan:1.2.x Jun 16, 2022
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants