Completed
Push — master ( 987d4d...982523 )
by Дмитрий
03:08
created

MethodCall::parseArgs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
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)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

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:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
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