Completed
Push — master ( f188aa...a2fcca )
by Дмитрий
41:46
created

Statement::factory()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 23
ccs 0
cts 20
cp 0
rs 5.8541
cc 9
eloc 19
nc 9
nop 1
crap 90
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
13
class Statement
14
{
15
    /**
16
     * @param Node\Stmt $stmt
17
     * @return AbstractCompiler
18
     */
19
    protected function factory($stmt)
20
    {
21
        switch (get_class($stmt)) {
22
            case 'PhpParser\Node\Stmt\Return_':
23
                return new Statement\ReturnSt();
24
            case 'PhpParser\Node\Stmt\While_':
25
                return new Statement\WhileSt();
26
            case 'PhpParser\Node\Stmt\Switch_':
27
                return new Statement\SwitchSt();
28
            case 'PhpParser\Node\Stmt\If_':
29
                return new Statement\IfSt();
30
            case 'PhpParser\Node\Stmt\Do_':
31
                return new Statement\DoSt();
32
            case 'PhpParser\Node\Stmt\For_':
33
                return new Statement\ForSt();
34
            case 'PhpParser\Node\Stmt\Foreach_':
35
                return new Statement\ForeachSt();
36
            case 'PhpParser\Node\Stmt\TryCatch':
37
                return new Statement\TryCatchSt();
38
        }
39
40
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
41
    }
42
43
    /**
44
     * @param Node\Stmt $stmt
45
     * @param Context $context
46
     */
47
    public function __construct(Node\Stmt $stmt, Context $context)
48
    {
49
        try {
50
            /**
51
             * Dont show an error
52
             * @todo This a little bit more about own statement for break;
53
             */
54
            if (get_class($stmt) == 'PhpParser\Node\Stmt\Break_') {
55
                return;
56
            }
57
58
            $compiler = $this->factory($stmt);
59
        } catch (\Exception $e) {
60
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
61
            return;
62
        }
63
64
        $compiler->pass($stmt, $context);
65
    }
66
}
67