Completed
Pull Request — master (#4)
by Nico
02:45 queued 31s
created

NodeFunction::resolveClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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 24
    public function getNode() : BaseToken
22
    {
23 24
        $current = $this->ast->getStack()->current();
24 24
        $function = $this->resolveFunctionName($current);
25 24
        $class = $this->resolveClassName($function);
26
27 24
        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 20
        $instance = new $class($current);
40
41 20
        if ($instance->getName() !== $function) {
42 2
            throw new ParserException(sprintf(
43 2
                '%s is not defined',
44
                $function
45
            ));
46
        }
47
48 18
        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 24
    private function resolveClassName(string $class) : string
52
    {
53 24
        return '\nicoSWD\Rules\Core\Functions\\' . ucfirst($class);
54
    }
55
56 24
    private function resolveFunctionName(BaseToken $token) : string
57
    {
58 24
        return rtrim($token->getValue(), " \r\n(");
59
    }
60
}
61