Assign::compile()   C
last analyzed

Complexity

Conditions 14
Paths 15

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
nc 15
nop 2
dl 0
loc 67
ccs 0
cts 38
cp 0
crap 210
rs 5.7333
c 0
b 0
f 0

How to fix   Long Method    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
namespace PHPSA\Compiler\Expression;
4
5
use PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PhpParser\Node;
8
use PHPSA\Definition\ClassDefinition;
9
10
class Assign extends AbstractExpressionCompiler
11
{
12
    protected $name = 'PhpParser\Node\Expr\Assign';
13
14
    /**
15
     * $a = 3;
16
     *
17
     * @param \PhpParser\Node\Expr\Assign $expr
18
     * @param Context $context
19
     * @return CompiledExpression
20
     */
21
    protected function compile($expr, Context $context)
0 ignored issues
show
Complexity introduced by
This operation has 408 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
22
    {
23
        $compiler = $context->getExpressionCompiler();
24
25
        $compiledExpression = $compiler->compile($expr->expr);
26
27
        if ($expr->var instanceof Node\Expr\List_) {
28
            $isCorrectType = $compiledExpression->isArray();
29
30
            foreach ($expr->var->items as $var) {
31
                if ($var === null) {
32
                    continue;
33
                }
34
35
                if (!$var->value instanceof Node\Expr\Variable) {
36
                    continue;
37
                }
38
39
                if ($var->value->name instanceof Node\Expr\Variable) {
40
                    $this->compileVariableDeclaration($compiler->compile($var->value->name), new CompiledExpression(), $context);
0 ignored issues
show
Bug introduced by
It seems like $compiler->compile($var->value->name) can be null; however, compileVariableDeclaration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
41
                    continue;
42
                }
43
44
                $symbol = $context->getSymbol($var->value->name);
45
                if (!$symbol) {
46
                    $symbol = new \PHPSA\Variable(
47
                        $var->value->name,
48
                        null,
49
                        CompiledExpression::UNKNOWN,
50
                        $context->getCurrentBranch()
51
                    );
52
                    $context->addVariable($symbol);
53
                }
54
55
                if (!$isCorrectType) {
56
                    $symbol->modify(CompiledExpression::NULL, null);
57
                }
58
59
                $symbol->incSets();
60
            }
61
62
            return new CompiledExpression();
63
        }
64
65
        if ($expr->var instanceof Node\Expr\Variable) {
66
            $this->compileVariableDeclaration($compiler->compile($expr->var->name), $compiledExpression, $context);
0 ignored issues
show
Bug introduced by
It seems like $compiler->compile($expr->var->name) can be null; however, compileVariableDeclaration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $compiledExpression defined by $compiler->compile($expr->expr) on line 25 can be null; however, PHPSA\Compiler\Expressio...leVariableDeclaration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
67
68
            return $compiledExpression;
69
        }
70
71
        if ($expr->var instanceof Node\Expr\PropertyFetch) {
72
            $compiledExpression = $compiler->compile($expr->var->var);
73
            if ($compiledExpression->getType() == CompiledExpression::OBJECT) {
74
                $objectDefinition = $compiledExpression->getValue();
75
                if ($objectDefinition instanceof ClassDefinition) {
76
                    if (is_string($expr->var->name)) {
77
                        if ($objectDefinition->hasProperty($expr->var->name)) {
78
                            return $compiler->compile($objectDefinition->getProperty($expr->var->name));
0 ignored issues
show
Bug introduced by
It seems like $objectDefinition->getProperty($expr->var->name) can be null; however, compile() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
79
                        }
80
                    }
81
                }
82
            }
83
        }
84
85
        $context->debug('Unknown how to pass symbol', $expr);
86
        return new CompiledExpression();
87
    }
88
89
90
    protected function compileVariableDeclaration(CompiledExpression $variableName, CompiledExpression $value, Context $context)
91
    {
92
        switch ($variableName->getType()) {
93
            case CompiledExpression::STRING:
94
                break;
95
            default:
96
                $context->debug('Unexpected type of Variable name after compile');
97
                return new CompiledExpression();
98
        }
99
100
        $symbol = $context->getSymbol($variableName->getValue());
101
        if ($symbol) {
102
            $symbol->modify($value->getType(), $value->getValue());
103
            $context->modifyReferencedVariables(
104
                $symbol,
105
                $value->getType(),
106
                $value->getValue()
107
            );
108
        } else {
109
            $symbol = new \PHPSA\Variable(
110
                $variableName->getValue(),
111
                $value->getValue(),
112
                $value->getType(),
113
                $context->getCurrentBranch()
114
            );
115
            $context->addVariable($symbol);
116
        }
117
118
        $symbol->incSets();
119
    }
120
}
121