|
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
|
|
|
protected function compile($expr, Context $context) |
|
26
|
|
|
{ |
|
27
|
|
|
$expressionCompiler = $context->getExpressionCompiler(); |
|
28
|
|
|
$leftCE = $expressionCompiler->compile($expr->class); |
|
29
|
|
|
$rightCE = $expressionCompiler->compile($expr->name); |
|
30
|
|
|
|
|
31
|
|
|
$this->parseArgs($expr->args, $context); |
|
32
|
|
|
|
|
33
|
|
|
if ($leftCE->isObject() && $rightCE->isString() && $rightCE->isCorrectValue()) { |
|
34
|
|
|
/** @var ClassDefinition|null $classDefinition */ |
|
35
|
|
|
$classDefinition = $leftCE->getValue(); |
|
36
|
|
|
if ($classDefinition instanceof ClassDefinition) { |
|
37
|
|
|
$name = $rightCE->getValue(); |
|
38
|
|
|
|
|
39
|
|
|
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
|
|
|
$method = $classDefinition->getMethod($name, true); |
|
50
|
|
|
if ($expr->class->toString() !== 'parent' && !$method->isStatic()) { |
|
|
|
|
|
|
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
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
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
|
|
|
protected function parseArgs(array $arguments, Context $context) |
|
73
|
|
|
{ |
|
74
|
|
|
$compiled = []; |
|
75
|
|
|
|
|
76
|
|
|
if ($arguments) { |
|
|
|
|
|
|
77
|
|
|
foreach ($arguments as $argument) { |
|
78
|
|
|
$compiled[] = $context->getExpressionCompiler()->compile($argument->value); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $compiled; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: