Skip to content

Commit 484a051

Browse files
authored
Bleeding edge - rule to disallow loose comparisons
1 parent 110818b commit 484a051

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

rules.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ rules:
5252
- PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule
5353
- PHPStan\Rules\VariableVariables\VariableVariablesRule
5454

55+
conditionalTags:
56+
PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule:
57+
phpstan.rules.rule: %featureToggles.bleedingEdge%
58+
5559
services:
5660
-
5761
class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper
@@ -71,3 +75,6 @@ services:
7175
universalObjectCratesClasses: %universalObjectCratesClasses%
7276
tags:
7377
- phpstan.rules.rule
78+
79+
-
80+
class: PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$bool1 = 123 == 456;
4+
$bool2 = 123 === 456;
5+
$bool3 = 123 != 456;
6+
$bool4 = 123 !== 456;

0 commit comments

Comments
 (0)