Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions Errors/ExtendableArgumentError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Modulus\Support\Errors;

use Error;

class ExtendableArgumentError extends Error
{
/**
* __construct
*
* @param Error $error
* @param bool $isStatic
* @return mixed
*/
public function __construct(Error $error, string $class, string $method, bool $isStatic = false)
{
if (starts_with($error->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;
}
}
14 changes: 12 additions & 2 deletions Extendable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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);
}
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [{
Expand Down