Completed
Pull Request — master (#149)
by Enrico
03:38
created

IfSt::compile()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 10.0244

Importance

Changes 0
Metric Value
cc 10
eloc 25
c 0
b 0
f 0
nc 12
nop 2
dl 0
loc 44
ccs 30
cts 32
cp 0.9375
crap 10.0244
rs 4.8196

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\Statement;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
use PHPSA\Variable;
11
12
class IfSt extends AbstractCompiler
13
{
14
    protected $name = '\PhpParser\Node\Stmt\If_';
15
16
    /**
17
     * @param \PhpParser\Node\Stmt\If_ $ifStatement
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21 4
    public function compile($ifStatement, Context $context)
22
    {
23 4
        $context->setCurrentBranch(Variable::BRANCH_CONDITIONAL_TRUE);
24
25 4
        $context->getExpressionCompiler()->compile($ifStatement->cond);
26
27 4
        if (count($ifStatement->stmts) > 0) {
28 1
            foreach ($ifStatement->stmts as $stmt) {
29 1
                \PHPSA\nodeVisitorFactory($stmt, $context);
30 1
            }
31 1
        } else {
32 3
            $context->notice('not-implemented-body', 'Missing body', $ifStatement);
33
        }
34
35 4
        $context->setCurrentBranch(Variable::BRANCH_CONDITIONAL_EXTERNAL);
36
37 4
        if (count($ifStatement->elseifs) > 0) {
38 1
            foreach ($ifStatement->elseifs as $elseIfStatement) {
39 1
                $context->getExpressionCompiler()->compile($elseIfStatement->cond);
40
41 1
                if (count($elseIfStatement->stmts) > 0) {
42 1
                    foreach ($elseIfStatement->stmts as $stmt) {
43 1
                        \PHPSA\nodeVisitorFactory($stmt, $context);
44 1
                    }
45 1
                } else {
46
                    $context->notice('not-implemented-body', 'Missing body', $elseIfStatement);
47
                }
48 1
            }
49 1
        }
50
51 4
        $context->setCurrentBranch(Variable::BRANCH_CONDITIONAL_FALSE);
52
53 4
        if ($ifStatement->else) {
54 1
            if (count($ifStatement->else->stmts) > 0) {
55 1
                foreach ($ifStatement->else->stmts as $stmt) {
56 1
                    \PHPSA\nodeVisitorFactory($stmt, $context);
57 1
                }
58 1
            } else {
59
                $context->notice('not-implemented-body', 'Missing body', $ifStatement->else);
60
            }
61 1
        }
62
63 4
        $context->setCurrentBranch(Variable::BRANCH_ROOT);
64 4
    }
65
}
66