|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Medvedev Alexandr https://github.com/lexty <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace PHPSA\Analyzer\Pass\Expression; |
|
7
|
|
|
|
|
8
|
|
|
use PhpParser\Node\Expr\BooleanNot; |
|
9
|
|
|
use PHPSA\Analyzer\Pass\AnalyzerPassInterface; |
|
10
|
|
|
use PHPSA\Context; |
|
11
|
|
|
|
|
12
|
|
|
class LogicInversion implements AnalyzerPassInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected $map = [ |
|
15
|
|
|
'Expr_BinaryOp_Equal' => ['!=', '=='], |
|
16
|
|
|
'Expr_BinaryOp_NotEqual' => ['==', '!='], |
|
17
|
|
|
'Expr_BinaryOp_Identical' => ['===', '!=='], |
|
18
|
|
|
'Expr_BinaryOp_NotIdentical' => ['!==', '==='], |
|
19
|
|
|
'Expr_BinaryOp_Greater' => ['>', '<='], |
|
20
|
|
|
'Expr_BinaryOp_GreaterOrEqual' => ['>=', '<'], |
|
21
|
|
|
'Expr_BinaryOp_Smaller' => ['<', '>='], |
|
22
|
|
|
'Expr_BinaryOp_SmallerOrEqual' => ['<=', '>'], |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param BooleanNot $expr |
|
27
|
|
|
* @param Context $context |
|
28
|
|
|
* @return bool |
|
29
|
|
|
*/ |
|
30
|
11 |
|
public function pass(BooleanNot $expr, Context $context) |
|
31
|
|
|
{ |
|
32
|
11 |
|
if (!array_key_exists($expr->expr->getType(), $this->map)) { |
|
33
|
11 |
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
list($use, $instead) = $this->map[$expr->expr->getType()]; |
|
37
|
1 |
|
$msg = sprintf('Use "a %s b" expression instead of "!(a %s b)".', $use, $instead); |
|
38
|
|
|
|
|
39
|
1 |
|
$context->notice('logic_inversion', $msg, $expr); |
|
40
|
|
|
|
|
41
|
1 |
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getRegister() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
1 |
|
BooleanNot::class, |
|
51
|
1 |
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|