Completed
Pull Request — master (#208)
by Enrico
03:43
created

FunctionCall::checkArguments()   D

Complexity

Conditions 10
Paths 5

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.0203

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 5
nop 2
dl 0
loc 29
ccs 16
cts 17
cp 0.9412
crap 10.0203
rs 4.8196
c 0
b 0
f 0

How to fix   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
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression;
9
10
use Ovr\PHPReflection\Reflector;
11
use PHPSA\CompiledExpression;
12
use PHPSA\Context;
13
use PHPSA\Definition\ClosureDefinition;
14
15
class FunctionCall extends AbstractExpressionCompiler
16
{
17
    protected $name = 'PhpParser\Node\Expr\FuncCall';
18
19
    /**
20
     * @param \PhpParser\Node\Expr\FuncCall $expr
21
     * @param Context $context
22
     * @return CompiledExpression
23
     */
24 10
    protected function compile($expr, Context $context)
0 ignored issues
show
Complexity introduced by
This operation has 270 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
25
    {
26 10
        $expressionCompiler = $context->getExpressionCompiler();
27 10
        $fNameExpression = $expressionCompiler->compile($expr->name);
28 10
        $name = $fNameExpression->getValue();
29
30 10
        $compiler = $context->application->compiler;
31 10
        $exists = false;
0 ignored issues
show
Unused Code introduced by
$exists is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32 10
        $arguments = $this->parseArgs($expr, clone $context);
33
34
        // is it a Closure
35 10
        if ($fNameExpression->isCallable() && $name instanceof ClosureDefinition) {
36
            return $name->run($this->parseArgs($expr, clone $context), $context);
37
        }
38
39
        // is the function name a correct string
40 10
        if (!$fNameExpression->isString() || !$fNameExpression->isCorrectValue()) {
41
            $context->debug(
42
                'Unexpected function name type ' . $fNameExpression->getTypeName(),
43
                $expr->name
44
            );
45
46
            return new CompiledExpression();
47
        }
48
49
        // namespace check for correct functionDefinition
50 10
        if ($context->scope) {
51 10
            $functionDefinition = $compiler->getFunctionNS($name, $context->scope->getNamespace());
52 10
        } else {
53 1
            $functionDefinition = $compiler->getFunction($name);
54
        }
55
56
        // does the function exist
57 10
        if ($functionDefinition) {
58
            if (!$functionDefinition->isCompiled()) {
59
                $functionDefinition->compile(clone $context);
60
            }
61
62
            $exists = true;
63
        } else {
64 10
            $exists = function_exists($name);
65
        }
66
67 10
        if (!$exists) {
68
            $context->notice(
69
                'undefined-fcall',
70
                sprintf('Function %s() does not exist', $expr->name->parts[0]),
71
                $expr
72
            );
73
        } else {
74 10
            $reflector = new Reflector(Reflector::manuallyFactory());
75 10
            $functionReflection = $reflector->getFunction($name);
76 10
            if ($functionReflection) {
77 3
                $argumentsSuccessPass = $this->checkArguments($arguments, $functionReflection);
78
79
                // when everything is ok we run the function
80 3
                if ($argumentsSuccessPass && $functionReflection->isRunnable()) {
81 3
                    array_walk(
82 3
                        $arguments,
83 2
                        function (&$item) {
84
                            /** @var CompiledExpression $item */
85 2
                            $item = $item->getValue();
86 2
                        }
87 3
                    );
88
89 3
                    return new CompiledExpression(
90 3
                        $functionReflection->getReturnType(),
91 3
                        $functionReflection->run($arguments)
92 3
                    );
93
                }
94
95 2
                return new CompiledExpression($functionReflection->getReturnType());
96
            }
97
        }
98
99 9
        return new CompiledExpression();
100
    }
101
102
    /**
103
     * @param \PhpParser\Node\Expr\FuncCall $expr
104
     * @return CompiledExpression[]
105
     */
106 10
    protected function parseArgs($expr, Context $context)
107
    {
108 10
        $arguments = [];
109
110 10
        foreach ($expr->args as $argument) {
111 9
            $arguments[] = $context->getExpressionCompiler()->compile($argument->value);
112 10
        }
113
114 10
        return $arguments;
115
    }
116
117 3
    protected function checkArguments(array $arguments, $functionReflection)
118
    {
119 3
        if (count($arguments) > 0) {
120 3
            foreach ($arguments as $key => $argument) {
121 3
                $parameter = $functionReflection->getParameter($key);
122 3
                $paramType = $parameter->getType();
123 3
                $argumentType = $argument->getType();
124
125 3
                $numberTypes = [CompiledExpression::INTEGER, CompiledExpression::DOUBLE];
126 3
                $callableTypes = [CompiledExpression::STRING, CompiledExpression::ARR];
127
128
                // the paramtype is equal to the argument type or mixed
129
                // or paramtype is number and argumenttype is integer, double
130
                // or paramtype is callable and argumenttype is string, array
131 3
                if (!($paramType == $argumentType || $paramType == CompiledExpression::MIXED)
132 3
                && !($paramType == CompiledExpression::NUMBER && in_array($argumentType, $numberTypes))
133 3
                && !($paramType == CompiledExpression::CALLABLE_TYPE && in_array($argumentType, $callableTypes))) {
134
                    return false;
135
                }
136 3
            }
137 3
        }
138
139
        // argumentcount != paramcount
140 3
        if (count($arguments) != $functionReflection->getNumberOfRequiredParameters()) {
141 2
            return false;
142
        }
143
144 3
        return true;
145
    }
146
}
147