Completed
Pull Request — master (#325)
by Enrico
02:57
created

MethodCall::compile()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 35

Importance

Changes 0
Metric Value
cc 8
eloc 35
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 56
ccs 9
cts 36
cp 0.25
crap 35
rs 7.3333

How to fix   Long Method   

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 PHPSA\Check;
9
use PHPSA\CompiledExpression;
10
use PHPSA\Context;
11
use PHPSA\Definition\ClassDefinition;
12
13
class MethodCall extends AbstractExpressionCompiler
14
{
15
    protected $name = 'PhpParser\Node\Expr\MethodCall';
16
17
    /**
18
     * @param \PhpParser\Node\Expr\MethodCall $expr
19
     * @param Context $context
20
     * @return CompiledExpression
21
     */
22 2
    protected function compile($expr, Context $context)
23
    {
24 2
        $expressionCompiler = $context->getExpressionCompiler();
25 2
        $methodNameCE = $expressionCompiler->compile($expr->name);
26
27 2
        $compiledArguments = $this->parseArgs($expr->args, $context);
28
29 2
        $leftCE = $expressionCompiler->compile($expr->var);
30 2
        if ($leftCE->canBeObject()) {
31
            /** @var ClassDefinition $calledObject */
32 2
            $calledObject = $leftCE->getValue();
33 2
            if ($calledObject instanceof ClassDefinition) {
34
                $methodName = $methodNameCE->isString() ? $methodNameCE->getValue() : false;
35
                if ($methodName) {
36
                    if (!$calledObject->hasMethod($methodName, true)) {
37
                        $context->notice(
38
                            'language_error',
39
                            sprintf('Method %s() does not exist in %s scope', $methodName, $calledObject->getName()),
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', $expr);
50
                        return new CompiledExpression();
51
                    }
52
53
                    if ($method->isStatic()) {
54
                        $context->notice(
55
                            'language_error',
56
                            "Method {$methodName}() is static but called like a class method",
57
                            $expr
58
                        );
59
                    }
60
61
                    return $method->run(clone $context, $compiledArguments);
62
                }
63
            }
64
65 2
            return new CompiledExpression();
66
        } else {
67
            $context->notice(
68
                'language_error',
69
                sprintf('$%s is not an object and cannot be called like this', $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...
70
                $expr->var,
71
                Check::CHECK_ALPHA
72
            );
73
        }
74
75
        $context->debug('[Unknown] @todo MethodCall', $expr);
76
        return new CompiledExpression();
77
    }
78
79
    /**
80
     * @param \PhpParser\Node\Arg[] $arguments
81
     * @param Context $context
82
     * @return CompiledExpression[]
83
     */
84 2
    protected function parseArgs(array $arguments, Context $context)
85
    {
86 2
        $compiled = [];
87
88 2
        if ($arguments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $arguments of type PhpParser\Node\Arg[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89 2
            foreach ($arguments as $argument) {
90 2
                $compiled[] = $context->getExpressionCompiler()->compile($argument->value);
91 2
            }
92 2
        }
93
94 2
        return $compiled;
95
    }
96
}
97