Completed
Pull Request — master (#235)
by Kévin
04:08 queued 20s
created

Assign::compile()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 13.0501

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 37
nc 14
nop 2
dl 0
loc 64
ccs 42
cts 45
cp 0.9333
crap 13.0501
rs 6.0391
c 1
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 PhpParser\NodeAbstract;
6
use PHPSA\CompiledExpression;
7
use PHPSA\Context;
8
use PhpParser\Node;
9
use PHPSA\Definition\ClassDefinition;
10
11
class Assign extends AbstractExpressionCompiler
12
{
13
    protected $name = 'PhpParser\Node\Expr\Assign';
14
15
    /**
16
     * $a = 3;
17
     *
18
     * @param \PhpParser\Node\Expr\Assign $expr
19
     * @param Context $context
20
     * @return CompiledExpression
21
     */
22 36
    protected function compile($expr, Context $context)
0 ignored issues
show
Complexity introduced by
This operation has 216 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...
23
    {
24 36
        $compiler = $context->getExpressionCompiler();
25
26 36
        $compiledExpression = $compiler->compile($expr->expr);
27
28 36
        if ($expr->var instanceof Node\Expr\List_) {
29 1
            $isCorrectType = $compiledExpression->isArray();
30
31 1
            foreach ($expr->var->vars as $key => $var) {
32 1
                if (!$var instanceof Node\Expr\Variable) {
33 1
                    continue;
34
                }
35
36 1
                if ($var->name instanceof Node\Expr\Variable) {
37 1
                    $this->compileVariableDeclaration($var, $var->name, new CompiledExpression(), $context);
38 1
                    continue;
39
                }
40
41 1
                $symbol = $context->getSymbol($var->name);
42 1
                if (!$symbol) {
43 1
                    $symbol = new \PhpSA\Variable(
44 1
                        $var->name,
0 ignored issues
show
Bug introduced by
It seems like $var->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...
45 1
                        null,
46 1
                        CompiledExpression::UNKNOWN,
47 1
                        $expr,
48 1
                        $context->getCurrentBranch()
49 1
                    );
50 1
                    $context->addVariable($symbol);
51 1
                }
52
53 1
                if (!$isCorrectType) {
54
                    $symbol->modify(CompiledExpression::NULL, null);
55
                }
56
57 1
                $symbol->incSets();
58 1
            }
59
60 1
            return new CompiledExpression();
61
        }
62
63 36
        if ($expr->var instanceof Node\Expr\Variable) {
64 36
            $this->compileVariableDeclaration($expr->var, $expr->var->name, $compiledExpression, $context);
65
66 36
            return $compiledExpression;
67
        }
68
69 2
        if ($expr->var instanceof Node\Expr\PropertyFetch) {
70 2
            $compiledExpression = $compiler->compile($expr->var->var);
71 2
            if ($compiledExpression->getType() == CompiledExpression::OBJECT) {
72 2
                $objectDefinition = $compiledExpression->getValue();
73 2
                if ($objectDefinition instanceof ClassDefinition) {
74 2
                    if (is_string($expr->var->name)) {
75 2
                        if ($objectDefinition->hasProperty($expr->var->name)) {
76
                            return $compiler->compile($objectDefinition->getProperty($expr->var->name));
77
                        }
78 2
                    }
79 2
                }
80 2
            }
81 2
        }
82
83 2
        $context->debug('Unknown how to pass symbol', $expr);
84 2
        return new CompiledExpression();
85
    }
86
87
88 36
    protected function compileVariableDeclaration(NodeAbstract $varExpression, $varNameExpression, CompiledExpression $value, Context $context)
89
    {
90 36
        $variableName = $context->getExpressionCompiler()->compile($varNameExpression);
91
92 36
        if (!$variableName->isString()) {
93
            $context->debug('Unexpected type of Variable name after compile');
94
            return new CompiledExpression();
95
        }
96
97 36
        $symbol = $context->getSymbol($variableName->getValue());
98 36
        if ($symbol) {
99 4
            $symbol->modify($value->getType(), $value->getValue());
100 4
            $context->modifyReferencedVariables(
101 4
                $symbol,
102 4
                $value->getType(),
103 4
                $value->getValue()
104 4
            );
105 4
        } else {
106 36
            $symbol = new \PhpSA\Variable(
107 36
                $variableName->getValue(),
108 36
                $value->getValue(),
109 36
                $value->getType(),
110 36
                $varExpression,
111 36
                $context->getCurrentBranch()
112 36
            );
113 36
            $context->addVariable($symbol);
114
        }
115
116 36
        $symbol->incSets();
117 36
    }
118
}
119