Completed
Push — master ( 40ee83...aecd2c )
by Дмитрий
07:12 queued 02:11
created

IfSt::compile()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 46
ccs 0
cts 34
cp 0
rs 4.9831
cc 10
eloc 27
nc 12
nop 2
crap 110

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