Statement::factory()   D
last analyzed

Complexity

Conditions 23
Paths 23

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 552

Importance

Changes 0
Metric Value
cc 23
nc 23
nop 1
dl 0
loc 51
ccs 0
cts 47
cp 0
crap 552
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    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
    protected function factory($stmt)
22
    {
23
        switch (get_class($stmt)) {
24
            case Stmt\Break_::class:
25
                return new Statement\BreakSt();
26
            case Stmt\Case_::class:
27
                return new Statement\CaseSt();
28
            case Stmt\Continue_::class:
29
                return new Statement\ContinueSt();
30
            case Stmt\Echo_::class:
31
                return new Statement\EchoSt();
32
            case Stmt\Return_::class:
33
                return new Statement\ReturnSt();
34
            case Stmt\While_::class:
35
                return new Statement\WhileSt();
36
            case Stmt\Switch_::class:
37
                return new Statement\SwitchSt();
38
            case Stmt\If_::class:
39
                return new Statement\IfSt();
40
            case Stmt\ElseIf_::class:
41
                return new Statement\ElseIfSt();
42
            case Stmt\Else_::class:
43
                return new Statement\ElseSt();
44
            case Stmt\Do_::class:
45
                return new Statement\DoSt();
46
            case Stmt\For_::class:
47
                return new Statement\ForSt();
48
            case Stmt\Foreach_::class:
49
                return new Statement\ForeachSt();
50
            case Stmt\TryCatch::class:
51
                return new Statement\TryCatchSt();
52
            case Stmt\Catch_::class:
53
                return new Statement\CatchSt();
54
            case Stmt\Finally_::class:
55
                return new Statement\FinallySt();
56
            case Stmt\Throw_::class:
57
                return new Statement\ThrowSt();
58
            case Stmt\Global_::class:
59
                return new Statement\GlobalSt();
60
            case Stmt\Static_::class:
61
                return new Statement\StaticSt();
62
            case Stmt\Declare_::class:
63
                return new Statement\DeclareSt();
64
            case Stmt\Const_::class:
65
                return new Statement\ConstSt();
66
            case Stmt\Unset_::class:
67
                return new Statement\UnsetSt();
68
        }
69
70
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
71
    }
72
73
    /**
74
     * @param Node\Stmt $stmt
75
     * @param Context $context
76
     */
77
    public function __construct(Node\Stmt $stmt, Context $context)
78
    {
79
        try {
80
            $context->getEventManager()->fire(
81
                Event\StatementBeforeCompile::EVENT_NAME,
82
                new Event\StatementBeforeCompile(
83
                    $stmt,
84
                    $context
85
                )
86
            );
87
            
88
            if ($stmt instanceof Stmt\Goto_ || $stmt instanceof Stmt\Label || $stmt instanceof Stmt\InlineHTML || $stmt instanceof Stmt\Nop) {
89
                return;
90
            }
91
92
            $compiler = $this->factory($stmt);
93
        } catch (\Exception $e) {
94
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
95
            return;
96
        }
97
98
        $compiler->pass($stmt, $context);
99
    }
100
}
101