Completed
Push — master ( 6fddd2...19c440 )
by Дмитрий
09:54 queued 01:23
created

Statement   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 14

Test Coverage

Coverage 87.8%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 68
ccs 36
cts 41
cp 0.878
rs 10
wmc 15
lcom 0
cbo 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
C factory() 0 29 12
B __construct() 0 26 3
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 13
    protected function factory($stmt)
22
    {
23 13
        switch (get_class($stmt)) {
24 13
            case Stmt\Echo_::class:
25
                return new Statement\EchoSt();
26 13
            case Stmt\Return_::class:
27 13
                return new Statement\ReturnSt();
28 2
            case Stmt\While_::class:
29
                return new Statement\WhileSt();
30 2
            case Stmt\Switch_::class:
31 1
                return new Statement\SwitchSt();
32 1
            case Stmt\If_::class:
33
                return new Statement\IfSt();
34 1
            case Stmt\Do_::class:
35
                return new Statement\DoSt();
36 1
            case Stmt\For_::class:
37
                return new Statement\ForSt();
38 1
            case Stmt\Foreach_::class:
39 1
                return new Statement\ForeachSt();
40 1
            case Stmt\TryCatch::class:
41 1
                return new Statement\TryCatchSt();
42 1
            case Stmt\Catch_::class:
43 1
                return new Statement\CatchSt();
44 1
            case Stmt\Throw_::class:
45 1
                return new Statement\ThrowSt();
46 1
        }
47
48 1
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
49
    }
50
51
    /**
52
     * @param Node\Stmt $stmt
53
     * @param Context $context
54
     */
55 13
    public function __construct(Node\Stmt $stmt, Context $context)
56
    {
57
        try {
58
            /**
59
             * @todo Think a little bit more about own statement for break;
60
             */
61 13
            if ($stmt instanceof Stmt\Break_) {
62 1
                return;
63
            }
64
65 13
            $context->getEventManager()->fire(
66 13
                Event\StatementBeforeCompile::EVENT_NAME,
67 13
                new Event\StatementBeforeCompile(
68 13
                    $stmt,
69
                    $context
70 13
                )
71 13
            );
72
            
73 13
            $compiler = $this->factory($stmt);
74 13
        } catch (\Exception $e) {
75 1
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
76 1
            return;
77
        }
78
79 13
        $compiler->pass($stmt, $context);
80 13
    }
81
}
82