Completed
Pull Request — master (#149)
by Enrico
04:04
created

Statement::factory()   D

Complexity

Conditions 19
Paths 19

Size

Total Lines 43
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 19.7049

Importance

Changes 0
Metric Value
cc 19
eloc 39
nc 19
nop 1
dl 0
loc 43
ccs 35
cts 40
cp 0.875
crap 19.7049
rs 4.9141
c 0
b 0
f 0

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