Completed
Pull Request — master (#135)
by Kévin
03:21
created

MissingBreakStatement   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 80.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 89
ccs 25
cts 31
cp 0.8065
rs 10
wmc 11
lcom 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 18 4
A getConfiguration() 0 9 1
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
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 1
        $caseStmts = $switchStmt->cases;
24
25 1
        if (count($caseStmts) < 2) {
26
            return $result;
27
        }
28
29 1
        array_pop($caseStmts); // the last case statement CAN have no "break" or "return"
30
31
        /** @var Stmt\Case_ $case */
32 1
        foreach ($caseStmts as $case) {
33 1
            $result = $this->checkCaseStatement($case, $context) || $result;
34 1
        }
35
36 1
        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 1
    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 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...
78 1
            return false;
79
        }
80
81 1
        foreach ($case->stmts as $node) {
82
            // look for a break statement
83 1
            if ($node instanceof Stmt\Break_) {
84 1
                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