1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Compiler\Expression; |
7
|
|
|
|
8
|
|
|
use PhpParser\Node\Expr\Variable; |
9
|
|
|
use PHPSA\Check; |
10
|
|
|
use PHPSA\CompiledExpression; |
11
|
|
|
use PHPSA\Context; |
12
|
|
|
use PHPSA\Definition\ClassDefinition; |
13
|
|
|
use PHPSA\Compiler\Expression; |
14
|
|
|
|
15
|
|
|
class MethodCall extends AbstractExpressionCompiler |
16
|
|
|
{ |
17
|
|
|
protected $name = 'PhpParser\Node\Expr\MethodCall'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param \PhpParser\Node\Expr\MethodCall $expr |
21
|
|
|
* @param Context $context |
22
|
|
|
* @return CompiledExpression |
23
|
|
|
*/ |
24
|
|
|
protected function compile($expr, Context $context) |
25
|
|
|
{ |
26
|
|
|
$expressionCompiler = $context->getExpressionCompiler(); |
27
|
|
|
$methodNameCE = $expressionCompiler->compile($expr->name); |
28
|
|
|
|
29
|
|
|
$compiledArguments = $this->parseArgs($expr->args, $context); |
30
|
|
|
|
31
|
|
|
$leftCE = $expressionCompiler->compile($expr->var); |
32
|
|
|
if ($leftCE->isObject()) { |
33
|
|
|
/** @var ClassDefinition $calledObject */ |
34
|
|
|
$calledObject = $leftCE->getValue(); |
35
|
|
|
if ($calledObject instanceof ClassDefinition) { |
36
|
|
|
$methodName = $methodNameCE->isString() ? $methodNameCE->getValue() : false; |
37
|
|
|
if ($methodName) { |
38
|
|
|
if (!$calledObject->hasMethod($methodName, true)) { |
39
|
|
|
$context->notice( |
40
|
|
|
'mcall.undefined', |
41
|
|
|
sprintf('Method %s() does not exist in %s scope', $methodName, $calledObject->getName()), |
42
|
|
|
$expr |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
//it's needed to exit |
46
|
|
|
return new CompiledExpression; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$method = $calledObject->getMethod($methodName, true); |
50
|
|
|
if (!$method) { |
51
|
|
|
$context->debug('getMethod is not working'); |
52
|
|
|
return new CompiledExpression; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($method->isStatic()) { |
56
|
|
|
$context->notice( |
57
|
|
|
'mcall.static', |
58
|
|
|
"Method {$methodName}() is a static function but called like class method", |
59
|
|
|
$expr |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $method->run(clone $context, $compiledArguments); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return new CompiledExpression; |
67
|
|
|
} |
68
|
|
|
} elseif (!$leftCE->canBeObject()) { |
69
|
|
|
$context->notice( |
70
|
|
|
'mcall.not-object', |
71
|
|
|
'Is not object cannot be called like this', |
72
|
|
|
$expr->var, |
73
|
|
|
Check::CHECK_ALPHA |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$context->debug('[Unknown] @todo MethodCall'); |
78
|
|
|
return new CompiledExpression; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \PhpParser\Node\Arg[] $arguments |
83
|
|
|
* @return CompiledExpression[] |
84
|
|
|
*/ |
85
|
|
|
protected function parseArgs(array $arguments = null, Context $context) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$compiled = array(); |
88
|
|
|
|
89
|
|
|
if ($arguments) { |
90
|
|
|
foreach ($arguments as $argument) { |
91
|
|
|
$compiled[] = $context->getExpressionCompiler()->compile($argument->value); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $compiled; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: