Passed
Push — master ( 430c15...f6872f )
by Tim
02:52 queued 13s
created

Assert::valueToString()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
eloc 21
c 2
b 0
f 0
nc 11
nop 1
dl 0
loc 43
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Assert;
6
7
use BadMethodCallException; // Requires ext-spl
8
use DateTime; // Requires ext-date
9
use DateTimeImmutable; // Requires ext-date
10
use InvalidArgumentException; // Requires ext-spl
11
use Throwable;
12
use Webmozart\Assert\Assert as Webmozart;
13
14
use function array_pop;
15
use function array_unshift;
16
use function call_user_func_array;
17
use function end;
18
use function enum_exists;
0 ignored issues
show
introduced by
The function enum_exists was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
19
use function function_exists;
20
use function get_class;
21
use function is_object;
22
use function is_resource;
23
use function is_string;
24
use function is_subclass_of;
25
use function lcfirst;
26
use function method_exists;
27
use function preg_match; // Requires ext-pcre
28
use function strval;
29
30
/**
31
 * Webmozart\Assert wrapper class
32
 *
33
 * @package simplesamlphp/assert
34
 *
35
 * @method static void string(mixed $value, string $message = '', string $exception = '')
36
 * @method static void stringNotEmpty(mixed $value, string $message = '', string $exception = '')
37
 * @method static void integer(mixed $value, string $message = '', string $exception = '')
38
 * @method static void integerish(mixed $value, string $message = '', string $exception = '')
39
 * @method static void positiveInteger(mixed $value, string $message = '', string $exception = '')
40
 * @method static void float(mixed $value, string $message = '', string $exception = '')
41
 * @method static void numeric(mixed $value, string $message = '', string $exception = '')
42
 * @method static void natural(mixed $value, string $message = '', string $exception = '')
43
 * @method static void boolean(mixed $value, string $message = '', string $exception = '')
44
 * @method static void scalar(mixed $value, string $message = '', string $exception = '')
45
 * @method static void object(mixed $value, string $message = '', string $exception = '')
46
 * @method static void resource(mixed $value, string|null $type, string $message = '', string $exception = '')
47
 * @method static void isCallable(mixed $value, string $message = '', string $exception = '')
48
 * @method static void isArray(mixed $value, string $message = '', string $exception = '')
49
 * @method static void isTraversable(mixed $value, string $message = '', string $exception = '')
50
 * @method static void isArrayAccessible(mixed $value, string $message = '', string $exception = '')
51
 * @method static void isCountable(mixed $value, string $message = '', string $exception = '')
52
 * @method static void isIterable(mixed $value, string $message = '', string $exception = '')
53
 * @method static void isInstanceOf(mixed $value, string|object $class, string $message = '', string $exception = '')
54
 * @method static void notInstanceOf(mixed $value, string|object $class, string $message = '', string $exception = '')
55
 * @method static void isInstanceOfAny(mixed $value, array $classes, string $message = '', string $exception = '')
56
 * @method static void isAOf(string|object $value, string $class, string $message = '', string $exception = '')
57
 * @method static void isNotA(string|object $value, string $class, string $message = '', string $exception = '')
58
 * @method static void isAnyOf(string|object $value, string[] $classes, string $message = '', string $exception = '')
59
 * @method static void isEmpty(mixed $value, string $message = '', string $exception = '')
60
 * @method static void notEmpty(mixed $value, string $message = '', string $exception = '')
61
 * @method static void null(mixed $value, string $message = '', string $exception = '')
62
 * @method static void notNull(mixed $value, string $message = '', string $exception = '')
63
 * @method static void true(mixed $value, string $message = '', string $exception = '')
64
 * @method static void false(mixed $value, string $message = '', string $exception = '')
65
 * @method static void notFalse(mixed $value, string $message = '', string $exception = '')
66
 * @method static void ip(mixed $value, string $message = '', string $exception = '')
67
 * @method static void ipv4(mixed $value, string $message = '', string $exception = '')
68
 * @method static void ipv6(mixed $value, string $message = '', string $exception = '')
69
 * @method static void email(mixed $value, string $message = '', string $exception = '')
70
 * @method static void uniqueValues(array $values, string $message = '', string $exception = '')
71
 * @method static void eq(mixed $value, mixed $expect, string $message = '', string $exception = '')
72
 * @method static void notEq(mixed $value, mixed $expect, string $message = '', string $exception = '')
73
 * @method static void same(mixed $value, mixed $expect, string $message = '', string $exception = '')
74
 * @method static void notSame(mixed $value, mixed $expect, string $message = '', string $exception = '')
75
 * @method static void greaterThan(mixed $value, mixed $limit, string $message = '', string $exception = '')
76
 * @method static void greaterThanEq(mixed $value, mixed $limit, string $message = '', string $exception = '')
77
 * @method static void lessThan(mixed $value, mixed $limit, string $message = '', string $exception = '')
78
 * @method static void lessThanEq(mixed $value, mixed $limit, string $message = '', string $exception = '')
79
 * @method static void range(mixed $value, mixed $min, mixed $max, string $message = '', string $exception = '')
80
 * @method static void oneOf(mixed $value, array $values, string $message = '', string $exception = '')
81
 * @method static void inArray(mixed $value, mixed $values, string $message = '', string $exception = '')
82
 * @method static void contains(string $value, string $subString, string $message = '', string $exception = '')
83
 * @method static void notContains(string $value, string $subString, string $message = '', string $exception = '')
84
 * @method static void notWhitespaceOnly($value, string $message = '', string $exception = '')
85
 * @method static void startsWith(string $value, string $prefix, string $message = '', string $exception = '')
86
 * @method static void notStartsWith(string $value, string $prefix, string $message = '', string $exception = '')
87
 * @method static void startsWithLetter(mixed $value, string $message = '', string $exception = '')
88
 * @method static void endsWith(string $value, string $suffix, string $message = '', string $exception = '')
89
 * @method static void notEndsWith(string $value, string $suffix, string $message = '', string $exception = '')
90
 * @method static void regex(string $value, string $pattern, string $message = '', string $exception = '')
91
 * @method static void notRegex(string $value, string $pattern, string $message = '', string $exception = '')
92
 * @method static void unicodeLetters(mixed $value, string $message = '', string $exception = '')
93
 * @method static void alpha(mixed $value, string $message = '', string $exception = '')
94
 * @method static void digits(string $value, string $message = '', string $exception = '')
95
 * @method static void alnum(string $value, string $message = '', string $exception = '')
96
 * @method static void lower(string $value, string $message = '', string $exception = '')
97
 * @method static void upper(string $value, string $message = '', string $exception = '')
98
 * @method static void length(string $value, int $length, string $message = '', string $exception = '')
99
 * @method static void minLength(string $value, int|float $min, string $message = '', string $exception = '')
100
 * @method static void maxLength(string $value, int|float $max, string $message = '', string $exception = '')
101
 * @method static void lengthBetween(string $value, int|float $min, int|float $max, string $message = '', string $exception = '')
102
 * @method static void fileExists(mixed $value, string $message = '', string $exception = '')
103
 * @method static void file(mixed $value, string $message = '', string $exception = '')
104
 * @method static void directory(mixed $value, string $message = '', string $exception = '')
105
 * @method static void readable(string $value, string $message = '', string $exception = '')
106
 * @method static void writable(string $value, string $message = '', string $exception = '')
107
 * @method static void classExists(mixed $value, string $message = '', string $exception = '')
108
 * @method static void subclassOf(mixed $value, string|object $class, string $message = '', string $exception = '')
109
 * @method static void interfaceExists(mixed $value, string $message = '', string $exception = '')
110
 * @method static void implementsInterface(mixed $value, mixed $interface, string $message = '', string $exception = '')
111
 * @method static void propertyExists(string|object $classOrObject, mixed $property, string $message = '', string $exception = '')
112
 * @method static void propertyNotExists(string|object $classOrObject, mixed $property, string $message = '', string $exception = '')
113
 * @method static void methodExists(string|object $classOrObject, mixed $method, string $message = '', string $exception = '')
114
 * @method static void methodNotExists(string|object $classOrObject, mixed $method, string $message = '', string $exception = '')
115
 * @method static void keyExists(array $array, string|int $key, string $message = '', string $exception = '')
116
 * @method static void keyNotExists(array $array, string|int $key, string $message = '', string $exception = '')
117
 * @method static void validArrayKey($value, string $message = '', string $exception = '')
118
 * @method static void count(Countable|array $array, int $number, string $message = '', string $exception = '')
119
 * @method static void minCount(Countable|array $array, int|float $min, string $message = '', string $exception = '')
120
 * @method static void maxCount(Countable|array $array, int|float $max, string $message = '', string $exception = '')
121
 * @method static void countBetween(Countable|array $array, int|float $min, int|float $max, string $message = '', string $exception = '')
122
 * @method static void isList(mixed $array, string $message = '', string $exception = '')
123
 * @method static void isNonEmptyList(mixed $array, string $message = '', string $exception = '')
124
 * @method static void isMap(mixed $array, string $message = '', string $exception = '')
125
 * @method static void isNonEmptyMap(mixed $array, string $message = '', string $exception = '')
126
 * @method static void uuid(string $value, string $message = '', string $exception = '')
127
 * @method static void throws(Closure $expression, string $class = 'Exception', string $message = '', string $exception = '')
128
 *
129
 * @method static void nullOrString(mixed $value, string $message = '', string $exception = '')
130
 * @method static void allString(mixed $value, string $message = '', string $exception = '')
131
 * @method static void nullOrStringNotEmpty(mixed $value, string $message = '', string $exception = '')
132
 * @method static void allOrStringNotEmpty(mixed $value, string $message = '', string $exception = '')
133
 * @method static void nullOrInteger(mixed $value, string $message = '', string $exception = '')
134
 * @method static void allInteger(mixed $value, string $message = '', string $exception = '')
135
 * @method static void nullOrIntegerish(mixed $value, string $message = '', string $exception = '')
136
 * @method static void allIntegerish(mixed $value, string $message = '', string $exception = '')
137
 * @method static void nullOrPositiveInteger(mixed $value, string $message = '', string $exception = '')
138
 * @method static void allPositiveInteger(mixed $value, string $message = '', string $exception = '')
139
 * @method static void nullOrFloat(mixed $value, string $message = '', string $exception = '')
140
 * @method static void allFloat(mixed $value, string $message = '', string $exception = '')
141
 * @method static void nullOrNumeric(mixed $value, string $message = '', string $exception = '')
142
 * @method static void allNumeric(mixed $value, string $message = '', string $exception = '')
143
 * @method static void nullOrNatural(mixed $value, string $message = '', string $exception = '')
144
 * @method static void allNatural(mixed $value, string $message = '', string $exception = '')
145
 * @method static void nullOrBoolean(mixed $value, string $message = '', string $exception = '')
146
 * @method static void allBoolean(mixed $value, string $message = '', string $exception = '')
147
 * @method static void nullOrScalar(mixed $value, string $message = '', string $exception = '')
148
 * @method static void allScalar(mixed $value, string $message = '', string $exception = '')
149
 * @method static void nullOrObject(mixed $value, string $message = '', string $exception = '')
150
 * @method static void allObject(mixed $value, string $message = '', string $exception = '')
151
 * @method static void nullOrResource(mixed $value, string|null $type, string $message = '', string $exception = '')
152
 * @method static void allResource(mixed $value, string|null $type, string $message = '', string $exception = '')
153
 * @method static void nullOrIsCallable(mixed $value, string $message = '', string $exception = '')
154
 * @method static void allIsCallable(mixed $value, string $message = '', string $exception = '')
155
 * @method static void nullOrIsArray(mixed $value, string $message = '', string $exception = '')
156
 * @method static void allIsArray(mixed $value, string $message = '', string $exception = '')
157
 * @method static void nullOrIsTraversable(mixed $value, string $message = '', string $exception = '')
158
 * @method static void allIsTraversable(mixed $value, string $message = '', string $exception = '')
159
 * @method static void nullOrIsArrayAccessible(mixed $value, string $message = '', string $exception = '')
160
 * @method static void allIsArrayAccessible(mixed $value, string $message = '', string $exception = '')
161
 * @method static void nullOrIsCountable(mixed $value, string $message = '', string $exception = '')
162
 * @method static void allIsCountable(mixed $value, string $message = '', string $exception = '')
163
 * @method static void nullOrIsIterable(mixed $value, string $message = '', string $exception = '')
164
 * @method static void allIsIterable(mixed $value, string $message = '', string $exception = '')
165
 * @method static void nullOrIsInstanceOf(mixed $value, string|object $class, string $message = '', string $exception = '')
166
 * @method static void allIsInstanceOf(mixed $value, string|object $class, string $message = '', string $exception = '')
167
 * @method static void nullOrNotInstanceOf(mixed $value, string|object $class, string $message = '', string $exception = '')
168
 * @method static void allNotInstanceOf(mixed $value, string|object $class, string $message = '', string $exception = '')
169
 * @method static void nullOrIsInstanceOfAny(mixed $value, array $classes, string $message = '', string $exception = '')
170
 * @method static void allIsInstanceOfAny(mixed $value, array $classes, string $message = '', string $exception = '')
171
 * @method static void nullOrIsAOf(object|string|null $value, string $class, string $message = '', string $exception = '')
172
 * @method static void allIsAOf(object|string|null $value, string $class, string $message = '', string $exception = '')
173
 * @method static void nullOrIsNotA(object|string|null $value, string $class, string $message = '', string $exception = '')
174
 * @method static void allIsNotA(array $value, string $class, string $message = '', string $exception = '')
175
 * @method static void nullOrIsAnyOf(object|string|null $value, string[] $classes, string $message = '', string $exception = '')
176
 * @method static void allIsAnyOf(array $value, string[] $classes, string $message = '', string $exception = '')
177
 * @method static void nullOrIsEmpty(mixed $value, string $message = '', string $exception = '')
178
 * @method static void allIsEmpty(mixed $value, string $message = '', string $exception = '')
179
 * @method static void nullOrNotEmpty(mixed $value, string $message = '', string $exception = '')
180
 * @method static void allNotEmpty(mixed $value, string $message = '', string $exception = '')
181
 * @method static void allNull(mixed $value, string $message = '', string $exception = '')
182
 * @method static void allNotNull(mixed $value, string $message = '', string $exception = '')
183
 * @method static void nullOrTrue(mixed $value, string $message = '', string $exception = '')
184
 * @method static void allTrue(mixed $value, string $message = '', string $exception = '')
185
 * @method static void nullOrFalse(mixed $value, string $message = '', string $exception = '')
186
 * @method static void allFalse(mixed $value, string $message = '', string $exception = '')
187
 * @method static void nullOrNotFalse(mixed $value, string $message = '', string $exception = '')
188
 * @method static void allNotFalse(mixed $value, string $message = '', string $exception = '')
189
 * @method static void nullOrIp(mixed $value, string $message = '', string $exception = '')
190
 * @method static void allIp(mixed $value, string $message = '', string $exception = '')
191
 * @method static void nullOrIpv4(mixed $value, string $message = '', string $exception = '')
192
 * @method static void allIpv4(mixed $value, string $message = '', string $exception = '')
193
 * @method static void nullOrIpv6(mixed $value, string $message = '', string $exception = '')
194
 * @method static void allIpv6(mixed $value, string $message = '', string $exception = '')
195
 * @method static void nullOrEmail(mixed $value, string $message = '', string $exception = '')
196
 * @method static void allEmail(mixed $value, string $message = '', string $exception = '')
197
 * @method static void nullOrUniqueValues(array|null $values, string $message = '', string $exception = '')
198
 * @method static void allUniqueValues(array $values, string $message = '', string $exception = '')
199
 * @method static void nullOrEq(mixed $value, mixed $expect, string $message = '', string $exception = '')
200
 * @method static void allEq(mixed $value, mixed $expect, string $message = '', string $exception = '')
201
 * @method static void nullOrNotEq(mixed $value, mixed $expect, string $message = '', string $exception = '')
202
 * @method static void allNotEq(mixed $value, mixed $expect, string $message = '', string $exception = '')
203
 * @method static void nullOrSame(mixed $value, mixed $expect, string $message = '', string $exception = '')
204
 * @method static void allSame(mixed $value, mixed $expect, string $message = '', string $exception = '')
205
 * @method static void nullOrNotSame(mixed $value, mixed $expect, string $message = '', string $exception = '')
206
 * @method static void allNotSame(mixed $value, mixed $expect, string $message = '', string $exception = '')
207
 * @method static void nullOrGreaterThan(mixed $value, mixed $limit, string $message = '', string $exception = '')
208
 * @method static void allGreaterThan(mixed $value, mixed $limit, string $message = '', string $exception = '')
209
 * @method static void nullOrGreaterThanEq(mixed $value, mixed $limit, string $message = '', string $exception = '')
210
 * @method static void allGreaterThanEq(mixed $value, mixed $limit, string $message = '', string $exception = '')
211
 * @method static void nullOrLessThan(mixed $value, mixed $limit, string $message = '', string $exception = '')
212
 * @method static void allLessThan(mixed $value, mixed $limit, string $message = '', string $exception = '')
213
 * @method static void nullOrLessThanEq(mixed $value, mixed $limit, string $message = '', string $exception = '')
214
 * @method static void allLessThanEq(mixed $value, mixed $limit, string $message = '', string $exception = '')
215
 * @method static void nullOrRange(mixed $value, mixed $min, mixed $max, string $message = '', string $exception = '')
216
 * @method static void allRange(mixed $value, mixed $min, mixed $max, string $message = '', string $exception = '')
217
 * @method static void nullOrOneOf(mixed $value, array $values, string $message = '', string $exception = '')
218
 * @method static void allOneOf(mixed $value, array $values, string $message = '', string $exception = '')
219
 * @method static void nullOrInArray(mixed $value, array $values, string $message = '', string $exception = '')
220
 * @method static void allInArray(mixed $value, array $values, string $message = '', string $exception = '')
221
 * @method static void nullOrContains(string|null $value, string $subString, string $message = '', string $exception = '')
222
 * @method static void allContains(string[] $value, string $subString, string $message = '', string $exception = '')
223
 * @method static void nullOrNotContains(string|null $value, string $subString, string $message = '', string $exception = '')
224
 * @method static void allNotContains(string[] $value, string $subString, string $message = '', string $exception = '')
225
 * @method static void nullOrNotWhitespaceOnly(string|null $value, string $message = '', string $exception = '')
226
 * @method static void allNotWhitespaceOnly(string[] $value, string $message = '', string $exception = '')
227
 * @method static void nullOrStartsWith(string|null $value, string $prefix, string $message = '', string $exception = '')
228
 * @method static void allStartsWith(string[] $value, string $prefix, string $message = '', string $exception = '')
229
 * @method static void nullOrNotStartsWith(string|null $value, string $prefix, string $message = '', string $exception = '')
230
 * @method static void allNotStartsWith(string[] $value, string $prefix, string $message = '', string $exception = '')
231
 * @method static void nullOrStartsWithLetter(mixed $value, string $message = '', string $exception = '')
232
 * @method static void allStartsWithLetter(string[] $value, string $message = '', string $exception = '')
233
 * @method static void nullOrEndsWith(string|null $value, string $suffix, string $message = '', string $exception = '')
234
 * @method static void allEndsWith(string[] $value, string $suffix, string $message = '', string $exception = '')
235
 * @method static void nullOrNotEndsWith(string|null $value, string $suffix, string $message = '', string $exception = '')
236
 * @method static void allNotEndsWith(string[] $value, string $suffix, string $message = '', string $exception = '')
237
 * @method static void nullOrRegex(string|null $value, string $prefix, string $message = '', string $exception = '')
238
 * @method static void allRegex(string[] $value, string $prefix, string $message = '', string $exception = '')
239
 * @method static void nullOrNotRegex(string|null $value, string $prefix, string $message = '', string $exception = '')
240
 * @method static void allNotRegex(string[] $value, string $prefix, string $message = '', string $exception = '')
241
 * @method static void nullOrUnicodeLetters(mixed $value, string $message = '', string $exception = '')
242
 * @method static void allUnicodeLetters(mixed $value, string $message = '', string $exception = '')
243
 * @method static void nullOrAlpha(mixed $value, string $message = '', string $exception = '')
244
 * @method static void allAlpha(string[] $value, string $message = '', string $exception = '')
245
 * @method static void nullOrDigits(string|null $value, string $message = '', string $exception = '')
246
 * @method static void allDigits(string[] $value, string $message = '', string $exception = '')
247
 * @method static void nullOrAlnum(string|null $value, string $message = '', string $exception = '')
248
 * @method static void allAlnum(string[] $value, string $message = '', string $exception = '')
249
 * @method static void nullOrLower(string|null $value, string $message = '', string $exception = '')
250
 * @method static void allLower(string[] $value, string $message = '', string $exception = '')
251
 * @method static void nullOrUpper(string|null $value, string $message = '', string $exception = '')
252
 * @method static void allUpper(string[] $value, string $message = '', string $exception = '')
253
 * @method static void nullOrLength(string|null $value, int $length, string $message = '', string $exception = '')
254
 * @method static void allLength(string[] $value, int $length, string $message = '', string $exception = '')
255
 * @method static void nullOrMinLength(string|null $value, int|float $min, string $message = '', string $exception = '')
256
 * @method static void allMinLength(string[] $value, int|float $min, string $message = '', string $exception = '')
257
 * @method static void nullOrMaxLength(string|null $value, int|float $max, string $message = '', string $exception = '')
258
 * @method static void allMaxLength(string[] $value, int|float $max, string $message = '', string $exception = '')
259
 * @method static void nullOrLengthBetween(string|null $value, int|float $min, int|float $max, string $message = '', string $exception = '')
260
 * @method static void allLengthBetween(string[] $value, int|float $min, int|float $max, string $message = '', string $exception = '')
261
 * @method static void nullOrFileExists(mixed $value, string $message = '', string $exception = '')
262
 * @method static void allFileExists(mixed $value, string $message = '', string $exception = '')
263
 * @method static void nullOrFile(mixed $value, string $message = '', string $exception = '')
264
 * @method static void allFile(mixed $value, string $message = '', string $exception = '')
265
 * @method static void nullOrDirectory(mixed $value, string $message = '', string $exception = '')
266
 * @method static void allDirectory(mixed $value, string $message = '', string $exception = '')
267
 * @method static void nullOrReadable(string|null $value, string $message = '', string $exception = '')
268
 * @method static void allReadable(string[] $value, string $message = '', string $exception = '')
269
 * @method static void nullOrWritable(string|null $value, string $message = '', string $exception = '')
270
 * @method static void allWritable(string[] $value, string $message = '', string $exception = '')
271
 * @method static void nullOrClassExists(mixed $value, string $message = '', string $exception = '')
272
 * @method static void allClassExists(mixed $value, string $message = '', string $exception = '')
273
 * @method static void nullOrSubclassOf(mixed $value, string|object $class, string $message = '', string $exception = '')
274
 * @method static void allSubclassOf(mixed $value, string|object $class, string $message = '', string $exception = '')
275
 * @method static void nullOrInterfaceExists(mixed $value, string $message = '', string $exception = '')
276
 * @method static void allInterfaceExists(mixed $value, string $message = '', string $exception = '')
277
 * @method static void nullOrImplementsInterface(mixed $value, mixed $interface, string $message = '', string $exception = '')
278
 * @method static void allImplementsInterface(mixed $value, mixed $interface, string $message = '', string $exception = '')
279
 * @method static void nullOrPropertyExists(string|object|null $classOrObject, mixed $property, string $message = '', string $exception = '')
280
 * @method static void allPropertyExists(array $classOrObject, mixed $property, string $message = '', string $exception = '')
281
 * @method static void nullOrPropertyNotExists(string|object|null $classOrObject, mixed $property, string $message = '', string $exception = '')
282
 * @method static void allPropertyNotExists(array $classOrObject, mixed $property, string $message = '', string $exception = '')
283
 * @method static void nullOrMethodExists(string|object|null $classOrObject, mixed $method, string $message = '', string $exception = '')
284
 * @method static void allMethodExists(array $classOrObject, mixed $method, string $message = '', string $exception = '')
285
 * @method static void nullOrMethodNotExists(string|object|null $classOrObject, mixed $method, string $message = '', string $exception = '')
286
 * @method static void allMethodNotExists(array $classOrObject, mixed $method, string $message = '', string $exception = '')
287
 * @method static void nullOrKeyExists(array|null $array, string|int $key, string $message = '', string $exception = '')
288
 * @method static void allKeyExists(array $array, string|int $key, string $message = '', string $exception = '')
289
 * @method static void nullOrKeyNotExists(array|null $array, string|int $key, string $message = '', string $exception = '')
290
 * @method static void allKeyNotExists(array $array, string|int $key, string $message = '', string $exception = '')
291
 * @method static void nullOrValidArrayKey(mixed $value, string $message = '', string $exception = '')
292
 * @method static void allValidArrayKey(mixed $value, string $message = '', string $exception = '')
293
 * @method static void nullOrCount(Countable|array|null $array, int $number, string $message = '', string $exception = '')
294
 * @method static void allCount(array $array, int $number, string $message = '', string $exception = '')
295
 * @method static void nullOrMinCount(Countable|array|null $array, int|float $min, string $message = '', string $exception = '')
296
 * @method static void allMinCount(array $array, int|float $min, string $message = '', string $exception = '')
297
 * @method static void nullOrMaxCount(Countable|array|null $array, int|float $max, string $message = '', string $exception = '')
298
 * @method static void allMaxCount(array $array, int|float $max, string $message = '', string $exception = '')
299
 * @method static void nullOrCountBetween(Countable|array|null $array, int|float $min, int|float $max, string $message = '', string $exception = '')
300
 * @method static void allCountBetween(array $array, int|float $min, int|float $max, string $message = '', string $exception = '')
301
 * @method static void nullOrIsList(mixed $array, string $message = '', string $exception = '')
302
 * @method static void allIsList(mixed $array, string $message = '', string $exception = '')
303
 * @method static void nullOrIsNonEmptyList(mixed $array, string $message = '', string $exception = '')
304
 * @method static void allIsNonEmptyList(mixed $array, string $message = '', string $exception = '')
305
 * @method static void nullOrIsMap(mixed $array, string $message = '', string $exception = '')
306
 * @method static void allIsMap(mixed $array, string $message = '', string $exception = '')
307
 * @method static void nullOrIsNonEmptyMap(mixed $array, string $message = '', string $exception = '')
308
 * @method static void allIsNonEmptyMap(mixed $array, string $message = '', string $exception = '')
309
 * @method static void nullOrUuid(string|null $value, string $message = '', string $exception = '')
310
 * @method static void allUuid(string[] $value, string $message = '', string $exception = '')
311
 * @method static void nullOrThrows(Closure|null $expression, string $class, string $message = '', string $exception = '')
312
 * @method static void allThrows(Closure[] $expression, string $class, string $message = '', string $exception = '')
313
 *
314
 * @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
315
 * @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
316
 * @method static void validDuration(mixed $value, string $message = '', string $exception = '')
317
 * @method static void stringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
318
 * @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
319
 * @method static void notInArray(mixed $value, array $values, string $message = '', string $exception = '')
320
 * @method static void validURN(mixed $value, string $message = '', string $exception = '')
321
 * @method static void validURI(mixed $value, string $message = '', string $exception = '')
322
 * @method static void validURL(mixed $value, string $message = '', string $exception = '')
323
 * @method static void validNCName(mixed $value, string $message = '', string $exception = '')
324
 * @method static void validQName(mixed $value, string $message = '', string $exception = '')
325
 * @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
326
 * @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
327
 * @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
328
 * @method static void nullOrStringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
329
 * @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
330
 * @method static void nullOrNotInArray(mixed $value, array $values, string $message = '', string $exception = '')
331
 * @method static void nullOrValidURN(mixed $value, string $message = '', string $exception = '')
332
 * @method static void nullOrValidURI(mixed $value, string $message = '', string $exception = '')
333
 * @method static void nullOrValidURL(mixed $value, string $message = '', string $exception = '')
334
 * @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
335
 * @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
336
 * @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
337
 * @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
338
 * @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
339
 * @method static void allStringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
340
 * @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
341
 * @method static void allNotInArray(mixed $value, array $values, string $message = '', string $exception = '')
342
 * @method static void allValidURN(mixed $value, string $message = '', string $exception = '')
343
 * @method static void allValidURI(mixed $value, string $message = '', string $exception = '')
344
 * @method static void allValidURL(mixed $value, string $message = '', string $exception = '')
345
 * @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
346
 * @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
347
 */
348
final class Assert
349
{
350
    use CustomAssertionTrait;
351
352
353
    /**
354
     * @param string $name
355
     * @param array<mixed> $arguments
356
     */
357
    public static function __callStatic(string $name, array $arguments): void
358
    {
359
        // Handle Exception-parameter
360
        $exception = AssertionFailedException::class;
361
362
        $last = end($arguments);
363
        if (is_string($last) && class_exists($last) && is_subclass_of($last, Throwable::class)) {
364
            $exception = $last;
365
            array_pop($arguments);
366
        }
367
368
        try {
369
            // Putting Webmozart first, since the most calls will be to their native assertions
370
            if (method_exists(Webmozart::class, $name)) {
371
                call_user_func_array([Webmozart::class, $name], $arguments);
372
                return;
373
            } elseif (method_exists(static::class, $name)) {
374
                call_user_func_array([static::class, $name], $arguments);
375
                return;
376
            } elseif (preg_match('/^nullOr(.*)$/i', $name, $matches)) {
377
                $method = lcfirst($matches[1]);
378
                if (method_exists(Webmozart::class, $method)) {
379
                    call_user_func_array([static::class, 'nullOr'], [[Webmozart::class, $method], $arguments]);
380
                } elseif (method_exists(static::class, $method)) {
381
                    call_user_func_array([static::class, 'nullOr'], [[static::class, $method], $arguments]);
382
                } else {
383
                    throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
384
                }
385
            } elseif (preg_match('/^all(.*)$/i', $name, $matches)) {
386
                $method = lcfirst($matches[1]);
387
                if (method_exists(Webmozart::class, $method)) {
388
                    call_user_func_array([static::class, 'all'], [[Webmozart::class, $method], $arguments]);
389
                } elseif (method_exists(static::class, $method)) {
390
                    call_user_func_array([static::class, 'all'], [[static::class, $method], $arguments]);
391
                } else {
392
                    throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $method));
393
                }
394
            } else {
395
                throw new BadMethodCallException(sprintf("Assertion named `%s` does not exists.", $name));
396
            }
397
        } catch (InvalidArgumentException $e) {
398
            throw new $exception($e->getMessage());
399
        }
400
    }
401
402
403
    /**
404
     * Handle nullOr* for either Webmozart or for our custom assertions
405
     *
406
     * @param callable $method
407
     * @param array<mixed> $arguments
408
     * @return void
409
     */
410
    private static function nullOr(callable $method, array $arguments): void
411
    {
412
        $value = reset($arguments);
413
        ($value === null) || call_user_func_array($method, $arguments);
414
    }
415
416
417
    /**
418
     * all* for our custom assertions
419
     *
420
     * @param callable $method
421
     * @param array<mixed> $arguments
422
     * @return void
423
     */
424
    private static function all(callable $method, array $arguments): void
425
    {
426
        $values = array_pop($arguments);
427
        foreach ($values as $value) {
428
            $tmp = $arguments;
429
            array_unshift($tmp, $value);
430
            call_user_func_array($method, $tmp);
431
        }
432
    }
433
434
435
    /**
436
     * @param mixed $value
437
     *
438
     * @return string
439
     */
440
    protected static function valueToString(mixed $value): string
441
    {
442
        if (is_resource($value)) {
443
            return 'resource';
444
        }
445
446
        if (null === $value) {
447
            return 'null';
448
        }
449
450
        if (true === $value) {
451
            return 'true';
452
        }
453
454
        if (false === $value) {
455
            return 'false';
456
        }
457
458
        if (is_array($value)) {
459
            return 'array';
460
        }
461
462
        if (is_object($value)) {
463
            if (method_exists($value, '__toString')) {
464
                return $value::class . ': ' . self::valueToString($value->__toString());
465
            }
466
467
            if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
468
                return $value::class . ': ' . self::valueToString($value->format('c'));
469
            }
470
471
            if (function_exists('enum_exists') && enum_exists(get_class($value))) {
472
                return get_class($value) . '::' . $value->name;
473
            }
474
475
            return $value::class;
476
        }
477
478
        if (is_string($value)) {
479
            return '"' . $value . '"';
480
        }
481
482
        return strval($value);
483
    }
484
}
485