Completed
Pull Request — master (#196)
by Enrico
05:34 queued 41s
created

AssignRef::compile()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5.1281

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 27
nc 7
nop 2
dl 0
loc 40
ccs 24
cts 29
cp 0.8276
crap 5.1281
rs 8.439
c 1
b 0
f 0
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\Expr\Variable as VariableNode;
10
11
class AssignRef extends AbstractExpressionCompiler
12
{
13
    protected $name = 'PhpParser\Node\Expr\AssignRef';
14
15
    /**
16
     * $a &= $b;
17
     *
18
     * @param \PhpParser\Node\Expr\AssignRef $expr
19
     * @param Context $context
20
     * @return CompiledExpression
21
     */
22 1
    protected function compile($expr, Context $context)
23
    {
24 1
        $compiler = $context->getExpressionCompiler();
25 1
        if ($expr->var instanceof VariableNode) {
26 1
            $name = $expr->var->name;
27
28 1
            $compiledExpression = $compiler->compile($expr->expr);
29
            
30 1
            $symbol = $context->getSymbol($name);
31 1
            if ($symbol) {
32
                $symbol->modify($compiledExpression->getType(), $compiledExpression->getValue());
33
            } else {
34 1
                $symbol = new \PHPSA\Variable(
35 1
                    $name,
0 ignored issues
show
Bug introduced by
It seems like $name defined by $expr->var->name on line 26 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...
36 1
                    $compiledExpression->getValue(),
37 1
                    $compiledExpression->getType(),
38 1
                    $context->getCurrentBranch()
39 1
                );
40 1
                $context->addVariable($symbol);
41
            }
42
43 1
            if ($expr->expr instanceof VariableNode) {
44 1
                $rightVarName = $expr->expr->name;
45
46 1
                $rightSymbol = $context->getSymbol($rightVarName);
47 1
                if ($rightSymbol) {
48 1
                    $rightSymbol->incUse();
49 1
                    $symbol->setReferencedTo($rightSymbol);
50 1
                } else {
51
                    $context->debug('Cannot fetch variable by name: ' . $rightVarName);
52
                }
53 1
            }
54
55 1
            $symbol->incSets();
56 1
            return $compiledExpression;
57
        }
58
59
        $context->debug('Unknown how to pass symbol by ref');
60
        return new CompiledExpression();
61
    }
62
}
63