Skip to content

Commit e9daa84

Browse files
committed
fixed bug in array typing
1 parent d04ecbd commit e9daa84

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/Traits/ErrorTrait.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use InvalidArgumentException;
2626
use TypeError;
2727
use function get_class;
28-
use function gettype;
2928
use function implode;
3029
use function is_array;
3130
use function is_bool;
@@ -54,6 +53,10 @@ trait ErrorTrait
5453
*/
5554
private static function assertClass(string $varName, $classNames, $userInput): void
5655
{
56+
if (!is_array($classNames)) {
57+
$classNames = [$classNames];
58+
}
59+
5760
if (!self::isClass($classNames, $userInput)) {
5861
throw self::typeError($varName, $classNames, $userInput);
5962
}
@@ -97,19 +100,14 @@ public static function getDebugType($value): string
97100
}
98101

99102
/**
100-
* @param string|array $classNames
103+
* @param string[] $classNames
101104
* @param mixed $userInput
102105
* @return bool
103106
*/
104-
private static function isClass($classNames, $userInput): bool
107+
private static function isClass(array $classNames, $userInput): bool
105108
{
106-
if (!is_array($classNames)) {
107-
$classNames = [$classNames];
108-
}
109-
110109
foreach ($classNames as $class) {
111-
$type = self::getDebugType($userInput);
112-
if ($class === $type || is_a($userInput, $class)) {
110+
if (is_a($userInput, $class) || $class === self::getDebugType($userInput)) {
113111
return true;
114112
}
115113
}
@@ -149,22 +147,22 @@ private static function assertValidName(string $name): void
149147

150148
/**
151149
* @param string $varName
152-
* @param $classNames
153-
* @param $userInput
150+
* @param string[] $classNames
151+
* @param mixed $userInput
154152
* @return TypeError
155153
*/
156-
private static function typeError(string $varName, $classNames, $userInput): TypeError
154+
private static function typeError(string $varName, array $classNames, $userInput): TypeError
157155
{
158156
return new TypeError(self::getTypeErrorText($varName, $classNames, $userInput));
159157
}
160158

161159
/**
162160
* @param string $varName
163-
* @param $classNames
164-
* @param $userInput
161+
* @param string[] $classNames
162+
* @param mixed $userInput
165163
* @return string
166164
*/
167-
private static function getTypeErrorText(string $varName, $classNames, $userInput): string
165+
private static function getTypeErrorText(string $varName, array $classNames, $userInput): string
168166
{
169167
return sprintf(
170168
'$%s should be a %s object, %s given.',

0 commit comments

Comments
 (0)