|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Kévin Gomez https://github.com/K-Phoen <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA\Analyzer\Pass\Statement; |
|
7
|
|
|
|
|
8
|
|
|
use PhpParser\Node\Expr\Assign; |
|
9
|
|
|
use PhpParser\Node\Stmt; |
|
10
|
|
|
use PhpParser\Node\Stmt\ElseIf_; |
|
11
|
|
|
use PhpParser\Node\Stmt\If_; |
|
12
|
|
|
use PHPSA\Analyzer\Pass; |
|
13
|
|
|
use PHPSA\Context; |
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
15
|
|
|
|
|
16
|
|
|
class AssignmentInCondition implements Pass\ConfigurablePassInterface, Pass\AnalyzerPassInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param $stmt |
|
20
|
|
|
* @param Context $context |
|
21
|
|
|
* @return bool |
|
22
|
|
|
*/ |
|
23
|
5 |
|
public function pass($stmt, Context $context) |
|
24
|
|
|
{ |
|
25
|
5 |
|
$result = false; |
|
26
|
|
|
|
|
27
|
5 |
|
if ($stmt instanceof If_) { |
|
28
|
5 |
|
if ($stmt->cond instanceof Assign) { |
|
29
|
2 |
|
$context->notice( |
|
30
|
2 |
|
'assignment_in_condition', |
|
31
|
2 |
|
'An assignment statement has been made instead of conditional statement', |
|
32
|
|
|
$stmt |
|
33
|
2 |
|
); |
|
34
|
2 |
|
$result = true; |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
5 |
|
$elseifStmts = $stmt->elseifs; |
|
38
|
5 |
|
foreach ($elseifStmts as $elseif) { |
|
39
|
2 |
|
$result = $this->checkElseIfStatement($elseif, $context) || $result; |
|
40
|
5 |
|
} |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
5 |
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return TreeBuilder |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getConfiguration() |
|
50
|
|
|
{ |
|
51
|
|
|
$treeBuilder = new TreeBuilder(); |
|
52
|
|
|
$treeBuilder->root('assignment_in_condition') |
|
53
|
|
|
->canBeDisabled(); |
|
54
|
|
|
|
|
55
|
|
|
return $treeBuilder; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getRegister() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
1 |
|
If_::class, |
|
65
|
1 |
|
ElseIf_::class, |
|
66
|
1 |
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param ElseIf_ $elseif |
|
71
|
|
|
* @param Context $context |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
2 |
|
private function checkElseIfStatement(ElseIf_ $elseif, Context $context) |
|
75
|
|
|
{ |
|
76
|
2 |
|
if ($elseif->cond instanceof Assign) { |
|
77
|
1 |
|
$context->notice( |
|
78
|
1 |
|
'assignment_in_condition', |
|
79
|
1 |
|
'An assignment statement has been made instead of conditional statement', |
|
80
|
|
|
$elseif |
|
81
|
1 |
|
); |
|
82
|
1 |
|
return true; |
|
83
|
|
|
} |
|
84
|
2 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|