|
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
|
1 |
|
public function pass(Stmt\Switch_ $switchStmt, Context $context) |
|
21
|
|
|
{ |
|
22
|
1 |
|
$result = false; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Stmt\Case_ $case */ |
|
25
|
1 |
|
foreach ($switchStmt->cases as $case) { |
|
26
|
1 |
|
$result = $this->checkCaseStatement($case, $context) || $result; |
|
27
|
1 |
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
return $result; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return TreeBuilder |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getConfiguration() |
|
36
|
|
|
{ |
|
37
|
|
|
$treeBuilder = new TreeBuilder(); |
|
38
|
|
|
$treeBuilder->root('missing_break_statement') |
|
39
|
|
|
->canBeDisabled() |
|
40
|
|
|
; |
|
41
|
|
|
|
|
42
|
|
|
return $treeBuilder; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function getRegister() |
|
49
|
|
|
{ |
|
50
|
|
|
return [ |
|
51
|
|
|
Stmt\Switch_::class |
|
52
|
1 |
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Stmt\Case_ $case |
|
57
|
|
|
* @param Context $context |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
1 |
|
private function checkCaseStatement(Stmt\Case_ $case, Context $context) |
|
61
|
|
|
{ |
|
62
|
1 |
|
foreach ($case->stmts as $node) { |
|
63
|
|
|
// look for a break statement |
|
64
|
1 |
|
if ($node instanceof Stmt\Break_) { |
|
65
|
1 |
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// or for a return |
|
69
|
1 |
|
if ($node instanceof Stmt\Return_) { |
|
70
|
|
|
// but emit a notice if it's empty. |
|
71
|
1 |
|
if (!$node->expr) { |
|
72
|
1 |
|
$context->notice( |
|
73
|
1 |
|
'missing_break_statement', |
|
74
|
1 |
|
'Empty return in "case" statement', |
|
75
|
|
|
$node |
|
76
|
1 |
|
); |
|
77
|
1 |
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return false; |
|
81
|
|
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
$context->notice( |
|
85
|
1 |
|
'missing_break_statement', |
|
86
|
1 |
|
'Missing "break" statement', |
|
87
|
|
|
$case |
|
88
|
1 |
|
); |
|
89
|
|
|
|
|
90
|
1 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|