Completed
Pull Request — master (#85)
by Kévin
18:16 queued 14:06
created

Statement::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

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 15
cts 15
cp 1
crap 3
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 11
    protected function factory($stmt)
21
    {
22 11
        switch (get_class($stmt)) {
23 11
            case Stmt\Return_::class:
24 11
                return new Statement\ReturnSt();
25 2
            case Stmt\While_::class:
26
                return new Statement\WhileSt();
27 2
            case Stmt\Switch_::class:
28 1
                return new Statement\SwitchSt();
29 1
            case Stmt\If_::class:
30
                return new Statement\IfSt();
31 1
            case Stmt\Do_::class:
32
                return new Statement\DoSt();
33 1
            case Stmt\For_::class:
34
                return new Statement\ForSt();
35 1
            case Stmt\Foreach_::class:
36 1
                return new Statement\ForeachSt();
37 1
            case Stmt\TryCatch::class:
38 1
                return new Statement\TryCatchSt();
39 1
            case Stmt\Catch_::class:
40 1
                return new Statement\CatchSt();
41 1
            case Stmt\Throw_::class:
42 1
                return new Statement\ThrowSt();
43 1
        }
44
45 1
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
46
    }
47
48
    /**
49
     * @param Node\Stmt $stmt
50
     * @param Context $context
51
     */
52 11
    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 11
            if ($stmt instanceof Stmt\Break_) {
59 1
                return;
60
            }
61
62 11
            $context->getEventManager()->fire(
63 11
                Event\StatementBeforeCompile::EVENT_NAME,
64 11
                new Event\StatementBeforeCompile(
65 11
                    $stmt,
66
                    $context
67 11
                )
68 11
            );
69
            
70 11
            $compiler = $this->factory($stmt);
71 11
        } catch (\Exception $e) {
72 1
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
73 1
            return;
74
        }
75
76 11
        $compiler->pass($stmt, $context);
77 11
    }
78
}
79