Completed
Push — master ( d76a3b...7bb466 )
by Дмитрий
02:38
created

MethodCall::compile()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 70
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 70
ccs 0
cts 44
cp 0
rs 5.9999
cc 10
eloc 42
nc 14
nop 2
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $leftCompiledExpression = $expressionCompiler->compile($expr->var);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $leftCompiledExpression exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
30
        $leftSymbol = $leftCompiledExpression->getVariable();
31
        if ($leftSymbol) {
32
            if ($leftCompiledExpression->isObject()) {
33
                /** @var ClassDefinition $calledObject */
34
                $calledObject = $leftSymbol->getValue();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $calledObject is correct as $leftSymbol->getValue() (which targets PHPSA\Variable::getValue()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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
                                'undefined-mcall',
41
                                sprintf('Method %s() does not exist in %s scope', $methodName, $expr->var->name),
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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
                                'undefined-mcall',
58
                                sprintf('Method %s() is a static function but called like class method in $%s variable', $methodName, $expr->var->name),
59
                                $expr
60
                            );
61
                        }
62
63
                        return $method->run($context, $expr->args);
64
                    }
65
66
                    return new CompiledExpression;
67
                }
68
69
                /**
70
                 * It's a wrong type or value, maybe it's implemented and We need to fix it in another compilers
71
                 */
72
                $context->debug('Unknown $calledObject - is ' . gettype($calledObject));
73
                return new CompiledExpression();
74
            } elseif (!$leftCompiledExpression->canBeObject()) {
75
                $context->notice(
76
                    'variable-wrongtype.mcall',
77
                    sprintf('Variable $%s is not object\\callable and cannot be called like this', $expr->var->name),
78
                    $expr,
79
                    Check::CHECK_ALPHA
80
                );
81
            }
82
83
            return new CompiledExpression;
84
        }
85
86
        $context->notice(
87
            'undefined-variable.mcall',
88
            sprintf('Variable $%s is not defined in this scope', $expr->var->name),
89
            $expr
90
        );
91
92
        return new CompiledExpression();
93
    }
94
}
95