Completed
Pull Request — master (#235)
by Kévin
05:19 queued 02:01
created

AssignRef   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 53
ccs 25
cts 30
cp 0.8333
rs 10
c 1
b 0
f 0
wmc 5
lcom 0
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 41 5
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
                    $expr->expr,
39 1
                    $context->getCurrentBranch()
40 1
                );
41 1
                $context->addVariable($symbol);
42
            }
43
44 1
            if ($expr->expr instanceof VariableNode) {
45 1
                $rightVarName = $expr->expr->name;
46
47 1
                $rightSymbol = $context->getSymbol($rightVarName);
48 1
                if ($rightSymbol) {
49 1
                    $rightSymbol->incUse();
50 1
                    $symbol->setReferencedTo($rightSymbol);
51 1
                } else {
52
                    $context->debug('Cannot fetch variable by name: ' . $rightVarName);
53
                }
54 1
            }
55
56 1
            $symbol->incSets();
57 1
            return $compiledExpression;
58
        }
59
60
        $context->debug('Unknown how to pass symbol by ref');
61
        return new CompiledExpression();
62
    }
63
}
64