Completed
Push — master ( 124462...3bbcdb )
by Дмитрий
02:56
created

Statement::factory()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 81.0147

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
c 4
b 0
f 0
nc 11
nop 1
dl 0
loc 27
ccs 4
cts 24
cp 0.1666
crap 81.0147
rs 5.2653

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 8
    protected function factory($stmt)
21
    {
22 8
        switch (get_class($stmt)) {
23 8
            case Stmt\Return_::class:
24 8
                return new Statement\ReturnSt();
25
            case Stmt\While_::class:
26
                return new Statement\WhileSt();
27
            case Stmt\Switch_::class:
28
                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 8
    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 8
            if ($stmt instanceof Stmt\Break_) {
59
                return;
60
            }
61
62 8
            $context->getEventManager()->fire(
63 8
                Event\StatementBeforeCompile::EVENT_NAME,
64 8
                new Event\StatementBeforeCompile(
65 8
                    $stmt,
66
                    $context
67 8
                )
68 8
            );
69
            
70 8
            $compiler = $this->factory($stmt);
71 8
        } catch (\Exception $e) {
72
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
73
            return;
74
        }
75
76 8
        $compiler->pass($stmt, $context);
77 8
    }
78
}
79