-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathOperandsInComparisonEqualRule.php
52 lines (42 loc) · 1.3 KB
/
OperandsInComparisonEqualRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Operators;
use PHPStan\Type\VerbosityLevel;
class OperandsInComparisonEqualRule implements \PHPStan\Rules\Rule
{
/** @var OperatorRuleHelper */
private $helper;
public function __construct(OperatorRuleHelper $helper)
{
$this->helper = $helper;
}
public function getNodeType(): string
{
return \PhpParser\Node\Expr\BinaryOp\Equal::class;
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\Equal $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);
$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in ==, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in ==, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}
return $messages;
}
}