Completed
Push — master ( 166bd7...2a4290 )
by Nico
01:28
created

AST::registerFunctionClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.3755

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 6
cts 11
cp 0.5455
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2.3755
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;
11
12
use Closure;
13
use InvalidArgumentException;
14
use nicoSWD\Rules\Core\CallableUserFunction;
15
use nicoSWD\Rules\Tokens\BaseToken;
16
use nicoSWD\Rules\Tokens\TokenFactory;
17
18
class AST
19
{
20
    /** @var TokenizerInterface */
21
    private $tokenizer;
22
23
    /** @var TokenFactory */
24
    private $tokenFactory;
25
26
    /** @var TokenStream */
27
    private $tokenStream;
28
29
    /** @var Callable[] */
30
    private $functions = [];
31
32
    /** @var mixed[] */
33
    private $variables = [];
34
35
    /** @var string[] */
36
    private $methods = [];
37
38 224
    public function __construct(
39
        TokenizerInterface $tokenizer,
40
        TokenFactory $tokenFactory,
41
        TokenStream $tokenStream
42
    ) {
43 224
        $this->tokenizer = $tokenizer;
44 224
        $this->tokenFactory = $tokenFactory;
45 224
        $this->tokenStream = $tokenStream;
46 224
    }
47
48 220
    public function getStream(string $rule): TokenStream
49
    {
50 220
        return $this->tokenStream->create($this->tokenizer->tokenize($rule), $this);
51
    }
52
53 26
    public function getFunction(string $name): Closure
54
    {
55 26
        if (empty($this->functions)) {
56 26
            $this->registerFunctions($this->tokenizer->getGrammar()->getInternalFunctions());
57
        }
58
59 26
        if (!isset($this->functions[$name])) {
60 4
            throw new Exceptions\ParserException(sprintf(
61 4
                '%s is not defined',
62 4
                $name
63
            ));
64
        }
65
66 22
        return $this->functions[$name];
67
    }
68
69 108
    public function getMethod(string $methodName, BaseToken $token): CallableUserFunction
70
    {
71 108
        if (empty($this->methods)) {
72 108
            $this->registerMethods($this->tokenizer->getGrammar()->getInternalMethods());
73
        }
74
75 108
        if (!isset($this->methods[$methodName])) {
76 4
            throw new Exceptions\UndefinedMethodException();
77
        }
78
79 104
        $method = new $this->methods[$methodName]($token);
80
81 104
        if (!$method instanceof CallableUserFunction) {
82
            throw new InvalidArgumentException(
83
                sprintf(
84
                    "%s must be an instance of %s",
85
                    $methodName,
86
                    CallableUserFunction::class
87
                )
88
            );
89
        }
90
91 104
        return $method;
92
    }
93
94 90
    public function variableExists(string $name): bool
95
    {
96 90
        return array_key_exists($name, $this->variables);
97
    }
98
99 220
    public function setVariables(array $variables)
100
    {
101 220
        $this->variables = $variables;
102 220
    }
103
104 90
    public function getVariable(string $name): BaseToken
105
    {
106 90
        if (!$this->variableExists($name)) {
107 4
            throw new Exceptions\UndefinedVariableException();
108
        }
109
110 86
        return $this->tokenFactory->createFromPHPType($this->variables[$name]);
111
    }
112
113 26
    private function registerFunctionClass(string $className)
114
    {
115
        /** @var CallableUserFunction $function */
116 26
        $function = new $className();
117
118 26
        if (!$function instanceof CallableUserFunction) {
119
            throw new InvalidArgumentException(
120
                sprintf(
121
                    "%s must be an instance of %s",
122
                    $className,
123
                    CallableUserFunction::class
124
                )
125
            );
126
        }
127
128 22
        $this->functions[$function->getName()] = function () use ($function): BaseToken {
129 22
            return $function->call(...func_get_args());
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, 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...
130
        };
131 26
    }
132
133 26
    private function registerFunctions(array $functions)
134
    {
135 26
        foreach ($functions as $function) {
136 26
            $this->registerFunctionClass($function);
137
        }
138 26
    }
139
140 108
    private function registerMethods(array $methods)
141
    {
142 108
        $this->methods = $methods;
143 108
    }
144
}
145