Completed
Push — master ( 3708b9...fb0aff )
by Enrico
03:36
created

MissingBreakStatement   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 18 4
A getRegister() 0 6 1
B checkCaseStatement() 0 34 5
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
12
class MissingBreakStatement implements Pass\AnalyzerPassInterface
13
{
14
    /**
15
     * @param Stmt\Switch_ $switchStmt
16
     * @param Context $context
17
     * @return bool
18
     */
19 2
    public function pass(Stmt\Switch_ $switchStmt, Context $context)
20
    {
21 2
        $result = false;
22 2
        $caseStmts = $switchStmt->cases;
23
24 2
        if (count($caseStmts) < 2) {
25 1
            return $result;
26
        }
27
28 2
        array_pop($caseStmts); // the last case statement CAN have no "break" or "return"
29
30
        /** @var Stmt\Case_ $case */
31 2
        foreach ($caseStmts as $case) {
32 2
            $result = $this->checkCaseStatement($case, $context) || $result;
33 2
        }
34
35 2
        return $result;
36
    }
37
38
    /**
39
     * @return array
40
     */
41 1
    public function getRegister()
42
    {
43
        return [
44
            Stmt\Switch_::class
45 1
        ];
46
    }
47
48
    /**
49
     * @param Stmt\Case_ $case
50
     * @param Context $context
51
     * @return bool
52
     */
53 2
    private function checkCaseStatement(Stmt\Case_ $case, Context $context)
54
    {
55
        /*
56
         * switch(…) {
57
         *     case 41:
58
         *     case 42:
59
         *     case 43:
60
         *         return 'the truth, or almost.';
61
         * }
62
         */
63 2
        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...
64 1
            return false;
65
        }
66
67 2
        foreach ($case->stmts as $node) {
68
            // look for a break statement
69 2
            if ($node instanceof Stmt\Break_) {
70 2
                return false;
71
            }
72
73
            // or for a return
74 1
            if ($node instanceof Stmt\Return_) {
75 1
                return false;
76
            }
77 1
        }
78
79 1
        $context->notice(
80 1
            'missing_break_statement',
81 1
            'Missing "break" statement',
82
            $case
83 1
        );
84
85 1
        return true;
86
    }
87
}
88