Completed
Push — master ( f06ad0...9cb85d )
by Nico
06:26
created

AST::getMethod()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.9102

Importance

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