Completed
Pull Request — master (#312)
by algo13
03:37
created

MissingBreakStatement::checkCaseStatement()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 36
ccs 16
cts 17
cp 0.9412
crap 8.013
rs 5.3846
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 2
    public function getRegister()
47
    {
48
        return [
49
            Stmt\Switch_::class
50 2
        ];
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 === null) {
69
            return false;
70
        }
71
72 1
        $stmt = end($case->stmts);
73 1
        while ($stmt !== false) {
74 1
            if ($stmt instanceof Stmt\Break_ || $stmt instanceof Stmt\Return_
75 1
            || $stmt instanceof Stmt\Throw_ || $stmt instanceof Stmt\Continue_) {
76 1
                break;
77
            }
78
79 1
            if (!$stmt instanceof Stmt\Nop) {
80 1
                $context->notice(
81 1
                    'missing_break_statement',
82 1
                    'Missing "break" statement',
83
                    $case
84 1
                );
85
86 1
                return true;
87
            }
88
89 1
            $stmt = prev($case->stmts);
90 1
        }
91
92 1
        return false;
93
    }
94
}
95