From 9538001a42988507a29aa55eef7f59f0462665ab Mon Sep 17 00:00:00 2001 From: madman-81 <50033071+madman-81@users.noreply.github.com> Date: Thu, 4 Aug 2022 14:07:41 +0200 Subject: Throw an IncorrectNumberOfFunctionParametersException if a function gets more arguments than it supports (#117) * Throw an IncorrectNumberOfFunctionParametersException if a function gets more arguments than it supports * Update CustomFunction.php Code Style Co-authored-by: Bruce Wells --- src/NXP/Classes/CustomFunction.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/NXP/Classes/CustomFunction.php') diff --git a/src/NXP/Classes/CustomFunction.php b/src/NXP/Classes/CustomFunction.php index 6e9ffc5..43c5b55 100644 --- a/src/NXP/Classes/CustomFunction.php +++ b/src/NXP/Classes/CustomFunction.php @@ -15,6 +15,8 @@ class CustomFunction */ public $function; + private bool $isVariadic; + private int $totalParamCount; private int $requiredParamCount; /** @@ -26,7 +28,10 @@ class CustomFunction { $this->name = $name; $this->function = $function; - $this->requiredParamCount = (new ReflectionFunction($function))->getNumberOfRequiredParameters(); + $reflection = (new ReflectionFunction($function)); + $this->isVariadic = $reflection->isVariadic(); + $this->totalParamCount = $reflection->getNumberOfParameters(); + $this->requiredParamCount = $reflection->getNumberOfRequiredParameters(); } @@ -40,6 +45,9 @@ class CustomFunction if ($paramCountInStack < $this->requiredParamCount) { throw new IncorrectNumberOfFunctionParametersException($this->name); } + if ($paramCountInStack > $this->totalParamCount && ! $this->isVariadic) { + throw new IncorrectNumberOfFunctionParametersException($this->name); + } $args = []; if ($paramCountInStack > 0) { -- cgit v1.2.3