Completed
Push — master ( 8bad52...c13fe7 )
by Дмитрий
03:41
created

Assign::compile()   C

Complexity

Conditions 14
Paths 15

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 14.129

Importance

Changes 0
Metric Value
cc 14
eloc 38
nc 15
nop 2
dl 0
loc 66
ccs 42
cts 46
cp 0.913
crap 14.129
rs 5.8815
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 43
    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 43
        $compiler = $context->getExpressionCompiler();
24
25 43
        $compiledExpression = $compiler->compile($expr->expr);
26
27 43
        if ($expr->var instanceof Node\Expr\List_) {
28 1
            $isCorrectType = $compiledExpression->isArray();
29
30 1
            foreach ($expr->var->items as $var) {
31 1
                if ($var === null) {
32 1
                    continue;
33
                }
34 1
                if (!$var->value instanceof Node\Expr\Variable) {
35
                    continue;
36
                }
37
38 1
                if ($var->value->name instanceof Node\Expr\Variable) {
39 1
                    $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...
40 1
                    continue;
41
                }
42
43 1
                $symbol = $context->getSymbol($var->value->name);
44 1
                if (!$symbol) {
45 1
                    $symbol = new \PHPSA\Variable(
46 1
                        $var->value->name,
0 ignored issues
show
Bug introduced by
It seems like $var->value->name can also be of type object<PhpParser\Node\Expr>; however, PHPSA\Variable::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47 1
                        null,
48 1
                        CompiledExpression::UNKNOWN,
49 1
                        $context->getCurrentBranch()
50 1
                    );
51 1
                    $context->addVariable($symbol);
52 1
                }
53
54 1
                if (!$isCorrectType) {
55
                    $symbol->modify(CompiledExpression::NULL, null);
56
                }
57
58 1
                $symbol->incSets();
59 1
            }
60
61 1
            return new CompiledExpression();
62
        }
63
64 43
        if ($expr->var instanceof Node\Expr\Variable) {
65 43
            $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...
66
67 43
            return $compiledExpression;
68
        }
69
70 2
        if ($expr->var instanceof Node\Expr\PropertyFetch) {
71 2
            $compiledExpression = $compiler->compile($expr->var->var);
72 2
            if ($compiledExpression->getType() == CompiledExpression::OBJECT) {
73 2
                $objectDefinition = $compiledExpression->getValue();
74 2
                if ($objectDefinition instanceof ClassDefinition) {
75 2
                    if (is_string($expr->var->name)) {
76 2
                        if ($objectDefinition->hasProperty($expr->var->name)) {
77
                            return $compiler->compile($objectDefinition->getProperty($expr->var->name));
78
                        }
79 2
                    }
80 2
                }
81 2
            }
82 2
        }
83
84 2
        $context->debug('Unknown how to pass symbol', $expr);
85 2
        return new CompiledExpression();
86
    }
87
88
89 43
    protected function compileVariableDeclaration(CompiledExpression $variableName, CompiledExpression $value, Context $context)
90
    {
91 43
        switch ($variableName->getType()) {
92 43
            case CompiledExpression::STRING:
93 43
                break;
94
            default:
95
                $context->debug('Unexpected type of Variable name after compile');
96
                return new CompiledExpression();
97 43
        }
98
99 43
        $symbol = $context->getSymbol($variableName->getValue());
100 43
        if ($symbol) {
101 5
            $symbol->modify($value->getType(), $value->getValue());
102 5
            $context->modifyReferencedVariables(
103 5
                $symbol,
104 5
                $value->getType(),
105 5
                $value->getValue()
106 5
            );
107 5
        } else {
108 42
            $symbol = new \PHPSA\Variable(
109 42
                $variableName->getValue(),
110 42
                $value->getValue(),
111 42
                $value->getType(),
112 42
                $context->getCurrentBranch()
113 42
            );
114 42
            $context->addVariable($symbol);
115
        }
116
117 43
        $symbol->incSets();
118 43
    }
119
}
120