Failed Conditions
Pull Request — master (#175)
by Gert de
02:39
created

Assert::typeToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/assert package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Assert;
13
14
use ArrayAccess;
15
use BadMethodCallException;
16
use Closure;
17
use Countable;
18
use Exception;
19
use InvalidArgumentException;
20
use ResourceBundle;
21
use SimpleXMLElement;
22
use Throwable;
23
use Traversable;
24
25
/**
26
 * Efficient assertions to validate the input/output of your methods.
27
 *
28
 * @method static void nullOrString($value, $message = '')
29
 * @method static void nullOrStringNotEmpty($value, $message = '')
30
 * @method static void nullOrInteger($value, $message = '')
31
 * @method static void nullOrIntegerish($value, $message = '')
32
 * @method static void nullOrFloat($value, $message = '')
33
 * @method static void nullOrNumeric($value, $message = '')
34
 * @method static void nullOrNatural($value, $message = '')
35
 * @method static void nullOrBoolean($value, $message = '')
36
 * @method static void nullOrScalar($value, $message = '')
37
 * @method static void nullOrObject($value, $message = '')
38
 * @method static void nullOrResource($value, $type = null, $message = '')
39
 * @method static void nullOrIsCallable($value, $message = '')
40
 * @method static void nullOrIsArray($value, $message = '')
41
 * @method static void nullOrIsTraversable($value, $message = '')
42
 * @method static void nullOrIsArrayAccessible($value, $message = '')
43
 * @method static void nullOrIsCountable($value, $message = '')
44
 * @method static void nullOrIsIterable($value, $message = '')
45
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
46
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
47
 * @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
48
 * @method static void nullOrIsAOf($value, $classes, $message = '')
49
 * @method static void nullOrIsAnyOf($value, $classes, $message = '')
50
 * @method static void nullOrIsNotA($value, $classes, $message = '')
51
 * @method static void nullOrIsEmpty($value, $message = '')
52
 * @method static void nullOrNotEmpty($value, $message = '')
53
 * @method static void nullOrTrue($value, $message = '')
54
 * @method static void nullOrFalse($value, $message = '')
55
 * @method static void nullOrNotFalse($value, $message = '')
56
 * @method static void nullOrIp($value, $message = '')
57
 * @method static void nullOrIpv4($value, $message = '')
58
 * @method static void nullOrIpv6($value, $message = '')
59
 * @method static void nullOrEmail($value, $message = '')
60
 * @method static void nullOrUniqueValues($values, $message = '')
61
 * @method static void nullOrEq($value, $expect, $message = '')
62
 * @method static void nullOrNotEq($value, $expect, $message = '')
63
 * @method static void nullOrSame($value, $expect, $message = '')
64
 * @method static void nullOrNotSame($value, $expect, $message = '')
65
 * @method static void nullOrGreaterThan($value, $limit, $message = '')
66
 * @method static void nullOrGreaterThanEq($value, $limit, $message = '')
67
 * @method static void nullOrLessThan($value, $limit, $message = '')
68
 * @method static void nullOrLessThanEq($value, $limit, $message = '')
69
 * @method static void nullOrRange($value, $min, $max, $message = '')
70
 * @method static void nullOrOneOf($value, $values, $message = '')
71
 * @method static void nullOrContains($value, $subString, $message = '')
72
 * @method static void nullOrNotContains($value, $subString, $message = '')
73
 * @method static void nullOrNotWhitespaceOnly($value, $message = '')
74
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
75
 * @method static void nullOrNotStartsWith($value, $prefix, $message = '')
76
 * @method static void nullOrStartsWithLetter($value, $message = '')
77
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
78
 * @method static void nullOrNotEndsWith($value, $suffix, $message = '')
79
 * @method static void nullOrRegex($value, $pattern, $message = '')
80
 * @method static void nullOrNotRegex($value, $pattern, $message = '')
81
 * @method static void nullOrUnicodeLetters($value, $message = '')
82
 * @method static void nullOrAlpha($value, $message = '')
83
 * @method static void nullOrDigits($value, $message = '')
84
 * @method static void nullOrAlnum($value, $message = '')
85
 * @method static void nullOrLower($value, $message = '')
86
 * @method static void nullOrUpper($value, $message = '')
87
 * @method static void nullOrLength($value, $length, $message = '')
88
 * @method static void nullOrMinLength($value, $min, $message = '')
89
 * @method static void nullOrMaxLength($value, $max, $message = '')
90
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
91
 * @method static void nullOrFileExists($value, $message = '')
92
 * @method static void nullOrFile($value, $message = '')
93
 * @method static void nullOrDirectory($value, $message = '')
94
 * @method static void nullOrReadable($value, $message = '')
95
 * @method static void nullOrWritable($value, $message = '')
96
 * @method static void nullOrClassExists($value, $message = '')
97
 * @method static void nullOrSubclassOf($value, $class, $message = '')
98
 * @method static void nullOrInterfaceExists($value, $message = '')
99
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
100
 * @method static void nullOrPropertyExists($value, $property, $message = '')
101
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
102
 * @method static void nullOrMethodExists($value, $method, $message = '')
103
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
104
 * @method static void nullOrKeyExists($value, $key, $message = '')
105
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
106
 * @method static void nullOrValidArrayKey($value, $message = '')
107
 * @method static void nullOrCount($value, $key, $message = '')
108
 * @method static void nullOrMinCount($value, $min, $message = '')
109
 * @method static void nullOrMaxCount($value, $max, $message = '')
110
 * @method static void nullOrIsList($value, $message = '')
111
 * @method static void nullOrIsNonEmptyList($value, $message = '')
112
 * @method static void nullOrIsMap($value, $message = '')
113
 * @method static void nullOrIsNonEmptyMap($value, $message = '')
114
 * @method static void nullOrCountBetween($value, $min, $max, $message = '')
115
 * @method static void nullOrUuid($values, $message = '')
116
 * @method static void nullOrThrows($expression, $class = 'Exception', $message = '')
117
 * @method static void allString($values, $message = '')
118
 * @method static void allStringNotEmpty($values, $message = '')
119
 * @method static void allInteger($values, $message = '')
120
 * @method static void allIntegerish($values, $message = '')
121
 * @method static void allFloat($values, $message = '')
122
 * @method static void allNumeric($values, $message = '')
123
 * @method static void allNatural($values, $message = '')
124
 * @method static void allBoolean($values, $message = '')
125
 * @method static void allScalar($values, $message = '')
126
 * @method static void allObject($values, $message = '')
127
 * @method static void allResource($values, $type = null, $message = '')
128
 * @method static void allIsCallable($values, $message = '')
129
 * @method static void allIsArray($values, $message = '')
130
 * @method static void allIsTraversable($values, $message = '')
131
 * @method static void allIsArrayAccessible($values, $message = '')
132
 * @method static void allIsCountable($values, $message = '')
133
 * @method static void allIsIterable($values, $message = '')
134
 * @method static void allIsInstanceOf($values, $class, $message = '')
135
 * @method static void allNotInstanceOf($values, $class, $message = '')
136
 * @method static void allIsInstanceOfAny($values, $classes, $message = '')
137
 * @method static void allIsAOf($values, $class, $message = '')
138
 * @method static void allIsAnyOf($values, $class, $message = '')
139
 * @method static void allIsNotA($values, $class, $message = '')
140
 * @method static void allNull($values, $message = '')
141
 * @method static void allNotNull($values, $message = '')
142
 * @method static void allIsEmpty($values, $message = '')
143
 * @method static void allNotEmpty($values, $message = '')
144
 * @method static void allTrue($values, $message = '')
145
 * @method static void allFalse($values, $message = '')
146
 * @method static void allNotFalse($values, $message = '')
147
 * @method static void allIp($values, $message = '')
148
 * @method static void allIpv4($values, $message = '')
149
 * @method static void allIpv6($values, $message = '')
150
 * @method static void allEmail($values, $message = '')
151
 * @method static void allUniqueValues($values, $message = '')
152
 * @method static void allEq($values, $expect, $message = '')
153
 * @method static void allNotEq($values, $expect, $message = '')
154
 * @method static void allSame($values, $expect, $message = '')
155
 * @method static void allNotSame($values, $expect, $message = '')
156
 * @method static void allGreaterThan($values, $limit, $message = '')
157
 * @method static void allGreaterThanEq($values, $limit, $message = '')
158
 * @method static void allLessThan($values, $limit, $message = '')
159
 * @method static void allLessThanEq($values, $limit, $message = '')
160
 * @method static void allRange($values, $min, $max, $message = '')
161
 * @method static void allOneOf($values, $values, $message = '')
162
 * @method static void allContains($values, $subString, $message = '')
163
 * @method static void allNotContains($values, $subString, $message = '')
164
 * @method static void allNotWhitespaceOnly($values, $message = '')
165
 * @method static void allStartsWith($values, $prefix, $message = '')
166
 * @method static void allNotStartsWith($values, $prefix, $message = '')
167
 * @method static void allStartsWithLetter($values, $message = '')
168
 * @method static void allEndsWith($values, $suffix, $message = '')
169
 * @method static void allNotEndsWith($values, $suffix, $message = '')
170
 * @method static void allRegex($values, $pattern, $message = '')
171
 * @method static void allNotRegex($values, $pattern, $message = '')
172
 * @method static void allUnicodeLetters($values, $message = '')
173
 * @method static void allAlpha($values, $message = '')
174
 * @method static void allDigits($values, $message = '')
175
 * @method static void allAlnum($values, $message = '')
176
 * @method static void allLower($values, $message = '')
177
 * @method static void allUpper($values, $message = '')
178
 * @method static void allLength($values, $length, $message = '')
179
 * @method static void allMinLength($values, $min, $message = '')
180
 * @method static void allMaxLength($values, $max, $message = '')
181
 * @method static void allLengthBetween($values, $min, $max, $message = '')
182
 * @method static void allFileExists($values, $message = '')
183
 * @method static void allFile($values, $message = '')
184
 * @method static void allDirectory($values, $message = '')
185
 * @method static void allReadable($values, $message = '')
186
 * @method static void allWritable($values, $message = '')
187
 * @method static void allClassExists($values, $message = '')
188
 * @method static void allSubclassOf($values, $class, $message = '')
189
 * @method static void allInterfaceExists($values, $message = '')
190
 * @method static void allImplementsInterface($values, $interface, $message = '')
191
 * @method static void allPropertyExists($values, $property, $message = '')
192
 * @method static void allPropertyNotExists($values, $property, $message = '')
193
 * @method static void allMethodExists($values, $method, $message = '')
194
 * @method static void allMethodNotExists($values, $method, $message = '')
195
 * @method static void allKeyExists($values, $key, $message = '')
196
 * @method static void allKeyNotExists($values, $key, $message = '')
197
 * @method static void allValidArrayKey($values, $message = '')
198
 * @method static void allCount($values, $key, $message = '')
199
 * @method static void allMinCount($values, $min, $message = '')
200
 * @method static void allMaxCount($values, $max, $message = '')
201
 * @method static void allCountBetween($values, $min, $max, $message = '')
202
 * @method static void allIsList($values, $message = '')
203
 * @method static void allIsNonEmptyList($values, $message = '')
204
 * @method static void allIsMap($values, $message = '')
205
 * @method static void allIsNonEmptyMap($values, $message = '')
206
 * @method static void allUuid($values, $message = '')
207
 * @method static void allThrows($expressions, $class = 'Exception', $message = '')
208
 *
209
 * @since  1.0
210
 *
211
 * @author Bernhard Schussek <[email protected]>
212
 */
213
class Assert
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
214
{
215
    /**
216
     * @psalm-pure
217
     * @psalm-assert string $value
218
     *
219
     * @param mixed  $value
220
     * @param string $message
221
     *
222
     * @throws InvalidArgumentException
223
     */
224 241
    public static function string($value, $message = '')
225
    {
226 241
        if (!\is_string($value)) {
227 45
            static::reportInvalidArgument(\sprintf(
228 45
                $message ?: 'Expected a string. Got: %s',
229 45
                static::typeToString($value)
230
            ));
231
        }
232 196
    }
233
234
    /**
235
     * @psalm-pure
236
     * @psalm-assert string $value
237
     * @psalm-assert !empty $value
238
     *
239
     * @param mixed  $value
240
     * @param string $message
241
     *
242
     * @throws InvalidArgumentException
243
     */
244 16
    public static function stringNotEmpty($value, $message = '')
245
    {
246 16
        static::string($value, $message);
247 12
        static::notEq($value, '', $message);
248 8
    }
249
250
    /**
251
     * @psalm-pure
252
     * @psalm-assert int $value
253
     *
254
     * @param mixed  $value
255
     * @param string $message
256
     *
257
     * @throws InvalidArgumentException
258
     */
259 17
    public static function integer($value, $message = '')
260
    {
261 17
        if (!\is_int($value)) {
262 13
            static::reportInvalidArgument(\sprintf(
263 13
                $message ?: 'Expected an integer. Got: %s',
264 13
                static::typeToString($value)
265
            ));
266
        }
267 4
    }
268
269
    /**
270
     * @psalm-pure
271
     * @psalm-assert numeric $value
272
     *
273
     * @param mixed  $value
274
     * @param string $message
275
     *
276
     * @throws InvalidArgumentException
277
     */
278 16
    public static function integerish($value, $message = '')
279
    {
280 16
        if (!\is_numeric($value) || $value != (int) $value) {
281 4
            static::reportInvalidArgument(\sprintf(
282 4
                $message ?: 'Expected an integerish value. Got: %s',
283 4
                static::typeToString($value)
284
            ));
285
        }
286 12
    }
287
288
    /**
289
     * @psalm-pure
290
     * @psalm-assert float $value
291
     *
292
     * @param mixed  $value
293
     * @param string $message
294
     *
295
     * @throws InvalidArgumentException
296
     */
297 16
    public static function float($value, $message = '')
298
    {
299 16
        if (!\is_float($value)) {
300 8
            static::reportInvalidArgument(\sprintf(
301 8
                $message ?: 'Expected a float. Got: %s',
302 8
                static::typeToString($value)
303
            ));
304
        }
305 8
    }
306
307
    /**
308
     * @psalm-pure
309
     * @psalm-assert numeric $value
310
     *
311
     * @param mixed  $value
312
     * @param string $message
313
     *
314
     * @throws InvalidArgumentException
315
     */
316 20
    public static function numeric($value, $message = '')
317
    {
318 20
        if (!\is_numeric($value)) {
319 4
            static::reportInvalidArgument(\sprintf(
320 4
                $message ?: 'Expected a numeric. Got: %s',
321 4
                static::typeToString($value)
322
            ));
323
        }
324 16
    }
325
326
    /**
327
     * @psalm-pure
328
     * @psalm-assert int $value
329
     *
330
     * @param mixed  $value
331
     * @param string $message
332
     *
333
     * @throws InvalidArgumentException
334
     */
335 24
    public static function natural($value, $message = '')
336
    {
337 24
        if (!\is_int($value) || $value < 0) {
338 16
            static::reportInvalidArgument(\sprintf(
339 16
                $message ?: 'Expected a non-negative integer. Got %s',
340 16
                static::valueToString($value)
341
            ));
342
        }
343 8
    }
344
345
    /**
346
     * @psalm-pure
347
     * @psalm-assert bool $value
348
     *
349
     * @param mixed  $value
350
     * @param string $message
351
     *
352
     * @throws InvalidArgumentException
353
     */
354 16
    public static function boolean($value, $message = '')
355
    {
356 16
        if (!\is_bool($value)) {
357 8
            static::reportInvalidArgument(\sprintf(
358 8
                $message ?: 'Expected a boolean. Got: %s',
359 8
                static::typeToString($value)
360
            ));
361
        }
362 8
    }
363
364
    /**
365
     * @psalm-pure
366
     * @psalm-assert scalar $value
367
     *
368
     * @param mixed  $value
369
     * @param string $message
370
     *
371
     * @throws InvalidArgumentException
372
     */
373 23
    public static function scalar($value, $message = '')
374
    {
375 23
        if (!\is_scalar($value)) {
376 11
            static::reportInvalidArgument(\sprintf(
377 11
                $message ?: 'Expected a scalar. Got: %s',
378 11
                static::typeToString($value)
379
            ));
380
        }
381 12
    }
382
383
    /**
384
     * @psalm-pure
385
     * @psalm-assert object $value
386
     *
387
     * @param mixed  $value
388
     * @param string $message
389
     *
390
     * @throws InvalidArgumentException
391
     */
392 23
    public static function object($value, $message = '')
393
    {
394 23
        if (!\is_object($value)) {
395 15
            static::reportInvalidArgument(\sprintf(
396 15
                $message ?: 'Expected an object. Got: %s',
397 15
                static::typeToString($value)
398
            ));
399
        }
400 8
    }
401
402
    /**
403
     * @psalm-pure
404
     * @psalm-assert resource $value
405
     *
406
     * @param mixed       $value
407
     * @param string|null $type    type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php
408
     * @param string      $message
409
     *
410
     * @throws InvalidArgumentException
411
     */
412 16
    public static function resource($value, $type = null, $message = '')
413
    {
414 16
        if (!\is_resource($value)) {
415 4
            static::reportInvalidArgument(\sprintf(
416 4
                $message ?: 'Expected a resource. Got: %s',
417 4
                static::typeToString($value)
418
            ));
419
        }
420
421 12
        if ($type && $type !== \get_resource_type($value)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
422 4
            static::reportInvalidArgument(\sprintf(
423 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
424 4
                static::typeToString($value),
425 4
                $type
426
            ));
427
        }
428 8
    }
429
430
    /**
431
     * @psalm-pure
432
     * @psalm-assert callable $value
433
     *
434
     * @param mixed  $value
435
     * @param string $message
436
     *
437
     * @throws InvalidArgumentException
438
     */
439 20
    public static function isCallable($value, $message = '')
440
    {
441 20
        if (!\is_callable($value)) {
442 8
            static::reportInvalidArgument(\sprintf(
443 8
                $message ?: 'Expected a callable. Got: %s',
444 8
                static::typeToString($value)
445
            ));
446
        }
447 12
    }
448
449
    /**
450
     * @psalm-pure
451
     * @psalm-assert array $value
452
     *
453
     * @param mixed  $value
454
     * @param string $message
455
     *
456
     * @throws InvalidArgumentException
457
     */
458 20
    public static function isArray($value, $message = '')
459
    {
460 20
        if (!\is_array($value)) {
461 12
            static::reportInvalidArgument(\sprintf(
462 12
                $message ?: 'Expected an array. Got: %s',
463 12
                static::typeToString($value)
464
            ));
465
        }
466 8
    }
467
468
    /**
469
     * @psalm-pure
470
     * @psalm-assert iterable $value
471
     *
472
     * @deprecated use "isIterable" or "isInstanceOf" instead
473
     *
474
     * @param mixed  $value
475
     * @param string $message
476
     *
477
     * @throws InvalidArgumentException
478
     */
479 20
    public static function isTraversable($value, $message = '')
480
    {
481 20
        @\trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
482 20
            \sprintf(
483 20
                'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.',
484 20
                __METHOD__
485
            ),
486 20
            \E_USER_DEPRECATED
487
        );
488
489 20
        if (!\is_array($value) && !($value instanceof Traversable)) {
490 8
            static::reportInvalidArgument(\sprintf(
491 8
                $message ?: 'Expected a traversable. Got: %s',
492 8
                static::typeToString($value)
493
            ));
494
        }
495 12
    }
496
497
    /**
498
     * @psalm-pure
499
     * @psalm-assert array|ArrayAccess $value
500
     *
501
     * @param mixed  $value
502
     * @param string $message
503
     *
504
     * @throws InvalidArgumentException
505
     */
506 20 View Code Duplication
    public static function isArrayAccessible($value, $message = '')
507
    {
508 20
        if (!\is_array($value) && !($value instanceof ArrayAccess)) {
509 8
            static::reportInvalidArgument(\sprintf(
510 8
                $message ?: 'Expected an array accessible. Got: %s',
511 8
                static::typeToString($value)
512
            ));
513
        }
514 12
    }
515
516
    /**
517
     * @psalm-pure
518
     * @psalm-assert countable $value
519
     *
520
     * @param mixed  $value
521
     * @param string $message
522
     *
523
     * @throws InvalidArgumentException
524
     */
525 28
    public static function isCountable($value, $message = '')
526
    {
527
        if (
528 28
            !\is_array($value)
529 28
            && !($value instanceof Countable)
530 28
            && !($value instanceof ResourceBundle)
0 ignored issues
show
Bug introduced by
The class ResourceBundle does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
531 28
            && !($value instanceof SimpleXMLElement)
532
        ) {
533 12
            static::reportInvalidArgument(\sprintf(
534 12
                $message ?: 'Expected a countable. Got: %s',
535 12
                static::typeToString($value)
536
            ));
537
        }
538 16
    }
539
540
    /**
541
     * @psalm-pure
542
     * @psalm-assert iterable $value
543
     *
544
     * @param mixed  $value
545
     * @param string $message
546
     *
547
     * @throws InvalidArgumentException
548
     */
549 1040 View Code Duplication
    public static function isIterable($value, $message = '')
550
    {
551 1040
        if (!\is_array($value) && !($value instanceof Traversable)) {
552 8
            static::reportInvalidArgument(\sprintf(
553 8
                $message ?: 'Expected an iterable. Got: %s',
554 8
                static::typeToString($value)
555
            ));
556
        }
557 1036
    }
558
559
    /**
560
     * @psalm-pure
561
     * @psalm-template ExpectedType of object
562
     * @psalm-param class-string<ExpectedType> $class
563
     * @psalm-assert ExpectedType $value
564
     *
565
     * @param mixed         $value
566
     * @param string|object $class
567
     * @param string        $message
568
     *
569
     * @throws InvalidArgumentException
570
     */
571 19
    public static function isInstanceOf($value, $class, $message = '')
572
    {
573 19
        if (!($value instanceof $class)) {
574 15
            static::reportInvalidArgument(\sprintf(
575 15
                $message ?: 'Expected an instance of %2$s. Got: %s',
576 15
                static::typeToString($value),
577 15
                $class
578
            ));
579
        }
580 4
    }
581
582
    /**
583
     * @psalm-pure
584
     * @psalm-template ExpectedType of object
585
     * @psalm-param class-string<ExpectedType> $class
586
     * @psalm-assert !ExpectedType $value
587
     *
588
     * @param mixed         $value
589
     * @param string|object $class
590
     * @param string        $message
591
     *
592
     * @throws InvalidArgumentException
593
     */
594 16
    public static function notInstanceOf($value, $class, $message = '')
595
    {
596 16
        if ($value instanceof $class) {
597 4
            static::reportInvalidArgument(\sprintf(
598 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
599 4
                static::typeToString($value),
600 4
                $class
601
            ));
602
        }
603 12
    }
604
605
    /**
606
     * @psalm-pure
607
     * @psalm-param array<class-string> $classes
608
     *
609
     * @param mixed                $value
610
     * @param array<object|string> $classes
611
     * @param string               $message
612
     *
613
     * @throws InvalidArgumentException
614
     */
615 20
    public static function isInstanceOfAny($value, array $classes, $message = '')
616
    {
617 20
        foreach ($classes as $class) {
618 20
            if ($value instanceof $class) {
619 8
                return;
620
            }
621
        }
622
623 12
        static::reportInvalidArgument(\sprintf(
624 12
            $message ?: 'Expected an instance of any of %2$s. Got: %s',
625 12
            static::typeToString($value),
626 12
            \implode(', ', \array_map(array('static', 'valueToString'), $classes))
627
        ));
628
    }
629
630
    /**
631
     * @psalm-pure
632
     * @psalm-template ExpectedType of object
633
     * @psalm-param class-string<ExpectedType> $class
634
     * @psalm-assert ExpectedType|class-string<ExpectedType> $value
635
     *
636
     * @param object|string $value
637
     * @param string        $class
638
     * @param string        $message
639
     *
640
     * @throws InvalidArgumentException
641
     */
642 20 View Code Duplication
    public static function isAOf($value, $class, $message = '')
643
    {
644 20
        static::string($class, 'Expected class as a string. Got: %s');
645
646 16
        if (!\is_a($value, $class, \is_string($value))) {
647 12
            static::reportInvalidArgument(sprintf(
648 12
                $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s',
649 12
                static::typeToString($value),
650
                $class
651
            ));
652
        }
653 4
    }
654
655
    /**
656
     * @psalm-pure
657
     * @psalm-template UnexpectedType of object
658
     * @psalm-param class-string<UnexpectedType> $class
659
     * @psalm-assert !UnexpectedType $value
660
     * @psalm-assert !class-string<UnexpectedType> $value
661
     *
662
     * @param object|string $value
663
     * @param string        $class
664
     * @param string        $message
665
     *
666
     * @throws InvalidArgumentException
667
     */
668 20 View Code Duplication
    public static function isNotA($value, $class, $message = '')
669
    {
670 20
        static::string($class, 'Expected class as a string. Got: %s');
671
672 16
        if (\is_a($value, $class, \is_string($value))) {
673 4
            static::reportInvalidArgument(sprintf(
674 4
                $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s',
675 4
                static::typeToString($value),
676
                $class
677
            ));
678
        }
679 12
    }
680
681
    /**
682
     * @psalm-pure
683
     * @psalm-param array<class-string> $classes
684
     *
685
     * @param object|string $value
686
     * @param string[]      $classes
687
     * @param string        $message
688
     *
689
     * @throws InvalidArgumentException
690
     */
691 24
    public static function isAnyOf($value, array $classes, $message = '')
692
    {
693 24
        foreach ($classes as $class) {
694 24
            static::string($class, 'Expected class as a string. Got: %s');
695
696 20
            if (\is_a($value, $class, \is_string($value))) {
697 8
                return;
698
            }
699
        }
700
701 12
        static::reportInvalidArgument(sprintf(
702 12
            $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s',
703 12
            static::typeToString($value),
704 12
            \implode(', ', \array_map(array('static', 'valueToString'), $classes))
705
        ));
706
    }
707
708
    /**
709
     * @psalm-pure
710
     * @psalm-assert empty $value
711
     *
712
     * @param mixed  $value
713
     * @param string $message
714
     *
715
     * @throws InvalidArgumentException
716
     */
717 23
    public static function isEmpty($value, $message = '')
718
    {
719 23
        if (!empty($value)) {
720 8
            static::reportInvalidArgument(\sprintf(
721 8
                $message ?: 'Expected an empty value. Got: %s',
722 8
                static::valueToString($value)
723
            ));
724
        }
725 15
    }
726
727
    /**
728
     * @psalm-pure
729
     * @psalm-assert !empty $value
730
     *
731
     * @param mixed  $value
732
     * @param string $message
733
     *
734
     * @throws InvalidArgumentException
735
     */
736 55
    public static function notEmpty($value, $message = '')
737
    {
738 55
        if (empty($value)) {
739 23
            static::reportInvalidArgument(\sprintf(
740 23
                $message ?: 'Expected a non-empty value. Got: %s',
741 23
                static::valueToString($value)
742
            ));
743
        }
744 32
    }
745
746
    /**
747
     * @psalm-pure
748
     * @psalm-assert null $value
749
     *
750
     * @param mixed  $value
751
     * @param string $message
752
     *
753
     * @throws InvalidArgumentException
754
     */
755 11
    public static function null($value, $message = '')
756
    {
757 11
        if (null !== $value) {
758 8
            static::reportInvalidArgument(\sprintf(
759 8
                $message ?: 'Expected null. Got: %s',
760 8
                static::valueToString($value)
761
            ));
762
        }
763 3
    }
764
765
    /**
766
     * @psalm-pure
767
     * @psalm-assert !null $value
768
     *
769
     * @param mixed  $value
770
     * @param string $message
771
     *
772
     * @throws InvalidArgumentException
773
     */
774 11
    public static function notNull($value, $message = '')
775
    {
776 11
        if (null === $value) {
777 3
            static::reportInvalidArgument(
778 3
                $message ?: 'Expected a value other than null.'
779
            );
780
        }
781 8
    }
782
783
    /**
784
     * @psalm-pure
785
     * @psalm-assert true $value
786
     *
787
     * @param mixed  $value
788
     * @param string $message
789
     *
790
     * @throws InvalidArgumentException
791
     */
792 15
    public static function true($value, $message = '')
793
    {
794 15
        if (true !== $value) {
795 11
            static::reportInvalidArgument(\sprintf(
796 11
                $message ?: 'Expected a value to be true. Got: %s',
797 11
                static::valueToString($value)
798
            ));
799
        }
800 4
    }
801
802
    /**
803
     * @psalm-pure
804
     * @psalm-assert false $value
805
     *
806
     * @param mixed  $value
807
     * @param string $message
808
     *
809
     * @throws InvalidArgumentException
810
     */
811 19
    public static function false($value, $message = '')
812
    {
813 19
        if (false !== $value) {
814 15
            static::reportInvalidArgument(\sprintf(
815 15
                $message ?: 'Expected a value to be false. Got: %s',
816 15
                static::valueToString($value)
817
            ));
818
        }
819 4
    }
820
821
    /**
822
     * @psalm-assert !false $value
823
     *
824
     * @param mixed  $value
825
     * @param string $message
826
     *
827
     * @throws InvalidArgumentException
828
     */
829 19
    public static function notFalse($value, $message = '')
830
    {
831 19
        if (false === $value) {
832 4
            static::reportInvalidArgument(
833 4
                $message ?: 'Expected a value other than false.'
834
            );
835
        }
836 15
    }
837
838
    /**
839
     * @param mixed  $value
840
     * @param string $message
841
     *
842
     * @throws InvalidArgumentException
843
     */
844 51 View Code Duplication
    public static function ip($value, $message = '')
845
    {
846 51
        if (false === \filter_var($value, \FILTER_VALIDATE_IP)) {
847 19
            static::reportInvalidArgument(\sprintf(
848 19
                $message ?: 'Expected a value to be an IP. Got: %s',
849 19
                static::valueToString($value)
850
            ));
851
        }
852 32
    }
853
854
    /**
855
     * @param mixed  $value
856
     * @param string $message
857
     *
858
     * @throws InvalidArgumentException
859
     */
860 51 View Code Duplication
    public static function ipv4($value, $message = '')
861
    {
862 51
        if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
863 35
            static::reportInvalidArgument(\sprintf(
864 35
                $message ?: 'Expected a value to be an IPv4. Got: %s',
865 35
                static::valueToString($value)
866
            ));
867
        }
868 16
    }
869
870
    /**
871
     * @param mixed  $value
872
     * @param string $message
873
     *
874
     * @throws InvalidArgumentException
875
     */
876 51 View Code Duplication
    public static function ipv6($value, $message = '')
877
    {
878 51
        if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
879 31
            static::reportInvalidArgument(\sprintf(
880 31
                $message ?: 'Expected a value to be an IPv6. Got %s',
881 31
                static::valueToString($value)
882
            ));
883
        }
884 20
    }
885
886
    /**
887
     * @param mixed  $value
888
     * @param string $message
889
     *
890
     * @throws InvalidArgumentException
891
     */
892 20 View Code Duplication
    public static function email($value, $message = '')
893
    {
894 20
        if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) {
895 12
            static::reportInvalidArgument(\sprintf(
896 12
                $message ?: 'Expected a value to be a valid e-mail address. Got %s',
897 12
                static::valueToString($value)
898
            ));
899
        }
900 8
    }
901
902
    /**
903
     * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion.
904
     *
905
     * @param array  $values
906
     * @param string $message
907
     *
908
     * @throws InvalidArgumentException
909
     */
910 12
    public static function uniqueValues(array $values, $message = '')
911
    {
912 12
        $allValues = \count($values);
913 12
        $uniqueValues = \count(\array_unique($values));
914
915 12
        if ($allValues !== $uniqueValues) {
916 8
            $difference = $allValues - $uniqueValues;
917
918 8
            static::reportInvalidArgument(\sprintf(
919 8
                $message ?: 'Expected an array of unique values, but %s of them %s duplicated',
920 8
                $difference,
921 8
                (1 === $difference ? 'is' : 'are')
922
            ));
923
        }
924 4
    }
925
926
    /**
927
     * @param mixed  $value
928
     * @param mixed  $expect
929
     * @param string $message
930
     *
931
     * @throws InvalidArgumentException
932
     */
933 33
    public static function eq($value, $expect, $message = '')
934
    {
935 33
        if ($expect != $value) {
936 17
            static::reportInvalidArgument(\sprintf(
937 17
                $message ?: 'Expected a value equal to %2$s. Got: %s',
938 17
                static::valueToString($value),
939 17
                static::valueToString($expect)
940
            ));
941
        }
942 16
    }
943
944
    /**
945
     * @param mixed  $value
946
     * @param mixed  $expect
947
     * @param string $message
948
     *
949
     * @throws InvalidArgumentException
950
     */
951 28
    public static function notEq($value, $expect, $message = '')
952
    {
953 28
        if ($expect == $value) {
954 16
            static::reportInvalidArgument(\sprintf(
955 16
                $message ?: 'Expected a different value than %s.',
956 16
                static::valueToString($expect)
957
            ));
958
        }
959 12
    }
960
961
    /**
962
     * @psalm-pure
963
     *
964
     * @param mixed  $value
965
     * @param mixed  $expect
966
     * @param string $message
967
     *
968
     * @throws InvalidArgumentException
969
     */
970 16
    public static function same($value, $expect, $message = '')
971
    {
972 16
        if ($expect !== $value) {
973 12
            static::reportInvalidArgument(\sprintf(
974 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
975 12
                static::valueToString($value),
976 12
                static::valueToString($expect)
977
            ));
978
        }
979 4
    }
980
981
    /**
982
     * @psalm-pure
983
     *
984
     * @param mixed  $value
985
     * @param mixed  $expect
986
     * @param string $message
987
     *
988
     * @throws InvalidArgumentException
989
     */
990 16
    public static function notSame($value, $expect, $message = '')
991
    {
992 16
        if ($expect === $value) {
993 4
            static::reportInvalidArgument(\sprintf(
994 4
                $message ?: 'Expected a value not identical to %s.',
995 4
                static::valueToString($expect)
996
            ));
997
        }
998 12
    }
999
1000
    /**
1001
     * @psalm-pure
1002
     *
1003
     * @param mixed  $value
1004
     * @param mixed  $limit
1005
     * @param string $message
1006
     *
1007
     * @throws InvalidArgumentException
1008
     */
1009 8
    public static function greaterThan($value, $limit, $message = '')
1010
    {
1011 8
        if ($value <= $limit) {
1012 4
            static::reportInvalidArgument(\sprintf(
1013 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
1014 4
                static::valueToString($value),
1015 4
                static::valueToString($limit)
1016
            ));
1017
        }
1018 4
    }
1019
1020
    /**
1021
     * @psalm-pure
1022
     *
1023
     * @param mixed  $value
1024
     * @param mixed  $limit
1025
     * @param string $message
1026
     *
1027
     * @throws InvalidArgumentException
1028
     */
1029 12
    public static function greaterThanEq($value, $limit, $message = '')
1030
    {
1031 12
        if ($value < $limit) {
1032 4
            static::reportInvalidArgument(\sprintf(
1033 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
1034 4
                static::valueToString($value),
1035 4
                static::valueToString($limit)
1036
            ));
1037
        }
1038 8
    }
1039
1040
    /**
1041
     * @psalm-pure
1042
     *
1043
     * @param mixed  $value
1044
     * @param mixed  $limit
1045
     * @param string $message
1046
     *
1047
     * @throws InvalidArgumentException
1048
     */
1049 8
    public static function lessThan($value, $limit, $message = '')
1050
    {
1051 8
        if ($value >= $limit) {
1052 4
            static::reportInvalidArgument(\sprintf(
1053 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
1054 4
                static::valueToString($value),
1055 4
                static::valueToString($limit)
1056
            ));
1057
        }
1058 4
    }
1059
1060
    /**
1061
     * @psalm-pure
1062
     *
1063
     * @param mixed  $value
1064
     * @param mixed  $limit
1065
     * @param string $message
1066
     *
1067
     * @throws InvalidArgumentException
1068
     */
1069 12
    public static function lessThanEq($value, $limit, $message = '')
1070
    {
1071 12
        if ($value > $limit) {
1072 4
            static::reportInvalidArgument(\sprintf(
1073 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
1074 4
                static::valueToString($value),
1075 4
                static::valueToString($limit)
1076
            ));
1077
        }
1078 8
    }
1079
1080
    /**
1081
     * Inclusive range, so Assert::(3, 3, 5) passes.
1082
     *
1083
     * @psalm-pure
1084
     *
1085
     * @param mixed  $value
1086
     * @param mixed  $min
1087
     * @param mixed  $max
1088
     * @param string $message
1089
     *
1090
     * @throws InvalidArgumentException
1091
     */
1092 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
1093
    {
1094 16
        if ($value < $min || $value > $max) {
1095 8
            static::reportInvalidArgument(\sprintf(
1096 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
1097 8
                static::valueToString($value),
1098 8
                static::valueToString($min),
1099 8
                static::valueToString($max)
1100
            ));
1101
        }
1102 8
    }
1103
1104
    /**
1105
     * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion.
1106
     *
1107
     * @psalm-pure
1108
     *
1109
     * @param mixed  $value
1110
     * @param array  $values
1111
     * @param string $message
1112
     *
1113
     * @throws InvalidArgumentException
1114
     */
1115 8
    public static function oneOf($value, array $values, $message = '')
1116
    {
1117 8
        if (!\in_array($value, $values, true)) {
1118 4
            static::reportInvalidArgument(\sprintf(
1119 4
                $message ?: 'Expected one of: %2$s. Got: %s',
1120 4
                static::valueToString($value),
1121 4
                \implode(', ', \array_map(array('static', 'valueToString'), $values))
1122
            ));
1123
        }
1124 4
    }
1125
1126
    /**
1127
     * @psalm-pure
1128
     *
1129
     * @param string $value
1130
     * @param string $subString
1131
     * @param string $message
1132
     *
1133
     * @throws InvalidArgumentException
1134
     */
1135 80 View Code Duplication
    public static function contains($value, $subString, $message = '')
1136
    {
1137 80
        if (false === \strpos($value, $subString)) {
1138 32
            static::reportInvalidArgument(\sprintf(
1139 32
                $message ?: 'Expected a value to contain %2$s. Got: %s',
1140 32
                static::valueToString($value),
1141 32
                static::valueToString($subString)
1142
            ));
1143
        }
1144 48
    }
1145
1146
    /**
1147
     * @psalm-pure
1148
     *
1149
     * @param string $value
1150
     * @param string $subString
1151
     * @param string $message
1152
     *
1153
     * @throws InvalidArgumentException
1154
     */
1155 80 View Code Duplication
    public static function notContains($value, $subString, $message = '')
1156
    {
1157 80
        if (false !== \strpos($value, $subString)) {
1158 48
            static::reportInvalidArgument(\sprintf(
1159 48
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
1160 48
                static::valueToString($value),
1161 48
                static::valueToString($subString)
1162
            ));
1163
        }
1164 32
    }
1165
1166
    /**
1167
     * @psalm-pure
1168
     *
1169
     * @param string $value
1170
     * @param string $message
1171
     *
1172
     * @throws InvalidArgumentException
1173
     */
1174 40
    public static function notWhitespaceOnly($value, $message = '')
1175
    {
1176 40
        if (\preg_match('/^\s*$/', $value)) {
1177 24
            static::reportInvalidArgument(\sprintf(
1178 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
1179 24
                static::valueToString($value)
1180
            ));
1181
        }
1182 16
    }
1183
1184
    /**
1185
     * @psalm-pure
1186
     *
1187
     * @param string $value
1188
     * @param string $prefix
1189
     * @param string $message
1190
     *
1191
     * @throws InvalidArgumentException
1192
     */
1193 48 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
1194
    {
1195 48
        if (0 !== \strpos($value, $prefix)) {
1196 32
            static::reportInvalidArgument(\sprintf(
1197 32
                $message ?: 'Expected a value to start with %2$s. Got: %s',
1198 32
                static::valueToString($value),
1199 32
                static::valueToString($prefix)
1200
            ));
1201
        }
1202 16
    }
1203
1204
    /**
1205
     * @psalm-pure
1206
     *
1207
     * @param string $value
1208
     * @param string $prefix
1209
     * @param string $message
1210
     *
1211
     * @throws InvalidArgumentException
1212
     */
1213 48 View Code Duplication
    public static function notStartsWith($value, $prefix, $message = '')
1214
    {
1215 48
        if (0 === \strpos($value, $prefix)) {
1216 16
            static::reportInvalidArgument(\sprintf(
1217 16
                $message ?: 'Expected a value not to start with %2$s. Got: %s',
1218 16
                static::valueToString($value),
1219 16
                static::valueToString($prefix)
1220
            ));
1221
        }
1222 32
    }
1223
1224
    /**
1225
     * @psalm-pure
1226
     *
1227
     * @param mixed  $value
1228
     * @param string $message
1229
     *
1230
     * @throws InvalidArgumentException
1231
     */
1232 35
    public static function startsWithLetter($value, $message = '')
1233
    {
1234 35
        static::string($value);
1235
1236 24
        $valid = isset($value[0]);
1237
1238 24
        if ($valid) {
1239 20
            $locale = \setlocale(LC_CTYPE, 0);
1240 20
            \setlocale(LC_CTYPE, 'C');
1241 20
            $valid = \ctype_alpha($value[0]);
1242 20
            \setlocale(LC_CTYPE, $locale);
1243
        }
1244
1245 24
        if (!$valid) {
1246 12
            static::reportInvalidArgument(\sprintf(
1247 12
                $message ?: 'Expected a value to start with a letter. Got: %s',
1248 12
                static::valueToString($value)
1249
            ));
1250
        }
1251 12
    }
1252
1253
    /**
1254
     * @psalm-pure
1255
     *
1256
     * @param string $value
1257
     * @param string $suffix
1258
     * @param string $message
1259
     *
1260
     * @throws InvalidArgumentException
1261
     */
1262 48 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
1263
    {
1264 48
        if ($suffix !== \substr($value, -\strlen($suffix))) {
1265 32
            static::reportInvalidArgument(\sprintf(
1266 32
                $message ?: 'Expected a value to end with %2$s. Got: %s',
1267 32
                static::valueToString($value),
1268 32
                static::valueToString($suffix)
1269
            ));
1270
        }
1271 16
    }
1272
1273
    /**
1274
     * @psalm-pure
1275
     *
1276
     * @param string $value
1277
     * @param string $suffix
1278
     * @param string $message
1279
     *
1280
     * @throws InvalidArgumentException
1281
     */
1282 48 View Code Duplication
    public static function notEndsWith($value, $suffix, $message = '')
1283
    {
1284 48
        if ($suffix === \substr($value, -\strlen($suffix))) {
1285 16
            static::reportInvalidArgument(\sprintf(
1286 16
                $message ?: 'Expected a value not to end with %2$s. Got: %s',
1287 16
                static::valueToString($value),
1288 16
                static::valueToString($suffix)
1289
            ));
1290
        }
1291 32
    }
1292
1293
    /**
1294
     * @psalm-pure
1295
     *
1296
     * @param string $value
1297
     * @param string $pattern
1298
     * @param string $message
1299
     *
1300
     * @throws InvalidArgumentException
1301
     */
1302 12
    public static function regex($value, $pattern, $message = '')
1303
    {
1304 12
        if (!\preg_match($pattern, $value)) {
1305 8
            static::reportInvalidArgument(\sprintf(
1306 8
                $message ?: 'The value %s does not match the expected pattern.',
1307 8
                static::valueToString($value)
1308
            ));
1309
        }
1310 4
    }
1311
1312
    /**
1313
     * @psalm-pure
1314
     *
1315
     * @param string $value
1316
     * @param string $pattern
1317
     * @param string $message
1318
     *
1319
     * @throws InvalidArgumentException
1320
     */
1321 12
    public static function notRegex($value, $pattern, $message = '')
1322
    {
1323 12
        if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
1324 4
            static::reportInvalidArgument(\sprintf(
1325 4
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
1326 4
                static::valueToString($value),
1327 4
                static::valueToString($pattern),
1328 4
                $matches[0][1]
1329
            ));
1330
        }
1331 8
    }
1332
1333
    /**
1334
     * @psalm-pure
1335
     *
1336
     * @param mixed  $value
1337
     * @param string $message
1338
     *
1339
     * @throws InvalidArgumentException
1340
     */
1341 28 View Code Duplication
    public static function unicodeLetters($value, $message = '')
1342
    {
1343 28
        static::string($value);
1344
1345 28
        if (!\preg_match('/^\p{L}+$/u', $value)) {
1346 16
            static::reportInvalidArgument(\sprintf(
1347 16
                $message ?: 'Expected a value to contain only Unicode letters. Got: %s',
1348 16
                static::valueToString($value)
1349
            ));
1350
        }
1351 12
    }
1352
1353
    /**
1354
     * @psalm-pure
1355
     *
1356
     * @param mixed  $value
1357
     * @param string $message
1358
     *
1359
     * @throws InvalidArgumentException
1360
     */
1361 20 View Code Duplication
    public static function alpha($value, $message = '')
1362
    {
1363 20
        static::string($value);
1364
1365 12
        $locale = \setlocale(LC_CTYPE, 0);
1366 12
        \setlocale(LC_CTYPE, 'C');
1367 12
        $valid = !\ctype_alpha($value);
1368 12
        \setlocale(LC_CTYPE, $locale);
1369
1370 12
        if ($valid) {
1371 8
            static::reportInvalidArgument(\sprintf(
1372 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
1373 8
                static::valueToString($value)
1374
            ));
1375
        }
1376 4
    }
1377
1378
    /**
1379
     * @psalm-pure
1380
     *
1381
     * @param string $value
1382
     * @param string $message
1383
     *
1384
     * @throws InvalidArgumentException
1385
     */
1386 12 View Code Duplication
    public static function digits($value, $message = '')
1387
    {
1388 12
        $locale = \setlocale(LC_CTYPE, 0);
1389 12
        \setlocale(LC_CTYPE, 'C');
1390 12
        $valid = !\ctype_digit($value);
1391 12
        \setlocale(LC_CTYPE, $locale);
1392
1393 12
        if ($valid) {
1394 8
            static::reportInvalidArgument(\sprintf(
1395 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
1396 8
                static::valueToString($value)
1397
            ));
1398
        }
1399 4
    }
1400
1401
    /**
1402
     * @psalm-pure
1403
     *
1404
     * @param string $value
1405
     * @param string $message
1406
     *
1407
     * @throws InvalidArgumentException
1408
     */
1409 12 View Code Duplication
    public static function alnum($value, $message = '')
1410
    {
1411 12
        $locale = \setlocale(LC_CTYPE, 0);
1412 12
        \setlocale(LC_CTYPE, 'C');
1413 12
        $valid = !\ctype_alnum($value);
1414 12
        \setlocale(LC_CTYPE, $locale);
1415
1416 12
        if ($valid) {
1417 8
            static::reportInvalidArgument(\sprintf(
1418 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
1419 8
                static::valueToString($value)
1420
            ));
1421
        }
1422 4
    }
1423
1424
    /**
1425
     * @psalm-pure
1426
     * @psalm-assert lowercase-string $value
1427
     *
1428
     * @param string $value
1429
     * @param string $message
1430
     *
1431
     * @throws InvalidArgumentException
1432
     */
1433 16 View Code Duplication
    public static function lower($value, $message = '')
1434
    {
1435 16
        $locale = \setlocale(LC_CTYPE, 0);
1436 16
        \setlocale(LC_CTYPE, 'C');
1437 16
        $valid = !\ctype_lower($value);
1438 16
        \setlocale(LC_CTYPE, $locale);
1439
1440 16
        if ($valid) {
1441 12
            static::reportInvalidArgument(\sprintf(
1442 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
1443 12
                static::valueToString($value)
1444
            ));
1445
        }
1446 4
    }
1447
1448
    /**
1449
     * @psalm-pure
1450
     * @psalm-assert !lowercase-string $value
1451
     *
1452
     * @param string $value
1453
     * @param string $message
1454
     *
1455
     * @throws InvalidArgumentException
1456
     */
1457 16 View Code Duplication
    public static function upper($value, $message = '')
1458
    {
1459 16
        $locale = \setlocale(LC_CTYPE, 0);
1460 16
        \setlocale(LC_CTYPE, 'C');
1461 16
        $valid = !\ctype_upper($value);
1462 16
        \setlocale(LC_CTYPE, $locale);
1463
1464 16
        if ($valid) {
1465 12
            static::reportInvalidArgument(\sprintf(
1466 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
1467 12
                static::valueToString($value)
1468
            ));
1469
        }
1470 4
    }
1471
1472
    /**
1473
     * @psalm-pure
1474
     *
1475
     * @param string $value
1476
     * @param int    $length
1477
     * @param string $message
1478
     *
1479
     * @throws InvalidArgumentException
1480
     */
1481 36
    public static function length($value, $length, $message = '')
1482
    {
1483 36
        if ($length !== static::strlen($value)) {
1484 24
            static::reportInvalidArgument(\sprintf(
1485 24
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
1486 24
                static::valueToString($value),
1487 24
                $length
1488
            ));
1489
        }
1490 12
    }
1491
1492
    /**
1493
     * Inclusive min.
1494
     *
1495
     * @psalm-pure
1496
     *
1497
     * @param string    $value
1498
     * @param int|float $min
1499
     * @param string    $message
1500
     *
1501
     * @throws InvalidArgumentException
1502
     */
1503 36
    public static function minLength($value, $min, $message = '')
1504
    {
1505 36
        if (static::strlen($value) < $min) {
1506 12
            static::reportInvalidArgument(\sprintf(
1507 12
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
1508 12
                static::valueToString($value),
1509 12
                $min
1510
            ));
1511
        }
1512 24
    }
1513
1514
    /**
1515
     * Inclusive max.
1516
     *
1517
     * @psalm-pure
1518
     *
1519
     * @param string    $value
1520
     * @param int|float $max
1521
     * @param string    $message
1522
     *
1523
     * @throws InvalidArgumentException
1524
     */
1525 36
    public static function maxLength($value, $max, $message = '')
1526
    {
1527 36
        if (static::strlen($value) > $max) {
1528 12
            static::reportInvalidArgument(\sprintf(
1529 12
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
1530 12
                static::valueToString($value),
1531 12
                $max
1532
            ));
1533
        }
1534 24
    }
1535
1536
    /**
1537
     * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion.
1538
     *
1539
     * @psalm-pure
1540
     *
1541
     * @param string    $value
1542
     * @param int|float $min
1543
     * @param int|float $max
1544
     * @param string    $message
1545
     *
1546
     * @throws InvalidArgumentException
1547
     */
1548 60 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
1549
    {
1550 60
        $length = static::strlen($value);
1551
1552 60
        if ($length < $min || $length > $max) {
1553 24
            static::reportInvalidArgument(\sprintf(
1554 24
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
1555 24
                static::valueToString($value),
1556 24
                $min,
1557 24
                $max
1558
            ));
1559
        }
1560 36
    }
1561
1562
    /**
1563
     * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file.
1564
     *
1565
     * @param mixed  $value
1566
     * @param string $message
1567
     *
1568
     * @throws InvalidArgumentException
1569
     */
1570 36 View Code Duplication
    public static function fileExists($value, $message = '')
1571
    {
1572 36
        static::string($value);
1573
1574 36
        if (!\file_exists($value)) {
1575 12
            static::reportInvalidArgument(\sprintf(
1576 12
                $message ?: 'The file %s does not exist.',
1577 12
                static::valueToString($value)
1578
            ));
1579
        }
1580 24
    }
1581
1582
    /**
1583
     * @param mixed  $value
1584
     * @param string $message
1585
     *
1586
     * @throws InvalidArgumentException
1587
     */
1588 12 View Code Duplication
    public static function file($value, $message = '')
1589
    {
1590 12
        static::fileExists($value, $message);
1591
1592 8
        if (!\is_file($value)) {
1593 4
            static::reportInvalidArgument(\sprintf(
1594 4
                $message ?: 'The path %s is not a file.',
1595 4
                static::valueToString($value)
1596
            ));
1597
        }
1598 4
    }
1599
1600
    /**
1601
     * @param mixed  $value
1602
     * @param string $message
1603
     *
1604
     * @throws InvalidArgumentException
1605
     */
1606 12 View Code Duplication
    public static function directory($value, $message = '')
1607
    {
1608 12
        static::fileExists($value, $message);
1609
1610 8
        if (!\is_dir($value)) {
1611 4
            static::reportInvalidArgument(\sprintf(
1612 4
                $message ?: 'The path %s is no directory.',
1613 4
                static::valueToString($value)
1614
            ));
1615
        }
1616 4
    }
1617
1618
    /**
1619
     * @param string $value
1620
     * @param string $message
1621
     *
1622
     * @throws InvalidArgumentException
1623
     */
1624
    public static function readable($value, $message = '')
1625
    {
1626
        if (!\is_readable($value)) {
1627
            static::reportInvalidArgument(\sprintf(
1628
                $message ?: 'The path %s is not readable.',
1629
                static::valueToString($value)
1630
            ));
1631
        }
1632
    }
1633
1634
    /**
1635
     * @param string $value
1636
     * @param string $message
1637
     *
1638
     * @throws InvalidArgumentException
1639
     */
1640
    public static function writable($value, $message = '')
1641
    {
1642
        if (!\is_writable($value)) {
1643
            static::reportInvalidArgument(\sprintf(
1644
                $message ?: 'The path %s is not writable.',
1645
                static::valueToString($value)
1646
            ));
1647
        }
1648
    }
1649
1650
    /**
1651
     * @param mixed  $value
1652
     * @param string $message
1653
     *
1654
     * @throws InvalidArgumentException
1655
     */
1656 8
    public static function classExists($value, $message = '')
1657
    {
1658 8
        if (!\class_exists($value)) {
1659 4
            static::reportInvalidArgument(\sprintf(
1660 4
                $message ?: 'Expected an existing class name. Got: %s',
1661 4
                static::valueToString($value)
1662
            ));
1663
        }
1664 4
    }
1665
1666
    /**
1667
     * @psalm-pure
1668
     * @psalm-template ExpectedType of object
1669
     * @psalm-param class-string<ExpectedType> $class
1670
     * @psalm-assert class-string<ExpectedType>|ExpectedType $value
1671
     *
1672
     * @param mixed         $value
1673
     * @param string|object $class
1674
     * @param string        $message
1675
     *
1676
     * @throws InvalidArgumentException
1677
     */
1678 8
    public static function subclassOf($value, $class, $message = '')
1679
    {
1680 8
        if (!\is_subclass_of($value, $class)) {
1681 4
            static::reportInvalidArgument(\sprintf(
1682 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
1683 4
                static::valueToString($value),
1684 4
                static::valueToString($class)
1685
            ));
1686
        }
1687 4
    }
1688
1689
    /**
1690
     * @param mixed  $value
1691
     * @param string $message
1692
     *
1693
     * @throws InvalidArgumentException
1694
     */
1695 8
    public static function interfaceExists($value, $message = '')
1696
    {
1697 8
        if (!\interface_exists($value)) {
1698 4
            static::reportInvalidArgument(\sprintf(
1699 4
                $message ?: 'Expected an existing interface name. got %s',
1700 4
                static::valueToString($value)
1701
            ));
1702
        }
1703 4
    }
1704
1705
    /**
1706
     * @psalm-pure
1707
     * @psalm-template ExpectedType of object
1708
     * @psalm-param class-string<ExpectedType> $interface
1709
     * @psalm-assert class-string<ExpectedType> $value
1710
     *
1711
     * @param mixed  $value
1712
     * @param mixed  $interface
1713
     * @param string $message
1714
     *
1715
     * @throws InvalidArgumentException
1716
     */
1717 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
1718
    {
1719 8
        if (!\in_array($interface, \class_implements($value))) {
1720 4
            static::reportInvalidArgument(\sprintf(
1721 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
1722 4
                static::valueToString($value),
1723 4
                static::valueToString($interface)
1724
            ));
1725
        }
1726 4
    }
1727
1728
    /**
1729
     * @psalm-pure
1730
     * @psalm-param class-string|object $classOrObject
1731
     *
1732
     * @param string|object $classOrObject
1733
     * @param mixed         $property
1734
     * @param string        $message
1735
     *
1736
     * @throws InvalidArgumentException
1737
     */
1738 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
1739
    {
1740 12
        if (!\property_exists($classOrObject, $property)) {
1741 4
            static::reportInvalidArgument(\sprintf(
1742 4
                $message ?: 'Expected the property %s to exist.',
1743 4
                static::valueToString($property)
1744
            ));
1745
        }
1746 8
    }
1747
1748
    /**
1749
     * @psalm-pure
1750
     * @psalm-param class-string|object $classOrObject
1751
     *
1752
     * @param string|object $classOrObject
1753
     * @param mixed         $property
1754
     * @param string        $message
1755
     *
1756
     * @throws InvalidArgumentException
1757
     */
1758 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
1759
    {
1760 12
        if (\property_exists($classOrObject, $property)) {
1761 8
            static::reportInvalidArgument(\sprintf(
1762 8
                $message ?: 'Expected the property %s to not exist.',
1763 8
                static::valueToString($property)
1764
            ));
1765
        }
1766 4
    }
1767
1768
    /**
1769
     * @psalm-pure
1770
     * @psalm-param class-string|object $classOrObject
1771
     *
1772
     * @param string|object $classOrObject
1773
     * @param mixed         $method
1774
     * @param string        $message
1775
     *
1776
     * @throws InvalidArgumentException
1777
     */
1778 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
1779
    {
1780 27
        if (!\method_exists($classOrObject, $method)) {
1781 19
            static::reportInvalidArgument(\sprintf(
1782 19
                $message ?: 'Expected the method %s to exist.',
1783 19
                static::valueToString($method)
1784
            ));
1785
        }
1786 8
    }
1787
1788
    /**
1789
     * @psalm-pure
1790
     * @psalm-param class-string|object $classOrObject
1791
     *
1792
     * @param string|object $classOrObject
1793
     * @param mixed         $method
1794
     * @param string        $message
1795
     *
1796
     * @throws InvalidArgumentException
1797
     */
1798 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
1799
    {
1800 27
        if (\method_exists($classOrObject, $method)) {
1801 8
            static::reportInvalidArgument(\sprintf(
1802 8
                $message ?: 'Expected the method %s to not exist.',
1803 8
                static::valueToString($method)
1804
            ));
1805
        }
1806 19
    }
1807
1808
    /**
1809
     * @psalm-pure
1810
     *
1811
     * @param array      $array
1812
     * @param string|int $key
1813
     * @param string     $message
1814
     *
1815
     * @throws InvalidArgumentException
1816
     */
1817 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
1818
    {
1819 12
        if (!(isset($array[$key]) || \array_key_exists($key, $array))) {
1820 4
            static::reportInvalidArgument(\sprintf(
1821 4
                $message ?: 'Expected the key %s to exist.',
1822 4
                static::valueToString($key)
1823
            ));
1824
        }
1825 8
    }
1826
1827
    /**
1828
     * @psalm-pure
1829
     *
1830
     * @param array      $array
1831
     * @param string|int $key
1832
     * @param string     $message
1833
     *
1834
     * @throws InvalidArgumentException
1835
     */
1836 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
1837
    {
1838 12
        if (isset($array[$key]) || \array_key_exists($key, $array)) {
1839 8
            static::reportInvalidArgument(\sprintf(
1840 8
                $message ?: 'Expected the key %s to not exist.',
1841 8
                static::valueToString($key)
1842
            ));
1843
        }
1844 4
    }
1845
1846
    /**
1847
     * Checks if a value is a valid array key (int or string).
1848
     *
1849
     * @psalm-pure
1850
     * @psalm-assert array-key $value
1851
     *
1852
     * @param mixed  $value
1853
     * @param string $message
1854
     *
1855
     * @throws InvalidArgumentException
1856
     */
1857 28
    public static function validArrayKey($value, $message = '')
1858
    {
1859 28
        if (!(\is_int($value) || \is_string($value))) {
1860 20
            static::reportInvalidArgument(\sprintf(
1861 20
                $message ?: 'Expected string or integer. Got: %s',
1862 20
                static::typeToString($value)
1863
            ));
1864
        }
1865 8
    }
1866
1867
    /**
1868
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1869
     *
1870
     * @param Countable|array $array
1871
     * @param int             $number
1872
     * @param string          $message
1873
     *
1874
     * @throws InvalidArgumentException
1875
     */
1876 8
    public static function count($array, $number, $message = '')
1877
    {
1878 8
        static::eq(
1879 8
            \count($array),
1880
            $number,
1881 8
            $message ?: \sprintf('Expected an array to contain %d elements. Got: %d.', $number, \count($array))
1882
        );
1883 4
    }
1884
1885
    /**
1886
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1887
     *
1888
     * @param Countable|array $array
1889
     * @param int|float       $min
1890
     * @param string          $message
1891
     *
1892
     * @throws InvalidArgumentException
1893
     */
1894 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
1895
    {
1896 12
        if (\count($array) < $min) {
1897 4
            static::reportInvalidArgument(\sprintf(
1898 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
1899 4
                \count($array),
1900 4
                $min
1901
            ));
1902
        }
1903 8
    }
1904
1905
    /**
1906
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1907
     *
1908
     * @param Countable|array $array
1909
     * @param int|float       $max
1910
     * @param string          $message
1911
     *
1912
     * @throws InvalidArgumentException
1913
     */
1914 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
1915
    {
1916 12
        if (\count($array) > $max) {
1917 4
            static::reportInvalidArgument(\sprintf(
1918 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
1919 4
                \count($array),
1920 4
                $max
1921
            ));
1922
        }
1923 8
    }
1924
1925
    /**
1926
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1927
     *
1928
     * @param Countable|array $array
1929
     * @param int|float       $min
1930
     * @param int|float       $max
1931
     * @param string          $message
1932
     *
1933
     * @throws InvalidArgumentException
1934
     */
1935 20
    public static function countBetween($array, $min, $max, $message = '')
1936
    {
1937 20
        $count = \count($array);
1938
1939 20
        if ($count < $min || $count > $max) {
1940 8
            static::reportInvalidArgument(\sprintf(
1941 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
1942 8
                $count,
1943 8
                $min,
1944 8
                $max
1945
            ));
1946
        }
1947 12
    }
1948
1949
    /**
1950
     * @psalm-pure
1951
     * @psalm-assert list $array
1952
     *
1953
     * @param mixed  $array
1954
     * @param string $message
1955
     *
1956
     * @throws InvalidArgumentException
1957
     */
1958 80 View Code Duplication
    public static function isList($array, $message = '')
1959
    {
1960 80
        if (!\is_array($array) || $array !== \array_values($array)) {
1961 32
            static::reportInvalidArgument(
1962 32
                $message ?: 'Expected list - non-associative array.'
1963
            );
1964
        }
1965 48
    }
1966
1967
    /**
1968
     * @psalm-pure
1969
     * @psalm-assert list $array
1970
     * @psalm-assert !empty $array
1971
     *
1972
     * @param mixed  $array
1973
     * @param string $message
1974
     *
1975
     * @throws InvalidArgumentException
1976
     */
1977 40
    public static function isNonEmptyList($array, $message = '')
1978
    {
1979 40
        static::isList($array, $message);
1980 24
        static::notEmpty($array, $message);
1981 20
    }
1982
1983
    /**
1984
     * @psalm-pure
1985
     * @psalm-template T
1986
     * @psalm-param mixed|array<T> $array
1987
     * @psalm-assert array<string, T> $array
1988
     *
1989
     * @param mixed  $array
1990
     * @param string $message
1991
     *
1992
     * @throws InvalidArgumentException
1993
     */
1994 32
    public static function isMap($array, $message = '')
1995
    {
1996
        if (
1997 32
            !\is_array($array) ||
1998 32
            \array_keys($array) !== \array_filter(\array_keys($array), '\is_string')
1999
        ) {
2000 16
            static::reportInvalidArgument(
2001 16
                $message ?: 'Expected map - associative array with string keys.'
2002
            );
2003
        }
2004 16
    }
2005
2006
    /**
2007
     * @psalm-pure
2008
     * @psalm-template T
2009
     * @psalm-param mixed|array<T> $array
2010
     * @psalm-assert array<string, T> $array
2011
     * @psalm-assert !empty $array
2012
     *
2013
     * @param mixed  $array
2014
     * @param string $message
2015
     *
2016
     * @throws InvalidArgumentException
2017
     */
2018 16
    public static function isNonEmptyMap($array, $message = '')
2019
    {
2020 16
        static::isMap($array, $message);
2021 8
        static::notEmpty($array, $message);
2022 4
    }
2023
2024
    /**
2025
     * @psalm-pure
2026
     *
2027
     * @param string $value
2028
     * @param string $message
2029
     *
2030
     * @throws InvalidArgumentException
2031
     */
2032 56
    public static function uuid($value, $message = '')
2033
    {
2034 56
        $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
2035
2036
        // The nil UUID is special form of UUID that is specified to have all
2037
        // 128 bits set to zero.
2038 56
        if ('00000000-0000-0000-0000-000000000000' === $value) {
2039 4
            return;
2040
        }
2041
2042 52
        if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) {
2043 20
            static::reportInvalidArgument(\sprintf(
2044 20
                $message ?: 'Value %s is not a valid UUID.',
2045 20
                static::valueToString($value)
2046
            ));
2047
        }
2048 32
    }
2049
2050
    /**
2051
     * @psalm-param class-string<Throwable>
2052
     *
2053
     * @param Closure $expression
2054
     * @param string  $class
2055
     * @param string  $message
2056
     *
2057
     * @throws InvalidArgumentException
2058
     */
2059 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
2060
    {
2061 24
        static::string($class);
2062
2063 24
        $actual = 'none';
2064
2065
        try {
2066 24
            $expression();
2067 24
        } catch (Exception $e) {
2068 20
            $actual = \get_class($e);
2069 20
            if ($e instanceof $class) {
2070 20
                return;
2071
            }
2072 4
        } catch (Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
2073 4
            $actual = \get_class($e);
2074 4
            if ($e instanceof $class) {
2075 4
                return;
2076
            }
2077
        }
2078
2079 8
        static::reportInvalidArgument($message ?: \sprintf(
2080 8
            'Expected to throw "%s", got "%s"',
2081 8
            $class,
2082 8
            $actual
2083
        ));
2084
    }
2085
2086
    /**
2087
     * @throws BadMethodCallException
2088
     */
2089 1635
    public static function __callStatic($name, $arguments)
2090
    {
2091 1635
        if ('nullOr' === \substr($name, 0, 6)) {
2092 604
            if (null !== $arguments[0]) {
2093 499
                $method = \lcfirst(\substr($name, 6));
2094 499
                \call_user_func_array(array('static', $method), $arguments);
2095
            }
2096
2097 352
            return;
2098
        }
2099
2100 1031
        if ('all' === \substr($name, 0, 3)) {
2101 1030
            static::isIterable($arguments[0]);
2102
2103 1030
            $method = \lcfirst(\substr($name, 3));
2104 1030
            $args = $arguments;
2105
2106 1030
            foreach ($arguments[0] as $entry) {
2107 1030
                $args[0] = $entry;
2108
2109 1030
                \call_user_func_array(array('static', $method), $args);
2110
            }
2111
2112 502
            return;
2113
        }
2114
2115 1
        throw new BadMethodCallException('No such method: '.$name);
2116
    }
2117
2118
    /**
2119
     * @param mixed $value
2120
     *
2121
     * @return string
2122
     */
2123 746
    protected static function valueToString($value)
2124
    {
2125 746
        if (null === $value) {
2126 20
            return 'null';
2127
        }
2128
2129 728
        if (true === $value) {
2130 15
            return 'true';
2131
        }
2132
2133 718
        if (false === $value) {
2134 25
            return 'false';
2135
        }
2136
2137 693
        if (\is_array($value)) {
2138 21
            return 'array';
2139
        }
2140
2141 672
        if (\is_object($value)) {
2142 2
            if (\method_exists($value, '__toString')) {
2143 1
                return \get_class($value).': '.self::valueToString($value->__toString());
2144
            }
2145
2146 1
            return \get_class($value);
2147
        }
2148
2149 671
        if (\is_resource($value)) {
2150 1
            return 'resource';
2151
        }
2152
2153 671
        if (\is_string($value)) {
2154 569
            return '"'.$value.'"';
2155
        }
2156
2157 110
        return (string) $value;
2158
    }
2159
2160
    /**
2161
     * @param mixed $value
2162
     *
2163
     * @return string
2164
     */
2165 251
    protected static function typeToString($value)
2166
    {
2167 251
        return \is_object($value) ? \get_class($value) : \gettype($value);
2168
    }
2169
2170 168
    protected static function strlen($value)
2171
    {
2172 168
        if (!\function_exists('mb_detect_encoding')) {
2173
            return \strlen($value);
2174
        }
2175
2176 168
        if (false === $encoding = \mb_detect_encoding($value)) {
2177
            return \strlen($value);
2178
        }
2179
2180 168
        return \mb_strlen($value, $encoding);
2181
    }
2182
2183
    /**
2184
     * @param string $message
2185
     *
2186
     * @throws InvalidArgumentException
2187
     *
2188
     * @psalm-pure this method is not supposed to perform side-effects
2189
     */
2190 1060
    protected static function reportInvalidArgument($message)
2191
    {
2192 1060
        throw new InvalidArgumentException($message);
2193
    }
2194
2195
    private function __construct()
2196
    {
2197
    }
2198
}
2199