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

Statement::__construct()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 3
nop 2
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 2
rs 9.4285
c 0
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 21
    protected function factory($stmt)
22
    {
23 21
        switch (get_class($stmt)) {
24 21
            case Stmt\Break_::class:
25 1
                return new Statement\BreakSt();
26 21
            case Stmt\Continue_::class:
27
                return new Statement\ContinueSt();
28 21
            case Stmt\Echo_::class:
29
                return new Statement\EchoSt();
30 21
            case Stmt\Return_::class:
31 20
                return new Statement\ReturnSt();
32 3
            case Stmt\While_::class:
33
                return new Statement\WhileSt();
34 3
            case Stmt\Switch_::class:
35 1
                return new Statement\SwitchSt();
36 2
            case Stmt\If_::class:
37
                return new Statement\IfSt();
38 2
            case Stmt\Do_::class:
39
                return new Statement\DoSt();
40 2
            case Stmt\For_::class:
41
                return new Statement\ForSt();
42 2
            case Stmt\Foreach_::class:
43 1
                return new Statement\ForeachSt();
44 2
            case Stmt\TryCatch::class:
45 1
                return new Statement\TryCatchSt();
46 2
            case Stmt\Catch_::class:
47 1
                return new Statement\CatchSt();
48 2
            case Stmt\Throw_::class:
49 1
                return new Statement\ThrowSt();
50 2
        }
51
52 2
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
53
    }
54
55
    /**
56
     * @param Node\Stmt $stmt
57
     * @param Context $context
58
     */
59 21
    public function __construct(Node\Stmt $stmt, Context $context)
60
    {
61
        try {
62 21
            $context->getEventManager()->fire(
63 21
                Event\StatementBeforeCompile::EVENT_NAME,
64 21
                new Event\StatementBeforeCompile(
65 21
                    $stmt,
66
                    $context
67 21
                )
68 21
            );
69
            
70 21
            $compiler = $this->factory($stmt);
71 21
        } catch (\Exception $e) {
72 2
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
73 2
            return;
74
        }
75
76 20
        $compiler->pass($stmt, $context);
77 20
    }
78
}
79