Completed
Push — master ( a96d79...b6226c )
by Дмитрий
03:40
created

StaticCall::compile()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.9812

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 5
nop 2
dl 0
loc 39
ccs 13
cts 25
cp 0.52
crap 9.9812
rs 8.439
c 0
b 0
f 0
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 1
    protected function compile($expr, Context $context)
26
    {
27 1
        $expressionCompiler = $context->getExpressionCompiler();
28 1
        $leftCE = $expressionCompiler->compile($expr->class);
29
30 1
        $this->parseArgs($expr->args, $context);
31
32 1
        if ($leftCE->isObject()) {
33
            /** @var ClassDefinition|null $classDefinition */
34 1
            $classDefinition = $leftCE->getValue();
35 1
            if ($classDefinition instanceof ClassDefinition) {
36 1
                $name = $expr->name;
37
                
38 1
                if (!$classDefinition->hasMethod($name, true)) {
0 ignored issues
show
Bug introduced by
It seems like $name defined by $expr->name on line 36 can also be of type object<PhpParser\Node\Expr>; however, PHPSA\Definition\ClassDefinition::hasMethod() 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...
39
                    $context->notice(
40
                        'language_error',
41
                        sprintf('Static method %s() does not exist in %s scope', $name, $expr->class),
42
                        $expr
43
                    );
44
45
                    return new CompiledExpression();
46
                }
47
48 1
                $method = $classDefinition->getMethod($name, true);
49 1
                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...
50
                    $context->notice(
51
                        'language_error',
52
                        sprintf('Method %s() is not static but was called in a static way', $name),
53
                        $expr
54
                    );
55
                }
56 1
            }
57
58 1
            return new CompiledExpression();
59
        }
60
61
        $context->debug('Unknown static function call', $expr);
62
        return new CompiledExpression();
63
    }
64
65
66
    /**
67
     * @param \PhpParser\Node\Arg[] $arguments
68
     * @param Context $context
69
     * @return CompiledExpression[]
70
     */
71 1
    protected function parseArgs(array $arguments, Context $context)
72
    {
73 1
        $compiled = [];
74
75 1
        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...
76
            foreach ($arguments as $argument) {
77
                $compiled[] = $context->getExpressionCompiler()->compile($argument->value);
78
            }
79
        }
80
81 1
        return $compiled;
82
    }
83
}
84