diff options
Diffstat (limited to 'src/NXP/Classes/CustomFunction.php')
-rw-r--r-- | src/NXP/Classes/CustomFunction.php | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 1ebdd2c..6e9ffc5 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -2,7 +2,6 @@ namespace NXP\Classes; -use NXP\Exception\IncorrectFunctionParameterException; use NXP\Exception\IncorrectNumberOfFunctionParametersException; use ReflectionException; use ReflectionFunction; @@ -16,7 +15,7 @@ class CustomFunction */ public $function; - private ReflectionFunction $reflectionFunction; + private int $requiredParamCount; /** * CustomFunction constructor. @@ -27,36 +26,25 @@ class CustomFunction { $this->name = $name; $this->function = $function; - $this->reflectionFunction = new ReflectionFunction($function); + $this->requiredParamCount = (new ReflectionFunction($function))->getNumberOfRequiredParameters(); } /** * @param array<Token> $stack * - * @throws IncorrectNumberOfFunctionParametersException|IncorrectFunctionParameterException + * @throws IncorrectNumberOfFunctionParametersException */ public function execute(array &$stack, int $paramCountInStack) : Token { - if ($paramCountInStack < $this->reflectionFunction->getNumberOfRequiredParameters()) { + if ($paramCountInStack < $this->requiredParamCount) { throw new IncorrectNumberOfFunctionParametersException($this->name); } $args = []; if ($paramCountInStack > 0) { - $reflectionParameters = $this->reflectionFunction->getParameters(); - for ($i = 0; $i < $paramCountInStack; $i++) { - $value = \array_pop($stack)->value; - $valueType = \gettype($value); - $reflectionParameter = $reflectionParameters[\min(\count($reflectionParameters) - 1, $i)]; - //TODO to support type check for union types (php >= 8.0) and intersection types (php >= 8.1), we should increase min php level in composer.json - // For now, only support basic types. @see testFunctionParameterTypes - if ($reflectionParameter->hasType() && $reflectionParameter->getType()->getName() !== $valueType){ - throw new IncorrectFunctionParameterException(); - } - - \array_unshift($args, $value); + \array_unshift($args, \array_pop($stack)->value); } } |