Completed
Push — master ( a323dc...8dfe2a )
by Enrico
04:19
created

StaticCall::compile()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 9.0581

Importance

Changes 0
Metric Value
cc 5
eloc 21
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 35
ccs 10
cts 22
cp 0.4545
crap 9.0581
rs 8.439
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
        if ($leftCE->isObject()) {
31 1
            $name = $expr->name;
32
33
            /** @var ClassDefinition $classDefinition */
34 1
            $classDefinition = $context->scope;
35 1
            if (!$classDefinition->hasMethod($name, true)) {
0 ignored issues
show
Bug introduced by
It seems like $name defined by $expr->name on line 31 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...
36
                $context->notice(
37
                    'undefined-scall',
38
                    sprintf('Static method %s() does not exist in %s scope', $name, $expr->class),
39
                    $expr
40
                );
41
42
                return new CompiledExpression();
43
            }
44
45 1
            $method = $classDefinition->getMethod($name, true);
46 1
            if ($expr->class->parts[0] !== 'parent' && !$method->isStatic()) {
47
                $context->notice(
48
                    'undefined-scall',
49
                    sprintf('Method %s() is not static but was called in a static way', $name),
50
                    $expr
51
                );
52
            }
53
54 1
            return new CompiledExpression();
55
        }
56
57
        $context->debug('Unknown static function call');
58
        return new CompiledExpression();
59
    }
60
}
61