Completed
Pull Request — master (#85)
by Kévin
18:16 queued 14:06
created

Statement::factory()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.5605

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 23
c 3
b 0
f 0
nc 11
nop 1
dl 0
loc 27
ccs 20
cts 24
cp 0.8333
crap 11.5605
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 11
    protected function factory($stmt)
21
    {
22 11
        switch (get_class($stmt)) {
23 11
            case Stmt\Return_::class:
24 11
                return new Statement\ReturnSt();
25 2
            case Stmt\While_::class:
26
                return new Statement\WhileSt();
27 2
            case Stmt\Switch_::class:
28 1
                return new Statement\SwitchSt();
29 1
            case Stmt\If_::class:
30
                return new Statement\IfSt();
31 1
            case Stmt\Do_::class:
32
                return new Statement\DoSt();
33 1
            case Stmt\For_::class:
34
                return new Statement\ForSt();
35 1
            case Stmt\Foreach_::class:
36 1
                return new Statement\ForeachSt();
37 1
            case Stmt\TryCatch::class:
38 1
                return new Statement\TryCatchSt();
39 1
            case Stmt\Catch_::class:
40 1
                return new Statement\CatchSt();
41 1
            case Stmt\Throw_::class:
42 1
                return new Statement\ThrowSt();
43 1
        }
44
45 1
        throw new RuntimeException('Unknown statement: ' . get_class($stmt));
46
    }
47
48
    /**
49
     * @param Node\Stmt $stmt
50
     * @param Context $context
51
     */
52 11
    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 11
            if ($stmt instanceof Stmt\Break_) {
59 1
                return;
60
            }
61
62 11
            $context->getEventManager()->fire(
63 11
                Event\StatementBeforeCompile::EVENT_NAME,
64 11
                new Event\StatementBeforeCompile(
65 11
                    $stmt,
66
                    $context
67 11
                )
68 11
            );
69
            
70 11
            $compiler = $this->factory($stmt);
71 11
        } catch (\Exception $e) {
72 1
            $context->debug('StatementCompiler is not implemented for ' . get_class($stmt));
73 1
            return;
74
        }
75
76 11
        $compiler->pass($stmt, $context);
77 11
    }
78
}
79