Completed
Pull Request — master (#325)
by Enrico
02:57
created

StaticCall::parseArgs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
1
<?php
2
/**
3
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression;
9
10
use PHPSA\CompiledExpression;
11
use PHPSA\Context;
12
use PHPSA\Definition\ClassDefinition;
13
14
class StaticCall extends AbstractExpressionCompiler
15
{
16
    protected $name = 'PhpParser\Node\Expr\StaticCall';
17
18
    /**
19
     * {expr}::{expr}();
20
     *
21
     * @param \PhpParser\Node\Expr\StaticCall $expr
22
     * @param Context $context
23
     * @return CompiledExpression
24
     */
25 3
    protected function compile($expr, Context $context)
26
    {
27 3
        $expressionCompiler = $context->getExpressionCompiler();
28 3
        $leftCE = $expressionCompiler->compile($expr->class);
29 3
        $rightCE = $expressionCompiler->compile($expr->name);
30
31 3
        $this->parseArgs($expr->args, $context);
32
33 3
        if ($leftCE->isObject() && $rightCE->isString() && $rightCE->isCorrectValue()) {
34
            /** @var ClassDefinition|null $classDefinition */
35 3
            $classDefinition = $leftCE->getValue();
36 3
            if ($classDefinition instanceof ClassDefinition) {
37 3
                $name = $rightCE->getValue();
38
39 3
                if (!$classDefinition->hasMethod($name, true)) {
40
                    $context->notice(
41
                        'language_error',
42
                        sprintf('Static method %s() does not exist in %s scope', $name, $expr->class),
43
                        $expr
44
                    );
45
46
                    return new CompiledExpression();
47
                }
48
49 3
                $method = $classDefinition->getMethod($name, true);
50 3
                if ($expr->class->toString() !== 'parent' && !$method->isStatic()) {
0 ignored issues
show
Bug introduced by
The method toString does only exist in PhpParser\Node\Name, but not in PhpParser\Node\Expr.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
51
                    $context->notice(
52
                        'language_error',
53
                        sprintf('Method %s() is not static but was called in a static way', $name),
54
                        $expr
55
                    );
56
                }
57 3
            }
58
59 3
            return new CompiledExpression();
60
        }
61
62
        $context->debug('Unknown static function call', $expr);
63
        return new CompiledExpression();
64
    }
65
66
67
    /**
68
     * @param \PhpParser\Node\Arg[] $arguments
69
     * @param Context $context
70
     * @return CompiledExpression[]
71
     */
72 3
    protected function parseArgs(array $arguments, Context $context)
73
    {
74 3
        $compiled = [];
75
76 3
        if ($arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type PhpParser\Node\Arg[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77 2
            foreach ($arguments as $argument) {
78 2
                $compiled[] = $context->getExpressionCompiler()->compile($argument->value);
79 2
            }
80 2
        }
81
82 3
        return $compiled;
83
    }
84
}
85