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

SwitchSt::compile()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9.9957

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 20
c 2
b 1
f 0
nc 12
nop 2
dl 0
loc 34
ccs 20
cts 26
cp 0.7692
crap 9.9957
rs 4.909
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler\Statement;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
use PHPSA\Compiler\Expression;
11
12
class SwitchSt extends AbstractCompiler
13
{
14
    protected $name = '\PhpParser\Node\Stmt\Switch_';
15
16
    /**
17
     * @param \PhpParser\Node\Stmt\Switch_ $stmt
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21 1
    public function compile($stmt, Context $context)
22
    {
23 1
        $context->getExpressionCompiler()->compile($stmt->cond);
24
25 1
        if (count($stmt->cases)) {
26 1
            foreach ($stmt->cases as $case) {
27 1
                if ($case->cond) {
28 1
                    $context->getExpressionCompiler()->compile($case->cond);
29 1
                }
30
31 1
                if (count($case->stmts) > 0) {
32 1
                    $beforeStatement = false;
33
34 1
                    foreach ($case->stmts as $caseStatements) {
35 1
                        \PHPSA\nodeVisitorFactory($caseStatements, $context);
36
37 1
                        if ($beforeStatement) {
38
                            if ($beforeStatement instanceof \PhpParser\Node\Stmt\Return_
39 1
                                && $caseStatements instanceof \PhpParser\Node\Stmt\Break_) {
40
                                $context->notice(
41
                                    'switch.unneeded-break',
42
                                    'Break after return statement is not needed',
43
                                    $caseStatements
44
                                );
45
                            }
46 1
                        }
47 1
                        $beforeStatement = $caseStatements;
48 1
                    }
49 1
                }
50 1
            }
51 1
        } else {
52
            $context->notice('switch.empty', 'Switch block is empty, lol', $stmt);
53
        }
54 1
    }
55
}
56