Completed
Push — master ( 09b8d4...001e48 )
by Nico
01:48
created

AST::getMethod()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3
1
<?php declare(strict_types=1);
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
namespace nicoSWD\Rule\TokenStream;
9
10
use Closure;
11
use InvalidArgumentException;
12
use nicoSWD\Rule\Grammar\CallableUserFunction;
13
use nicoSWD\Rule\TokenStream\Exception\UndefinedVariableException;
14
use nicoSWD\Rule\TokenStream\Token\BaseToken;
15
use nicoSWD\Rule\TokenStream\Token\TokenFactory;
16
use nicoSWD\Rule\Tokenizer\TokenizerInterface;
17
18
class AST
19
{
20
    /** @var TokenizerInterface */
21
    private $tokenizer;
22
    /** @var TokenFactory */
23
    private $tokenFactory;
24
    /** @var TokenStreamFactory */
25
    private $tokenStreamFactory;
26
    /** @var Closure[] */
27
    private $functions = [];
28
    /** @var string[] */
29
    private $methods = [];
30
    /** @var mixed[] */
31
    private $variables = [];
32
33 224
    public function __construct(
34
        TokenizerInterface $tokenizer,
35
        TokenFactory $tokenFactory,
36
        TokenStreamFactory $tokenStreamFactory
37
    ) {
38 224
        $this->tokenizer = $tokenizer;
39 224
        $this->tokenFactory = $tokenFactory;
40 224
        $this->tokenStreamFactory = $tokenStreamFactory;
41 224
    }
42
43 218
    public function getStream(string $rule): TokenStream
44
    {
45 218
        return $this->tokenStreamFactory->create($this->tokenizer->tokenize($rule), $this);
46
    }
47
48 108
    public function getMethod(string $methodName, BaseToken $token): CallableUserFunction
49
    {
50 108
        if (empty($this->methods)) {
51 108
            $this->registerMethods();
52
        }
53
54 108
        if (!isset($this->methods[$methodName])) {
55 4
            throw new Exception\UndefinedMethodException($methodName);
56
        }
57
58 104
        return new $this->methods[$methodName]($token);
59
    }
60
61 108
    private function registerMethods()
62
    {
63 108
        $this->methods = $this->tokenizer->getGrammar()->getInternalMethods();
64 108
    }
65
66 218
    public function setVariables(array $variables)
67
    {
68 218
        $this->variables = $variables;
69 218
    }
70
71 90
    public function getVariable(string $variableName): BaseToken
72
    {
73 90
        if (!$this->variableExists($variableName)) {
74 4
            throw new UndefinedVariableException($variableName);
75
        }
76
77 86
        return $this->tokenFactory->createFromPHPType($this->variables[$variableName]);
78
    }
79
80 90
    public function variableExists(string $variableName): bool
81
    {
82 90
        return array_key_exists($variableName, $this->variables);
83
    }
84
85 32
    public function getFunction(string $functionName): Closure
86
    {
87 32
        if (empty($this->functions)) {
88 32
            $this->registerFunctions();
89
        }
90
91 32
        if (!isset($this->functions[$functionName])) {
92 6
            throw new Exception\UndefinedFunctionException($functionName);
93
        }
94
95 26
        return $this->functions[$functionName];
96
    }
97
98
    private function registerFunctionClass(string $functionName, string $className)
99
    {
100 26
        $this->functions[$functionName] = function (BaseToken ...$args) use ($className): BaseToken {
101 26
            $function = new $className();
102
103 26
            if (!$function instanceof CallableUserFunction) {
104 2
                throw new InvalidArgumentException(
105 2
                    sprintf(
106 2
                        "%s must be an instance of %s",
107 2
                        $className,
108 2
                        CallableUserFunction::class
109
                    )
110
                );
111
            }
112
113 24
            return $function->call(...$args);
0 ignored issues
show
Documentation introduced by
$args is of type array<integer,object<nic...tream\Token\BaseToken>>, but the function expects a null|object<nicoSWD\Rule...Stream\Token\BaseToken>.

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...
114
        };
115 30
    }
116
117 32
    private function registerFunctions()
118
    {
119 32
        foreach ($this->tokenizer->getGrammar()->getInternalFunctions() as $functionName => $className) {
120 30
            $this->registerFunctionClass($functionName, $className);
121
        }
122 32
    }
123
}
124