nicoSWD /
php-rule-parser
| 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\Parser\Exception\ParserException; |
||||
| 14 | use nicoSWD\Rule\TokenStream\Exception\UndefinedVariableException; |
||||
| 15 | use nicoSWD\Rule\TokenStream\Token\BaseToken; |
||||
| 16 | use nicoSWD\Rule\TokenStream\Token\TokenFactory; |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | use nicoSWD\Rule\Tokenizer\TokenizerInterface; |
||||
| 18 | use nicoSWD\Rule\TokenStream\Token\TokenObject; |
||||
| 19 | |||||
| 20 | class AST |
||||
| 21 | { |
||||
| 22 | private array $functions = []; |
||||
| 23 | private array $methods = []; |
||||
| 24 | private array $variables = []; |
||||
| 25 | |||||
| 26 | public function __construct( |
||||
| 27 | private TokenizerInterface $tokenizer, |
||||
|
0 ignored issues
–
show
The parameter
$tokenizer is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 28 | private TokenFactory $tokenFactory, |
||||
|
0 ignored issues
–
show
The parameter
$tokenFactory is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 29 | private TokenStreamFactory $tokenStreamFactory, |
||||
|
0 ignored issues
–
show
The parameter
$tokenStreamFactory is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 30 | private CallableUserMethodFactoryInterface $userMethodFactory |
||||
|
0 ignored issues
–
show
The parameter
$userMethodFactory is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 31 | ) { |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | public function getStream(string $rule): TokenStream |
||||
| 35 | { |
||||
| 36 | return $this->tokenStreamFactory->create($this->tokenizer->tokenize($rule), $this); |
||||
|
0 ignored issues
–
show
|
|||||
| 37 | 286 | } |
|||
| 38 | |||||
| 39 | /** |
||||
| 40 | * @throws Exception\UndefinedMethodException |
||||
| 41 | * @throws Exception\ForbiddenMethodException |
||||
| 42 | */ |
||||
| 43 | 286 | public function getMethod(string $methodName, BaseToken $token): CallableUserFunctionInterface |
|||
| 44 | 286 | { |
|||
| 45 | 286 | if ($token instanceof TokenObject) { |
|||
| 46 | 286 | return $this->getCallableUserMethod($token, $methodName); |
|||
| 47 | 286 | } |
|||
| 48 | |||||
| 49 | 280 | if (empty($this->methods)) { |
|||
| 50 | $this->registerMethods(); |
||||
| 51 | 280 | } |
|||
| 52 | |||||
| 53 | if (!isset($this->methods[$methodName])) { |
||||
| 54 | throw new Exception\UndefinedMethodException(); |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | return new $this->methods[$methodName]($token); |
||||
| 58 | 166 | } |
|||
| 59 | |||||
| 60 | 166 | public function setVariables(array $variables): void |
|||
| 61 | 44 | { |
|||
| 62 | $this->variables = $variables; |
||||
| 63 | } |
||||
| 64 | 122 | ||||
| 65 | 122 | /** |
|||
| 66 | * @throws UndefinedVariableException |
||||
| 67 | * @throws ParserException |
||||
| 68 | 122 | */ |
|||
| 69 | 4 | public function getVariable(string $variableName): BaseToken |
|||
| 70 | { |
||||
| 71 | if (!$this->variableExists($variableName)) { |
||||
| 72 | 118 | throw new UndefinedVariableException($variableName); |
|||
| 73 | } |
||||
| 74 | |||||
| 75 | 280 | return $this->tokenFactory->createFromPHPType($this->variables[$variableName]); |
|||
|
0 ignored issues
–
show
|
|||||
| 76 | } |
||||
| 77 | 280 | ||||
| 78 | 280 | public function variableExists(string $variableName): bool |
|||
| 79 | { |
||||
| 80 | return array_key_exists($variableName, $this->variables); |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | /** @throws Exception\UndefinedFunctionException */ |
||||
| 84 | 140 | public function getFunction(string $functionName): Closure |
|||
| 85 | { |
||||
| 86 | 140 | if (empty($this->functions)) { |
|||
| 87 | 4 | $this->registerFunctions(); |
|||
| 88 | } |
||||
| 89 | |||||
| 90 | 136 | if (!isset($this->functions[$functionName])) { |
|||
| 91 | throw new Exception\UndefinedFunctionException($functionName); |
||||
| 92 | } |
||||
| 93 | 140 | ||||
| 94 | return $this->functions[$functionName]; |
||||
| 95 | 140 | } |
|||
| 96 | |||||
| 97 | private function registerMethods(): void |
||||
| 98 | { |
||||
| 99 | 32 | $this->methods = $this->tokenizer->getGrammar()->getInternalMethods(); |
|||
|
0 ignored issues
–
show
|
|||||
| 100 | } |
||||
| 101 | 32 | ||||
| 102 | 32 | private function registerFunctionClass(string $functionName, string $className): void |
|||
| 103 | { |
||||
| 104 | $this->functions[$functionName] = function (...$args) use ($className) { |
||||
| 105 | 32 | $function = new $className(); |
|||
| 106 | 6 | ||||
| 107 | if (!$function instanceof CallableUserFunctionInterface) { |
||||
| 108 | throw new InvalidArgumentException( |
||||
| 109 | 26 | sprintf( |
|||
| 110 | "%s must be an instance of %s", |
||||
| 111 | $className, |
||||
| 112 | 122 | CallableUserFunctionInterface::class |
|||
| 113 | ) |
||||
| 114 | 122 | ); |
|||
| 115 | 122 | } |
|||
| 116 | |||||
| 117 | 30 | return $function->call(...$args); |
|||
| 118 | }; |
||||
| 119 | } |
||||
| 120 | 26 | ||||
| 121 | private function registerFunctions(): void |
||||
| 122 | 26 | { |
|||
| 123 | 2 | foreach ($this->tokenizer->getGrammar()->getInternalFunctions() as $functionName => $className) { |
|||
|
0 ignored issues
–
show
|
|||||
| 124 | 2 | $this->registerFunctionClass($functionName, $className); |
|||
| 125 | 2 | } |
|||
| 126 | 2 | } |
|||
| 127 | 2 | ||||
| 128 | private function getCallableUserMethod(BaseToken $token, string $methodName): CallableUserFunctionInterface |
||||
| 129 | { |
||||
| 130 | return $this->userMethodFactory->create($token, $this->tokenFactory, $methodName); |
||||
|
0 ignored issues
–
show
|
|||||
| 131 | } |
||||
| 132 | } |
||||
| 133 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths