|
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\Stmt; |
|
9
|
|
|
use PHPSA\Analyzer\Pass; |
|
10
|
|
|
use PHPSA\Context; |
|
11
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
12
|
|
|
|
|
13
|
|
|
class MissingBreakStatement implements Pass\ConfigurablePassInterface, Pass\AnalyzerPassInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param Stmt\Switch_ $switchStmt |
|
17
|
|
|
* @param Context $context |
|
18
|
|
|
* @return bool |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function pass(Stmt\Switch_ $switchStmt, Context $context) |
|
21
|
|
|
{ |
|
22
|
2 |
|
$result = false; |
|
23
|
2 |
|
$caseStmts = $switchStmt->cases; |
|
24
|
|
|
|
|
25
|
2 |
|
if (count($caseStmts) < 2) { |
|
26
|
1 |
|
return $result; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
array_pop($caseStmts); // the last case statement CAN have no "break" or "return" |
|
30
|
|
|
|
|
31
|
|
|
/** @var Stmt\Case_ $case */ |
|
32
|
2 |
|
foreach ($caseStmts as $case) { |
|
33
|
2 |
|
$result = $this->checkCaseStatement($case, $context) || $result; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
return $result; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return TreeBuilder |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getConfiguration() |
|
43
|
|
|
{ |
|
44
|
|
|
$treeBuilder = new TreeBuilder(); |
|
45
|
|
|
$treeBuilder->root('missing_break_statement') |
|
46
|
|
|
->canBeDisabled() |
|
47
|
|
|
; |
|
48
|
|
|
|
|
49
|
|
|
return $treeBuilder; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function getRegister() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
Stmt\Switch_::class |
|
59
|
1 |
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param Stmt\Case_ $case |
|
64
|
|
|
* @param Context $context |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
2 |
|
private function checkCaseStatement(Stmt\Case_ $case, Context $context) |
|
68
|
|
|
{ |
|
69
|
|
|
/* |
|
70
|
|
|
* switch(…) { |
|
71
|
|
|
* case 41: |
|
72
|
|
|
* case 42: |
|
73
|
|
|
* case 43: |
|
74
|
|
|
* return 'the truth, or almost.'; |
|
75
|
|
|
* } |
|
76
|
|
|
*/ |
|
77
|
2 |
|
if (!$case->stmts) { |
|
|
|
|
|
|
78
|
1 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
foreach ($case->stmts as $node) { |
|
82
|
|
|
// look for a break statement |
|
83
|
2 |
|
if ($node instanceof Stmt\Break_) { |
|
84
|
2 |
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// or for a return |
|
88
|
1 |
|
if ($node instanceof Stmt\Return_) { |
|
89
|
1 |
|
return false; |
|
90
|
|
|
} |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$context->notice( |
|
94
|
1 |
|
'missing_break_statement', |
|
95
|
1 |
|
'Missing "break" statement', |
|
96
|
|
|
$case |
|
97
|
1 |
|
); |
|
98
|
|
|
|
|
99
|
1 |
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.