|
25 | 25 | use InvalidArgumentException;
|
26 | 26 | use TypeError;
|
27 | 27 | use function get_class;
|
28 |
| -use function gettype; |
29 | 28 | use function implode;
|
30 | 29 | use function is_array;
|
31 | 30 | use function is_bool;
|
@@ -54,6 +53,10 @@ trait ErrorTrait
|
54 | 53 | */
|
55 | 54 | private static function assertClass(string $varName, $classNames, $userInput): void
|
56 | 55 | {
|
| 56 | + if (!is_array($classNames)) { |
| 57 | + $classNames = [$classNames]; |
| 58 | + } |
| 59 | + |
57 | 60 | if (!self::isClass($classNames, $userInput)) {
|
58 | 61 | throw self::typeError($varName, $classNames, $userInput);
|
59 | 62 | }
|
@@ -97,19 +100,14 @@ public static function getDebugType($value): string
|
97 | 100 | }
|
98 | 101 |
|
99 | 102 | /**
|
100 |
| - * @param string|array $classNames |
| 103 | + * @param string[] $classNames |
101 | 104 | * @param mixed $userInput
|
102 | 105 | * @return bool
|
103 | 106 | */
|
104 |
| - private static function isClass($classNames, $userInput): bool |
| 107 | + private static function isClass(array $classNames, $userInput): bool |
105 | 108 | {
|
106 |
| - if (!is_array($classNames)) { |
107 |
| - $classNames = [$classNames]; |
108 |
| - } |
109 |
| - |
110 | 109 | 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)) { |
113 | 111 | return true;
|
114 | 112 | }
|
115 | 113 | }
|
@@ -149,22 +147,22 @@ private static function assertValidName(string $name): void
|
149 | 147 |
|
150 | 148 | /**
|
151 | 149 | * @param string $varName
|
152 |
| - * @param $classNames |
153 |
| - * @param $userInput |
| 150 | + * @param string[] $classNames |
| 151 | + * @param mixed $userInput |
154 | 152 | * @return TypeError
|
155 | 153 | */
|
156 |
| - private static function typeError(string $varName, $classNames, $userInput): TypeError |
| 154 | + private static function typeError(string $varName, array $classNames, $userInput): TypeError |
157 | 155 | {
|
158 | 156 | return new TypeError(self::getTypeErrorText($varName, $classNames, $userInput));
|
159 | 157 | }
|
160 | 158 |
|
161 | 159 | /**
|
162 | 160 | * @param string $varName
|
163 |
| - * @param $classNames |
164 |
| - * @param $userInput |
| 161 | + * @param string[] $classNames |
| 162 | + * @param mixed $userInput |
165 | 163 | * @return string
|
166 | 164 | */
|
167 |
| - private static function getTypeErrorText(string $varName, $classNames, $userInput): string |
| 165 | + private static function getTypeErrorText(string $varName, array $classNames, $userInput): string |
168 | 166 | {
|
169 | 167 | return sprintf(
|
170 | 168 | '$%s should be a %s object, %s given.',
|
|
0 commit comments