Completed
Push — master ( cacb89...b59b39 )
by Дмитрий
9s
created

Statement::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0213

Importance

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