diff --git a/Errors/ExtendableArgumentError.php b/Errors/ExtendableArgumentError.php new file mode 100644 index 0000000..55dbe08 --- /dev/null +++ b/Errors/ExtendableArgumentError.php @@ -0,0 +1,91 @@ +getMessage(), 'Argument')) { + + $chars = explode(' ', $error->getMessage()); + + $argument = (int)$chars[1]; + + $srcClass = $chars[4]; + + if ($argument == 1 && $isStatic == false) { + + $this->message = $error->getMessage(); + $this->code = $error->getCode(); + $this->file = $error->getFile(); + $this->line = $error->getLine(); + + } else { + + $args = debug_backtrace()[1]; + + foreach ($args as $key => $value) { + $this->{$key} = $value; + } + + $newArgument = (!$isStatic ? $argument - 1 : $argument); + + /** + * Update the error message + */ + $message = str_replace("{$srcClass}", "{$class}::{$method}()", $error->getMessage()); + $this->message = str_replace("Argument {$argument} passed", "Argument {$newArgument} passed", $message); + + } + + return; + + } else if (starts_with($error->getMessage(), 'Too few arguments to function')) { + + $chars = explode(' ', $error->getMessage()); + + $passed = (int)$chars[6]; + + $expected = (int)$chars[10]; + + $srcClass = $chars[5]; + + // dd($chars); + + $args = debug_backtrace()[1]; + + foreach ($args as $key => $value) { + $this->{$key} = $value; + } + + $newPassed = (!$isStatic ? $passed - 1 : $passed); + + $newExpected = (!$isStatic ? $expected - 1 : $expected); + + /** + * Update the error message + */ + $message = str_replace(", {$passed} passed", ", {$newPassed} passed", $error->getMessage()); + $message = str_replace("{$srcClass}", "{$class}::{$method}()", $message); + $this->message = str_replace("exactly {$expected} expected", "exactly {$newExpected} expected", $message); + + return; + + } + + /** + * Throw the initial error + */ + throw $error; + } +} diff --git a/Extendable.php b/Extendable.php index d7289af..7d9d19a 100644 --- a/Extendable.php +++ b/Extendable.php @@ -2,7 +2,9 @@ namespace Modulus\Support; +use Error; use Closure; +use Modulus\Support\Errors\ExtendableArgumentError; use Modulus\Support\Exceptions\CannotAddMethodException; use Modulus\Support\Exceptions\CannotCallMethodException; @@ -84,7 +86,11 @@ public static function static(string $method, Closure $closure) public function __call(string $method, array $args) { if (array_key_exists($method, Self::$functions)) { - return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args)); + try { + return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args)); + } catch (Error $e) { + throw new ExtendableArgumentError($e, self::class, $method); + } } } @@ -98,7 +104,11 @@ public function __call(string $method, array $args) public static function __callStatic(string $method, array $args) { if (array_key_exists($method, Self::$staticFunctions)) { - return call_user_func_array(Self::$staticFunctions[$method][$method], $args); + try { + return call_user_func_array(Self::$staticFunctions[$method][$method], $args); + } catch (Error $e) { + throw new ExtendableArgumentError($e, self::class, $method, true); + } } } } diff --git a/composer.json b/composer.json index 9d0e769..2661eb4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "modulusphp/support", "description": "Support component for Modulus", - "version": "1.9.3.0", + "version": "1.9.3.1", "license": "MIT", "type": "package", "authors": [{