|
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; |
|
9
|
|
|
use PhpParser\Node\Expr\Assign; |
|
10
|
|
|
use PhpParser\Node\Stmt; |
|
11
|
|
|
use PhpParser\Node\Stmt\Do_; |
|
12
|
|
|
use PhpParser\Node\Stmt\ElseIf_; |
|
13
|
|
|
use PhpParser\Node\Stmt\If_; |
|
14
|
|
|
use PhpParser\Node\Stmt\Switch_; |
|
15
|
|
|
use PhpParser\Node\Stmt\While_; |
|
16
|
|
|
use PHPSA\Analyzer\Pass; |
|
17
|
|
|
use PHPSA\Context; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
19
|
|
|
|
|
20
|
|
|
class AssignmentInCondition implements Pass\ConfigurablePassInterface, Pass\AnalyzerPassInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param $stmt |
|
24
|
|
|
* @param Context $context |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
10 |
|
public function pass($stmt, Context $context) |
|
28
|
|
|
{ |
|
29
|
10 |
|
$result = false; |
|
30
|
|
|
|
|
31
|
10 |
|
if ($stmt instanceof If_) { |
|
32
|
5 |
|
$this->checkAssignment($stmt, $context); |
|
33
|
|
|
|
|
34
|
5 |
|
$elseifStmts = $stmt->elseifs; |
|
35
|
5 |
|
foreach ($elseifStmts as $elseif) { |
|
36
|
2 |
|
$result = $this->checkAssignment($elseif, $context) || $result; |
|
37
|
5 |
|
} |
|
38
|
10 |
|
} elseif ($stmt instanceof While_ || $stmt instanceof Do_) { |
|
39
|
5 |
|
$this->checkAssignment($stmt, $context); |
|
40
|
6 |
|
} elseif ($stmt instanceof Switch_) { |
|
41
|
2 |
|
$caseStmts = $stmt->cases; |
|
42
|
2 |
|
foreach ($caseStmts as $case) { |
|
43
|
2 |
|
$result = $this->checkAssignment($case, $context) || $result; |
|
44
|
2 |
|
} |
|
45
|
2 |
|
} |
|
46
|
|
|
|
|
47
|
10 |
|
return $result; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return TreeBuilder |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getConfiguration() |
|
54
|
|
|
{ |
|
55
|
|
|
$treeBuilder = new TreeBuilder(); |
|
56
|
|
|
$treeBuilder->root('assignment_in_condition') |
|
57
|
|
|
->canBeDisabled(); |
|
58
|
|
|
|
|
59
|
|
|
return $treeBuilder; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getRegister() |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
1 |
|
If_::class, |
|
69
|
1 |
|
ElseIf_::class, |
|
70
|
1 |
|
While_::class, |
|
71
|
1 |
|
Do_::class, |
|
72
|
1 |
|
Switch_::class, |
|
73
|
1 |
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Stmt $stmt |
|
78
|
|
|
* @param Context $context |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
10 |
|
private function checkAssignment(Stmt $stmt, Context $context) |
|
82
|
|
|
{ |
|
83
|
10 |
|
if ($stmt->cond instanceof Assign) { |
|
|
|
|
|
|
84
|
4 |
|
$context->notice( |
|
85
|
4 |
|
'assignment_in_condition', |
|
86
|
4 |
|
'An assignment statement has been made instead of conditional statement', |
|
87
|
|
|
$stmt |
|
88
|
4 |
|
); |
|
89
|
4 |
|
return true; |
|
90
|
|
|
} |
|
91
|
7 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.