Completed
Pull Request — master (#194)
by Enrico
07:43
created

Statement::factory()   C

Complexity

Conditions 22
Paths 22

Size

Total Lines 49
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 22.6216

Importance

Changes 0
Metric Value
cc 22
eloc 45
c 0
b 0
f 0
nc 22
nop 1
dl 0
loc 49
ccs 41
cts 46
cp 0.8913
crap 22.6216
rs 5.0781

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler;
7
8
use PHPSA\Context;
9
use PhpParser\Node;
10
use PHPSA\Compiler\Statement\AbstractCompiler;
11
use RuntimeException;
12
use PhpParser\Node\Stmt;
13
14
class Statement
15
{
16
    /**
17
     * @param Node\Stmt $stmt
18
     * @throws RuntimeException
19
     * @return AbstractCompiler
20
     */
21 42
    protected function factory($stmt)
22
    {
23 42
        switch (get_class($stmt)) {
24 42
            case Stmt\Break_::class:
25 2
                return new Statement\BreakSt();
26 42
            case Stmt\Case_::class:
27 3
                return new Statement\CaseSt();
28 42
            case Stmt\Continue_::class:
29
                return new Statement\ContinueSt();
30 42
            case Stmt\Echo_::class:
31 3
                return new Statement\EchoSt();
32 40
            case Stmt\Return_::class:
33 28
                return new Statement\ReturnSt();
34 20
            case Stmt\While_::class:
35 4
                return new Statement\WhileSt();
36 18
            case Stmt\Switch_::class:
37 3
                return new Statement\SwitchSt();
38 17
            case Stmt\If_::class:
39 8
                return new Statement\IfSt();
40 13
            case Stmt\ElseIf_::class:
41 3
                return new Statement\ElseIfSt();
42 12
            case Stmt\Else_::class:
43 2
                return new Statement\ElseSt();
44 11
            case Stmt\Do_::class:
45 4
                return new Statement\DoSt();
46 9
            case Stmt\For_::class:
47 6
                return new Statement\ForSt();
48 3
            case Stmt\Foreach_::class:
49 1
                return new Statement\ForeachSt();
50 3
            case Stmt\TryCatch::class:
51 1
                return new Statement\TryCatchSt();
52 3
            case Stmt\Catch_::class:
53 1
                return new Statement\CatchSt();
54 3
            case Stmt\Throw_::class:
55 1
                return new Statement\ThrowSt();
56 3
            case Stmt\Global_::class:
57 2
                return new Statement\GlobalSt();
58 2
            case Stmt\Static_::class:
59 2
                return new Statement\StaticSt();
60 1
            case Stmt\Declare_::class:
61
                return new Statement\DeclareSt();
62 1
            case Stmt\Const_::class:
63
                return new Statement\ConstSt();
64 1
            case Stmt\Unset_::class:
65 1
                return new Statement\UnsetSt();
66
        }
67
68
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
69
    }
70
71
    /**
72
     * @param Node\Stmt $stmt
73
     * @param Context $context
74
     */
75 43
    public function __construct(Node\Stmt $stmt, Context $context)
76
    {
77
        try {
78 43
            $context->getEventManager()->fire(
79 43
                Event\StatementBeforeCompile::EVENT_NAME,
80 43
                new Event\StatementBeforeCompile(
81 43
                    $stmt,
82
                    $context
83 43
                )
84 43
            );
85
            
86 43
            if ($stmt instanceof Stmt\Goto_ || $stmt instanceof Stmt\Label || $stmt instanceof Stmt\InlineHTML || $stmt instanceof Stmt\Nop) {
87 2
                return;
88
            }
89
90 42
            $compiler = $this->factory($stmt);
91 42
        } catch (\Exception $e) {
92
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
93
            return;
94
        }
95
96 42
        $compiler->pass($stmt, $context);
97 42
    }
98
}
99