Completed
Push — master ( c099ef...dd0030 )
by Дмитрий
03:42
created

MethodCall   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 9
c 9
b 0
f 0
lcom 0
cbo 7
dl 0
loc 64
ccs 0
cts 36
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B compile() 0 54 9
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 = new Expression($context);
27
        $methodNameCE = $expressionCompiler->compile($expr->var);
28
29
        $leftCE = $expressionCompiler->compile($expr->var);
30
        if ($leftCE->isObject()) {
31
            /** @var ClassDefinition $calledObject */
32
            $calledObject = $leftCE->getValue();
33
            if ($calledObject instanceof ClassDefinition) {
34
                $methodName = $methodNameCE->isString() ? $methodNameCE->getValue() : false;
35
                if ($methodName) {
36
                    if (!$calledObject->hasMethod($methodName, true)) {
37
                        $context->notice(
38
                            'undefined-mcall',
39
                            sprintf('Method %s() does not exist in %s scope', $methodName, $leftCE->getValue()),
40
                            $expr
41
                        );
42
43
                        //it's needed to exit
44
                        return new CompiledExpression;
45
                    }
46
47
                    $method = $calledObject->getMethod($methodName, true);
48
                    if (!$method) {
49
                        $context->debug('getMethod is not working');
50
                        return new CompiledExpression;
51
                    }
52
53
                    if ($method->isStatic()) {
54
                        $context->notice(
55
                            'mcall.static',
56
                            "Method {$methodName}() is a static function but called like class method",
57
                            $expr
58
                        );
59
                    }
60
61
                    return $method->run($context, $expr->args);
62
                }
63
64
                return new CompiledExpression;
65
            }
66
        } elseif (!$leftCE->canBeObject()) {
67
            $context->notice(
68
                'variable-wrongtype.mcall',
69
                'Is not object cannot be called like this',
70
                $expr->var,
71
                Check::CHECK_ALPHA
72
            );
73
        }
74
75
        $context->debug('[Unknown] @todo MethodCall');
76
        return new CompiledExpression();
77
    }
78
}
79