Completed
Pull Request — master (#287)
by algo13
02:34
created

MissingBreakStatement::getRegister()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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\Helper\DefaultMetadataPassTrait;
10
use PHPSA\Analyzer\Pass;
11
use PHPSA\Context;
12
13
class MissingBreakStatement implements Pass\AnalyzerPassInterface
14
{
15
    use DefaultMetadataPassTrait;
16
17
    const DESCRIPTION = 'Checks for a missing break or return statement in switch cases. Can ignore empty cases and the last case.';
18
19
    /**
20
     * @param Stmt\Switch_ $switchStmt
21
     * @param Context $context
22
     * @return bool
23
     */
24 1
    public function pass(Stmt\Switch_ $switchStmt, Context $context)
25
    {
26 1
        $result = false;
27 1
        $caseStmts = $switchStmt->cases;
28
29 1
        if (count($caseStmts) < 2) {
30
            return $result;
31
        }
32
33 1
        array_pop($caseStmts); // the last case statement CAN have no "break" or "return"
34
35
        /** @var Stmt\Case_ $case */
36 1
        foreach ($caseStmts as $case) {
37 1
            $result = $this->checkCaseStatement($case, $context) || $result;
38 1
        }
39
40 1
        return $result;
41
    }
42
43
    /**
44
     * @return array
45
     */
46 1
    public function getRegister()
47
    {
48
        return [
49
            Stmt\Switch_::class
50 1
        ];
51
    }
52
53
    /**
54
     * @param Stmt\Case_ $case
55
     * @param Context $context
56
     * @return bool
57
     */
58 1
    private function checkCaseStatement(Stmt\Case_ $case, Context $context)
59
    {
60
        /*
61
         * switch(…) {
62
         *     case 41:
63
         *     case 42:
64
         *     case 43:
65
         *         return 'the truth, or almost.';
66
         * }
67
         */
68 1
        if (!$case->stmts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $case->stmts of type PhpParser\Node[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
69 1
            return false;
70
        }
71
72 1
        foreach ($case->stmts as $node) {
73
            // look for a break statement
74 1
            if ($node instanceof Stmt\Break_) {
75 1
                return false;
76
            }
77
78
            // or for a return
79 1
            if ($node instanceof Stmt\Return_) {
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