Completed
Push — master ( 827422...e8808c )
by Nico
9s
created

NodeFunction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getNode() 0 29 4
A resolveClassName() 0 4 1
A resolveFunctionName() 0 4 1
1
<?php
2
3
/**
4
 * @license     http://opensource.org/licenses/mit-license.php MIT
5
 * @link        https://github.com/nicoSWD
6
 * @author      Nicolas Oelgart <[email protected]>
7
 */
8
declare(strict_types=1);
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
    /**
19
     * @throws ParserException
20
     */
21 28
    public function getNode() : BaseToken
22
    {
23 28
        $current = $this->ast->getStack()->current();
24 28
        $function = $this->resolveFunctionName($current);
25 28
        $class = $this->resolveClassName($function);
26
27 28
        if (!class_exists($class)) {
28 4
            if (!$userFunction = $this->ast->parser->getFunction($function)) {
29 2
                throw new ParserException(sprintf(
30 2
                    '%s is not defined',
31
                    $function
32
                ));
33
            }
34
35 2
            return $userFunction->call($this, ...$this->getArguments());
36
        }
37
38
        /** @var CallableFunction $instance */
39 24
        $instance = new $class($current);
40
41 24
        if ($instance->getName() !== $function) {
42 2
            throw new ParserException(sprintf(
43 2
                '%s is not defined',
44
                $function
45
            ));
46
        }
47
48 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...
49
    }
50
51 28
    private function resolveClassName(string $class) : string
52
    {
53 28
        return '\nicoSWD\Rules\Core\Functions\\' . ucfirst($class);
54
    }
55
56 28
    private function resolveFunctionName(BaseToken $token) : string
57
    {
58 28
        return rtrim($token->getValue(), " \r\n(");
59
    }
60
}
61