Completed
Push — master ( f754fe...9d04b6 )
by Дмитрий
11:59
created

Assign::compile()   C

Complexity

Conditions 14
Paths 15

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 15.0307

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 38
nc 15
nop 2
dl 0
loc 66
ccs 38
cts 46
cp 0.8261
crap 15.0307
rs 5.8815
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 42
    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...
23
    {
24 42
        $compiler = $context->getExpressionCompiler();
25
26 42
        $compiledExpression = $compiler->compile($expr->expr);
27
28 42
        if ($expr->var instanceof Node\Expr\List_) {
29 1
            $isCorrectType = $compiledExpression->isArray();
30
31 1
            foreach ($expr->var->items as $var) {
32 1
                if ($var === null) {
33 1
                    continue;
34
                }
35 1
                if (!$var->value instanceof Node\Expr\Variable) {
36
                    continue;
37
                }
38
39 1
                if ($var->value->name instanceof Node\Expr\Variable) {
40 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...
41 1
                    continue;
42
                }
43
44 1
                $symbol = $context->getSymbol($var->value->name);
45 1
                if (!$symbol) {
46 1
                    $symbol = new \PHPSA\Variable(
47 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...
48 1
                        null,
49 1
                        CompiledExpression::UNKNOWN,
50 1
                        $context->getCurrentBranch()
51 1
                    );
52 1
                    $context->addVariable($symbol);
53 1
                }
54
55 1
                if (!$isCorrectType) {
56
                    $symbol->modify(CompiledExpression::NULL, null);
57
                }
58
59 1
                $symbol->incSets();
60 1
            }
61
62 1
            return new CompiledExpression();
63
        }
64
65 42
        if ($expr->var instanceof Node\Expr\Variable) {
66 42
            $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 26 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 42
            return $compiledExpression;
69
        }
70
71 2
        if ($expr->var instanceof Node\Expr\PropertyFetch) {
72 2
            $compiledExpression = $compiler->compile($expr->var->var);
73 2
            if ($compiledExpression->getType() == CompiledExpression::OBJECT) {
74 2
                $objectDefinition = $compiledExpression->getValue();
75 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...
76
                    if (is_string($expr->var->name)) {
77
                        if ($objectDefinition->hasProperty($expr->var->name)) {
78
                            return $compiler->compile($objectDefinition->getProperty($expr->var->name));
79
                        }
80
                    }
81
                }
82 2
            }
83 2
        }
84
85 2
        $context->debug('Unknown how to pass symbol', $expr);
86 2
        return new CompiledExpression();
87
    }
88
89
90 42
    protected function compileVariableDeclaration(CompiledExpression $variableName, CompiledExpression $value, Context $context)
91
    {
92 42
        switch ($variableName->getType()) {
93 42
            case CompiledExpression::STRING:
94 42
                break;
95
            default:
96
                $context->debug('Unexpected type of Variable name after compile');
97
                return new CompiledExpression();
98 42
        }
99
100 42
        $symbol = $context->getSymbol($variableName->getValue());
101 42
        if ($symbol) {
102 5
            $symbol->modify($value->getType(), $value->getValue());
103 5
            $context->modifyReferencedVariables(
104 5
                $symbol,
105 5
                $value->getType(),
106 5
                $value->getValue()
107 5
            );
108 5
        } else {
109 41
            $symbol = new \PHPSA\Variable(
110 41
                $variableName->getValue(),
111 41
                $value->getValue(),
112 41
                $value->getType(),
113 41
                $context->getCurrentBranch()
114 41
            );
115 41
            $context->addVariable($symbol);
116
        }
117
118 42
        $symbol->incSets();
119 42
    }
120
}
121