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

Statement::__construct()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0852

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
nc 4
nop 2
dl 0
loc 23
ccs 13
cts 15
cp 0.8667
crap 6.0852
rs 8.5906
c 2
b 0
f 0
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 36
    protected function factory($stmt)
22
    {
23 36
        switch (get_class($stmt)) {
24 36
            case Stmt\Break_::class:
25 2
                return new Statement\BreakSt();
26 36
            case Stmt\Case_::class:
27 2
                return new Statement\CaseSt();
28 36
            case Stmt\Continue_::class:
29
                return new Statement\ContinueSt();
30 36
            case Stmt\Echo_::class:
31 3
                return new Statement\EchoSt();
32 34
            case Stmt\Return_::class:
33 22
                return new Statement\ReturnSt();
34 15
            case Stmt\While_::class:
35 3
                return new Statement\WhileSt();
36 13
            case Stmt\Switch_::class:
37 2
                return new Statement\SwitchSt();
38 12
            case Stmt\If_::class:
39 5
                return new Statement\IfSt();
40 10
            case Stmt\ElseIf_::class:
41 2
                return new Statement\ElseIfSt();
42 9
            case Stmt\Else_::class:
43 1
                return new Statement\ElseSt();
44 8
            case Stmt\Do_::class:
45 3
                return new Statement\DoSt();
46 6
            case Stmt\For_::class:
47 5
                return new Statement\ForSt();
48 1
            case Stmt\Foreach_::class:
49 1
                return new Statement\ForeachSt();
50 1
            case Stmt\TryCatch::class:
51 1
                return new Statement\TryCatchSt();
52 1
            case Stmt\Catch_::class:
53 1
                return new Statement\CatchSt();
54 1
            case Stmt\Throw_::class:
55 1
                return new Statement\ThrowSt();
56 1
            case Stmt\Global_::class:
57 1
                return new Statement\GlobalSt();
58 1
            case Stmt\Static_::class:
59 1
                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 37
    public function __construct(Node\Stmt $stmt, Context $context)
76
    {
77
        try {
78 37
            $context->getEventManager()->fire(
79 37
                Event\StatementBeforeCompile::EVENT_NAME,
80 37
                new Event\StatementBeforeCompile(
81 37
                    $stmt,
82
                    $context
83 37
                )
84 37
            );
85
            
86 37
            if ($stmt instanceof Stmt\Goto_ || $stmt instanceof Stmt\Label || $stmt instanceof Stmt\InlineHTML || $stmt instanceof Stmt\Nop) {
87 2
                return;
88
            }
89
90 36
            $compiler = $this->factory($stmt);
91 36
        } catch (\Exception $e) {
92
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
93
            return;
94
        }
95
96 36
        $compiler->pass($stmt, $context);
97 36
    }
98
}
99