aboutsummaryrefslogtreecommitdiff
path: root/src/NXP/Classes/CustomFunction.php
diff options
context:
space:
mode:
authorFatih Kızmaz <barka_21@hotmail.com>2022-05-19 05:03:44 +0300
committerGitHub <noreply@github.com>2022-05-19 05:03:44 +0300
commit3e6700d1576c6582169a5d50c95da106ee3017cf (patch)
tree66acb56fd05278a6f7ef328a1cc85c2ecfa9a8d2 /src/NXP/Classes/CustomFunction.php
parentf71b77a62eb27184b5653d6293250e7fda2fdfef (diff)
Added ability to escape quotes in strings. (#110)
* Added ability to escape quotes in strings. * Removed type checking for customfunc arguments. It was a bad idea to check types, because php automatically tries to convert a parameter to required type and throws if it failures. On the other hand, we can check types also in callables if required. * Update phpdoc * Fix some typos + improve min, max, avg funcs. * Update readme + improvements. * Fix a typo in sample. * Fix unshown backslash in readme.
Diffstat (limited to 'src/NXP/Classes/CustomFunction.php')
-rw-r--r--src/NXP/Classes/CustomFunction.php22
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);
}
}