Completed
Pull Request — master (#196)
by Enrico
05:42
created

Assign::compile()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 63
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 13.6806

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 36
nc 14
nop 2
dl 0
loc 63
ccs 37
cts 44
cp 0.8409
crap 13.6806
rs 6.1128
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 PHPSA\CompiledExpression;
6
use PHPSA\Context;
7
use PHPSA\Compiler\Expression;
8
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
9
use PhpParser\Node;
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 29
    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 29
        $compiler = $context->getExpressionCompiler();
25
26 29
        $compiledExpression = $compiler->compile($expr->expr);
27
28 29
        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($compiler->compile($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
                        $context->getCurrentBranch()
48 1
                    );
49 1
                    $context->addVariable($symbol);
50 1
                }
51
52 1
                if (!$isCorrectType) {
53
                    $symbol->modify(CompiledExpression::NULL, null);
54
                }
55
56 1
                $symbol->incSets();
57 1
            }
58
59 1
            return new CompiledExpression();
60
        }
61
62 29
        if ($expr->var instanceof Node\Expr\Variable) {
63 29
            $this->compileVariableDeclaration($compiler->compile($expr->var->name), $compiledExpression, $context);
64
65 29
            return $compiledExpression;
66
        }
67
68 2
        if ($expr->var instanceof Node\Expr\PropertyFetch) {
69 2
            $compiledExpression = $compiler->compile($expr->var->var);
70 2
            if ($compiledExpression->getType() == CompiledExpression::OBJECT) {
71 2
                $objectDefinition = $compiledExpression->getValue();
72 2
                if ($objectDefinition instanceof ClassDefinition) {
0 ignored issues
show
Bug introduced by
The class PHPSA\Compiler\Expression\ClassDefinition does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
                    if (is_string($expr->var->name)) {
74
                        if ($objectDefinition->hasProperty($expr->var->name)) {
75
                            return $compiler->compile($objectDefinition->getProperty($expr->var->name));
76
                        }
77
                    }
78
                }
79 2
            }
80 2
        }
81
82 2
        $context->debug('Unknown how to pass symbol', $expr);
83 2
        return new CompiledExpression();
84
    }
85
86
87 29
    protected function compileVariableDeclaration(CompiledExpression $variableName, CompiledExpression $value, Context $context)
88
    {
89 29
        switch ($variableName->getType()) {
90 29
            case CompiledExpression::STRING:
91 29
                break;
92
            default:
93
                $context->debug('Unexpected type of Variable name after compile');
94
                return new CompiledExpression();
95 29
        }
96
97 29
        $symbol = $context->getSymbol($variableName->getValue());
98 29
        if ($symbol) {
99 3
            $symbol->modify($value->getType(), $value->getValue());
100 3
            $context->modifyReferencedVariables(
101 3
                $symbol,
102 3
                $value->getType(),
103 3
                $value->getValue()
104 3
            );
105 3
        } else {
106 29
            $symbol = new \PHPSA\Variable(
107 29
                $variableName->getValue(),
108 29
                $value->getValue(),
109 29
                $value->getType(),
110 29
                $context->getCurrentBranch()
111 29
            );
112 29
            $context->addVariable($symbol);
113
        }
114
115 29
        $symbol->incSets();
116 29
    }
117
}
118