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

Statement::factory()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13.0239

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 25
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 29
ccs 21
cts 26
cp 0.8077
crap 13.0239
rs 5.1612

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
     * @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