Completed
Push — master ( e2bb59...e124f5 )
by Nico
02:06
created

NodeFunction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 35
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNode() 0 22 3
A resolveClassName() 0 4 1
A resolveFunctionName() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rules\AST\Nodes;
11
12
use nicoSWD\Rules\Core\CallableFunction;
13
use nicoSWD\Rules\Exceptions\ParserException;
14
use nicoSWD\Rules\Tokens\BaseToken;
15
16
final class NodeFunction extends BaseNode
17
{
18 30
    public function getNode(): BaseToken
19
    {
20 30
        $current = $this->ast->getStack()->current();
21 30
        $function = $this->resolveFunctionName($current);
22 30
        $class = $this->resolveClassName($function);
23
24 30
        if (!class_exists($class)) {
25 6
            return $this->ast->parser->getFunction($function)->call($this, ...$this->getArguments());
26
        }
27
28
        /** @var CallableFunction $instance */
29 24
        $instance = new $class($current);
30
31 24
        if ($instance->getName() !== $function) {
32 2
            throw new ParserException(sprintf(
33 2
                '%s is not defined',
34 2
                $function
35
            ));
36
        }
37
38 22
        return $instance->call(...$this->getArguments());
0 ignored issues
show
Documentation introduced by
$this->getArguments() is of type object<nicoSWD\Rules\AST\TokenCollection>, but the function expects a object<nicoSWD\Rules\Tokens\BaseToken>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
41 30
    private function resolveClassName(string $class): string
42
    {
43 30
        return '\nicoSWD\Rules\Core\Functions\\' . ucfirst($class);
44
    }
45
46 30
    private function resolveFunctionName(BaseToken $token): string
47
    {
48 30
        return rtrim($token->getValue(), " \r\n(");
49
    }
50
}
51