File tree Expand file tree Collapse file tree 4 files changed +92
-0
lines changed
src/Rules/DisallowedConstructs
tests/Rules/DisallowedConstructs Expand file tree Collapse file tree 4 files changed +92
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,10 @@ rules:
52
52
- PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule
53
53
- PHPStan\Rules\VariableVariables\VariableVariablesRule
54
54
55
+ conditionalTags :
56
+ PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule :
57
+ phpstan.rules.rule : %featureToggles.bleedingEdge%
58
+
55
59
services :
56
60
-
57
61
class : PHPStan\Rules\BooleansInConditions\BooleanRuleHelper
@@ -71,3 +75,6 @@ services:
71
75
universalObjectCratesClasses : %universalObjectCratesClasses%
72
76
tags :
73
77
- phpstan.rules.rule
78
+
79
+ -
80
+ class : PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \DisallowedConstructs ;
4
+
5
+ use PhpParser \Node ;
6
+ use PhpParser \Node \Expr \BinaryOp ;
7
+ use PhpParser \Node \Expr \BinaryOp \Equal ;
8
+ use PhpParser \Node \Expr \BinaryOp \NotEqual ;
9
+ use PHPStan \Analyser \Scope ;
10
+ use PHPStan \Rules \Rule ;
11
+ use PHPStan \Rules \RuleErrorBuilder ;
12
+
13
+ /**
14
+ * @implements Rule<BinaryOp>
15
+ */
16
+ class DisallowedLooseComparisonRule implements Rule
17
+ {
18
+
19
+ public function getNodeType (): string
20
+ {
21
+ return BinaryOp::class;
22
+ }
23
+
24
+ public function processNode (Node $ node , Scope $ scope ): array
25
+ {
26
+ if ($ node instanceof Equal) {
27
+ return [
28
+ RuleErrorBuilder::message (
29
+ 'Loose comparison via "==" is not allowed. '
30
+ )->tip ('Use strict comparison via "===" instead. ' )->build (),
31
+ ];
32
+ }
33
+ if ($ node instanceof NotEqual) {
34
+ return [
35
+ RuleErrorBuilder::message (
36
+ 'Loose comparison via "!=" is not allowed. '
37
+ )->tip ('Use strict comparison via "!==" instead. ' )->build (),
38
+ ];
39
+ }
40
+
41
+ return [];
42
+ }
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace PHPStan \Rules \DisallowedConstructs ;
4
+
5
+ use PHPStan \Rules \Rule ;
6
+ use PHPStan \Testing \RuleTestCase ;
7
+
8
+ /**
9
+ * @extends RuleTestCase<DisallowedLooseComparisonRule>
10
+ */
11
+ class DisallowedLooseComparisonRuleTest extends RuleTestCase
12
+ {
13
+
14
+ protected function getRule (): Rule
15
+ {
16
+ return new DisallowedLooseComparisonRule ();
17
+ }
18
+
19
+ public function testRule (): void
20
+ {
21
+ $ this ->analyse ([__DIR__ . '/data/weak-comparison.php ' ], [
22
+ [
23
+ 'Loose comparison via "==" is not allowed. ' ,
24
+ 3 ,
25
+ 'Use strict comparison via "===" instead. ' ,
26
+ ],
27
+ [
28
+ 'Loose comparison via "!=" is not allowed. ' ,
29
+ 5 ,
30
+ 'Use strict comparison via "!==" instead. ' ,
31
+ ],
32
+ ]);
33
+ }
34
+
35
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ $ bool1 = 123 == 456 ;
4
+ $ bool2 = 123 === 456 ;
5
+ $ bool3 = 123 != 456 ;
6
+ $ bool4 = 123 !== 456 ;
You can’t perform that action at this time.
0 commit comments