Completed
Pull Request — master (#296)
by algo13
03:47
created

Statement::factory()   C

Complexity

Conditions 23
Paths 23

Size

Total Lines 51
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 23.1291

Importance

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