Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 4e9bf31

Browse files
authored
Merge pull request #15 from modulusphp/feature/argumenterror
Feature/argumenterror
2 parents 5196ebe + 5a398dc commit 4e9bf31

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

Errors/ExtendableArgumentError.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Modulus\Support\Errors;
4+
5+
use Error;
6+
7+
class ExtendableArgumentError extends Error
8+
{
9+
/**
10+
* __construct
11+
*
12+
* @param Error $error
13+
* @param bool $isStatic
14+
* @return mixed
15+
*/
16+
public function __construct(Error $error, string $class, string $method, bool $isStatic = false)
17+
{
18+
if (starts_with($error->getMessage(), 'Argument')) {
19+
20+
$chars = explode(' ', $error->getMessage());
21+
22+
$argument = (int)$chars[1];
23+
24+
$srcClass = $chars[4];
25+
26+
if ($argument == 1 && $isStatic == false) {
27+
28+
$this->message = $error->getMessage();
29+
$this->code = $error->getCode();
30+
$this->file = $error->getFile();
31+
$this->line = $error->getLine();
32+
33+
} else {
34+
35+
$args = debug_backtrace()[1];
36+
37+
foreach ($args as $key => $value) {
38+
$this->{$key} = $value;
39+
}
40+
41+
$newArgument = (!$isStatic ? $argument - 1 : $argument);
42+
43+
/**
44+
* Update the error message
45+
*/
46+
$message = str_replace("{$srcClass}", "{$class}::{$method}()", $error->getMessage());
47+
$this->message = str_replace("Argument {$argument} passed", "Argument {$newArgument} passed", $message);
48+
49+
}
50+
51+
return;
52+
53+
} else if (starts_with($error->getMessage(), 'Too few arguments to function')) {
54+
55+
$chars = explode(' ', $error->getMessage());
56+
57+
$passed = (int)$chars[6];
58+
59+
$expected = (int)$chars[10];
60+
61+
$srcClass = $chars[5];
62+
63+
// dd($chars);
64+
65+
$args = debug_backtrace()[1];
66+
67+
foreach ($args as $key => $value) {
68+
$this->{$key} = $value;
69+
}
70+
71+
$newPassed = (!$isStatic ? $passed - 1 : $passed);
72+
73+
$newExpected = (!$isStatic ? $expected - 1 : $expected);
74+
75+
/**
76+
* Update the error message
77+
*/
78+
$message = str_replace(", {$passed} passed", ", {$newPassed} passed", $error->getMessage());
79+
$message = str_replace("{$srcClass}", "{$class}::{$method}()", $message);
80+
$this->message = str_replace("exactly {$expected} expected", "exactly {$newExpected} expected", $message);
81+
82+
return;
83+
84+
}
85+
86+
/**
87+
* Throw the initial error
88+
*/
89+
throw $error;
90+
}
91+
}

Extendable.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Modulus\Support;
44

5+
use Error;
56
use Closure;
7+
use Modulus\Support\Errors\ExtendableArgumentError;
68
use Modulus\Support\Exceptions\CannotAddMethodException;
79
use Modulus\Support\Exceptions\CannotCallMethodException;
810

@@ -84,7 +86,11 @@ public static function static(string $method, Closure $closure)
8486
public function __call(string $method, array $args)
8587
{
8688
if (array_key_exists($method, Self::$functions)) {
87-
return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args));
89+
try {
90+
return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args));
91+
} catch (Error $e) {
92+
throw new ExtendableArgumentError($e, self::class, $method);
93+
}
8894
}
8995
}
9096

@@ -98,7 +104,11 @@ public function __call(string $method, array $args)
98104
public static function __callStatic(string $method, array $args)
99105
{
100106
if (array_key_exists($method, Self::$staticFunctions)) {
101-
return call_user_func_array(Self::$staticFunctions[$method][$method], $args);
107+
try {
108+
return call_user_func_array(Self::$staticFunctions[$method][$method], $args);
109+
} catch (Error $e) {
110+
throw new ExtendableArgumentError($e, self::class, $method, true);
111+
}
102112
}
103113
}
104114
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "modulusphp/support",
33
"description": "Support component for Modulus",
4-
"version": "1.9.3.0",
4+
"version": "1.9.3.1",
55
"license": "MIT",
66
"type": "package",
77
"authors": [{

0 commit comments

Comments
 (0)