Completed
Push — master ( 45984e...e3e24d )
by Enrico
17:48 queued 11:36
created

FunctionCall::checkArguments()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.0239

Importance

Changes 0
Metric Value
cc 9
eloc 14
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 27
ccs 14
cts 15
cp 0.9333
crap 9.0239
rs 4.909
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
        foreach ($arguments as $key => $argument) {
120 3
            $parameter = $functionReflection->getParameter($key);
121 3
            $paramType = $parameter->getType();
122 3
            $argumentType = $argument->getType();
123
124 3
            $numberTypes = [CompiledExpression::INTEGER, CompiledExpression::DOUBLE];
125 3
            $callableTypes = [CompiledExpression::STRING, CompiledExpression::ARR];
126
127
            // the paramtype is equal to the argument type or mixed
128
            // or paramtype is number and argumenttype is integer, double
129
            // or paramtype is callable and argumenttype is string, array
130 3
            if (!($paramType == $argumentType || $paramType == CompiledExpression::MIXED)
131 3
            && !($paramType == CompiledExpression::NUMBER && in_array($argumentType, $numberTypes))
132 3
            && !($paramType == CompiledExpression::CALLABLE_TYPE && in_array($argumentType, $callableTypes))) {
133
                return false;
134
            }
135 3
        }
136
137
        // argumentcount != paramcount
138 3
        if (count($arguments) != $functionReflection->getNumberOfRequiredParameters()) {
139 2
            return false;
140
        }
141
142 3
        return true;
143
    }
144
}
145