Completed
Pull Request — master (#149)
by Enrico
04:21
created

Statement   B

Complexity

Total Complexity 19

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 16

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 69
ccs 39
cts 45
cp 0.8667
rs 8.4614
wmc 19
lcom 0
cbo 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
C factory() 0 33 14
B __construct() 0 23 5
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 20
    protected function factory($stmt)
22
    {
23 20
        switch (get_class($stmt)) {
24 20
            case Stmt\Break_::class:
25 1
                return new Statement\BreakSt();
26 20
            case Stmt\Continue_::class:
27
                return new Statement\ContinueSt();
28 20
            case Stmt\Echo_::class:
29
                return new Statement\EchoSt();
30 20
            case Stmt\Return_::class:
31 20
                return new Statement\ReturnSt();
32 2
            case Stmt\While_::class:
33
                return new Statement\WhileSt();
34 2
            case Stmt\Switch_::class:
35 1
                return new Statement\SwitchSt();
36 1
            case Stmt\If_::class:
37
                return new Statement\IfSt();
38 1
            case Stmt\Do_::class:
39
                return new Statement\DoSt();
40 1
            case Stmt\For_::class:
41
                return new Statement\ForSt();
42 1
            case Stmt\Foreach_::class:
43 1
                return new Statement\ForeachSt();
44 1
            case Stmt\TryCatch::class:
45 1
                return new Statement\TryCatchSt();
46 1
            case Stmt\Catch_::class:
47 1
                return new Statement\CatchSt();
48 1
            case Stmt\Throw_::class:
49 1
                return new Statement\ThrowSt();
50 1
        }
51
52 1
        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
            if ($stmt instanceof Stmt\Goto_ || $stmt instanceof Stmt\Label || $stmt instanceof Stmt\InlineHTML) {
71 1
                return;
72
            }
73
            
74 20
            $compiler = $this->factory($stmt);
75 20
        } catch (\Exception $e) {
76 1
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
77 1
            return;
78
        }
79
80 20
        $compiler->pass($stmt, $context);
81 20
    }
82
}
83