Completed
Push — master ( d4b65d...556902 )
by Nico
02:06
created

AST::getCallableUserMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 2
crap 1
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\CallableUserFunctionInterface;
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
use nicoSWD\Rule\TokenStream\Token\TokenObject;
18
19
class AST
20
{
21
    /** @var TokenizerInterface */
22
    private $tokenizer;
23
    /** @var TokenFactory */
24
    private $tokenFactory;
25
    /** @var TokenStreamFactory */
26
    private $tokenStreamFactory;
27
    /** @var CallableUserMethodFactoryInterface */
28
    private $userMethodFactory;
29
    /** @var Closure[] */
30
    private $functions = [];
31
    /** @var string[] */
32
    private $methods = [];
33
    /** @var mixed[] */
34
    private $variables = [];
35
36 234
    public function __construct(
37
        TokenizerInterface $tokenizer,
38
        TokenFactory $tokenFactory,
39
        TokenStreamFactory $tokenStreamFactory,
40
        CallableUserMethodFactoryInterface $userMethodFactory
41
    ) {
42 234
        $this->tokenizer = $tokenizer;
43 234
        $this->tokenFactory = $tokenFactory;
44 234
        $this->tokenStreamFactory = $tokenStreamFactory;
45 234
        $this->userMethodFactory = $userMethodFactory;
46 234
    }
47
48 228
    public function getStream(string $rule): TokenStream
49
    {
50 228
        return $this->tokenStreamFactory->create($this->tokenizer->tokenize($rule), $this);
51
    }
52
53 118
    public function getMethod(string $methodName, BaseToken $token): CallableUserFunctionInterface
54
    {
55 118
        if ($token instanceof TokenObject) {
56 10
            return $this->getCallableUserMethod($token, $methodName);
57
        }
58
59 108
        if (empty($this->methods)) {
60 108
            $this->registerMethods();
61
        }
62
63 108
        if (!isset($this->methods[$methodName])) {
64 4
            throw new Exception\UndefinedMethodException();
65
        }
66
67 104
        return new $this->methods[$methodName]($token);
68
    }
69
70 108
    private function registerMethods()
71
    {
72 108
        $this->methods = $this->tokenizer->getGrammar()->getInternalMethods();
73 108
    }
74
75 228
    public function setVariables(array $variables)
76
    {
77 228
        $this->variables = $variables;
78 228
    }
79
80 100
    public function getVariable(string $variableName): BaseToken
81
    {
82 100
        if (!$this->variableExists($variableName)) {
83 4
            throw new UndefinedVariableException($variableName);
84
        }
85
86 96
        return $this->tokenFactory->createFromPHPType($this->variables[$variableName]);
87
    }
88
89 100
    public function variableExists(string $variableName): bool
90
    {
91 100
        return array_key_exists($variableName, $this->variables);
92
    }
93
94 32
    public function getFunction(string $functionName): Closure
95
    {
96 32
        if (empty($this->functions)) {
97 32
            $this->registerFunctions();
98
        }
99
100 32
        if (!isset($this->functions[$functionName])) {
101 6
            throw new Exception\UndefinedFunctionException($functionName);
102
        }
103
104 26
        return $this->functions[$functionName];
105
    }
106
107
    private function registerFunctionClass(string $functionName, string $className)
108
    {
109 26
        $this->functions[$functionName] = function (BaseToken ...$args) use ($className): BaseToken {
110 26
            $function = new $className();
111
112 26
            if (!$function instanceof CallableUserFunctionInterface) {
113 2
                throw new InvalidArgumentException(
114 2
                    sprintf(
115 2
                        "%s must be an instance of %s",
116 2
                        $className,
117 2
                        CallableUserFunctionInterface::class
118
                    )
119
                );
120
            }
121
122 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...
123
        };
124 30
    }
125
126 32
    private function registerFunctions()
127
    {
128 32
        foreach ($this->tokenizer->getGrammar()->getInternalFunctions() as $functionName => $className) {
129 30
            $this->registerFunctionClass($functionName, $className);
130
        }
131 32
    }
132
133 10
    private function getCallableUserMethod(BaseToken $token, string $methodName): CallableUserFunctionInterface
134
    {
135 10
        return $this->userMethodFactory->create($token, $this->tokenFactory, $methodName);
136
    }
137
}
138