aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/Classes/CustomFunction.php
diff options
context:
space:
mode:
authorFatih Kızmaz <barka_21@hotmail.com>2022-05-13 15:55:52 +0300
committerGitHub <noreply@github.com>2022-05-13 15:55:52 +0300
commite21d59c9def9f25bc6a7c3fbd41e2e126d68b1df (patch)
tree379ebf22c76d48aa5897bb9db487c60cae542533 /src/NXP/Classes/CustomFunction.php
parentda506a7ce05c6141e3c32f72c351eace38577f89 (diff)
Support unlimited args for min, max default funcs. (#106)
* Support unlimited args for min, max default funcs. Default functions max and min were requiring 2 arguments strictly. Now they supoort unlimited args, same as php's min, max funcs. * Improved functions: support unlimited parameters (see min, max funcs), optional parameters (see round func), parameters with types (see round func, throws IncorrectFunctionParameterException on unmatched type, union types and intersection types not supported because of min php level! there is a todo for this, to support them later @see CustomFunction@execute) Also added unittests for improvements. * Run php-cs-fixer fix
Diffstat (limited to 'src/NXP/Classes/CustomFunction.php')
-rw-r--r--src/NXP/Classes/CustomFunction.php36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php
index 843cf82..1ebdd2c 100644
--- a/src/NXP/Classes/CustomFunction.php
+++ b/src/NXP/Classes/CustomFunction.php
@@ -2,6 +2,7 @@
namespace NXP\Classes;
+use NXP\Exception\IncorrectFunctionParameterException;
use NXP\Exception\IncorrectNumberOfFunctionParametersException;
use ReflectionException;
use ReflectionFunction;
@@ -15,41 +16,48 @@ class CustomFunction
*/
public $function;
- public int $places = 0;
+ private ReflectionFunction $reflectionFunction;
/**
* CustomFunction constructor.
*
* @throws ReflectionException
- * @throws IncorrectNumberOfFunctionParametersException
*/
- public function __construct(string $name, callable $function, ?int $places = null)
+ public function __construct(string $name, callable $function)
{
$this->name = $name;
$this->function = $function;
+ $this->reflectionFunction = new ReflectionFunction($function);
- if (null === $places) {
- $reflection = new ReflectionFunction($function);
- $this->places = $reflection->getNumberOfParameters();
- } else {
- $this->places = $places;
- }
}
/**
* @param array<Token> $stack
*
- * @throws IncorrectNumberOfFunctionParametersException
+ * @throws IncorrectNumberOfFunctionParametersException|IncorrectFunctionParameterException
*/
- public function execute(array &$stack) : Token
+ public function execute(array &$stack, int $paramCountInStack) : Token
{
- if (\count($stack) < $this->places) {
+ if ($paramCountInStack < $this->reflectionFunction->getNumberOfRequiredParameters()) {
throw new IncorrectNumberOfFunctionParametersException($this->name);
}
$args = [];
- for ($i = 0; $i < $this->places; $i++) {
- \array_unshift($args, \array_pop($stack)->value);
+ 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);
+ }
}
$result = \call_user_func_array($this->function, $args);