Completed
Push — master ( b3f6ba...781244 )
by Дмитрий
9s
created

MissingBreakStatement   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 84.85%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 92
ccs 28
cts 33
cp 0.8485
rs 10
wmc 11
lcom 0
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pass() 0 11 3
A getConfiguration() 0 9 1
A getRegister() 0 6 1
B checkCaseStatement() 0 44 6
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
        /*
63
         * switch(…) {
64
         *     case 41:
65
         *     case 42:
66
         *     case 43:
67
         *         return 'the truth, or almost.';
68
         * }
69
         */
70 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...
71 1
            return false;
72
        }
73
74 1
        foreach ($case->stmts as $node) {
75
            // look for a break statement
76 1
            if ($node instanceof Stmt\Break_) {
77 1
                return false;
78
            }
79
80
            // or for a return
81 1
            if ($node instanceof Stmt\Return_) {
82
                // but emit a notice if it's empty.
83 1
                if (!$node->expr) {
84 1
                    $context->notice(
85 1
                        'missing_break_statement',
86 1
                        'Empty return in "case" statement',
87
                        $node
88 1
                    );
89 1
                    return true;
90
                }
91
92 1
                return false;
93
            }
94 1
        }
95
96 1
        $context->notice(
97 1
            'missing_break_statement',
98 1
            'Missing "break" statement',
99
            $case
100 1
        );
101
102 1
        return true;
103
    }
104
}
105