Failed Conditions
Pull Request — master (#174)
by Gert de
06:50 queued 04:45
created

Assert::directory()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
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 DateTime;
19
use DateTimeImmutable;
20
use Exception;
21
use InvalidArgumentException;
22
use ResourceBundle;
23
use SimpleXMLElement;
24
use Throwable;
25
use Traversable;
26
27
/**
28
 * Efficient assertions to validate the input/output of your methods.
29
 *
30
 * @method static void nullOrString($value, $message = '')
31
 * @method static void nullOrStringNotEmpty($value, $message = '')
32
 * @method static void nullOrInteger($value, $message = '')
33
 * @method static void nullOrIntegerish($value, $message = '')
34
 * @method static void nullOrFloat($value, $message = '')
35
 * @method static void nullOrNumeric($value, $message = '')
36
 * @method static void nullOrNatural($value, $message = '')
37
 * @method static void nullOrBoolean($value, $message = '')
38
 * @method static void nullOrScalar($value, $message = '')
39
 * @method static void nullOrObject($value, $message = '')
40
 * @method static void nullOrResource($value, $type = null, $message = '')
41
 * @method static void nullOrIsCallable($value, $message = '')
42
 * @method static void nullOrIsArray($value, $message = '')
43
 * @method static void nullOrIsTraversable($value, $message = '')
44
 * @method static void nullOrIsArrayAccessible($value, $message = '')
45
 * @method static void nullOrIsCountable($value, $message = '')
46
 * @method static void nullOrIsIterable($value, $message = '')
47
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
48
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
49
 * @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
50
 * @method static void nullOrIsAOf($value, $classes, $message = '')
51
 * @method static void nullOrIsAnyOf($value, $classes, $message = '')
52
 * @method static void nullOrIsNotA($value, $classes, $message = '')
53
 * @method static void nullOrIsEmpty($value, $message = '')
54
 * @method static void nullOrNotEmpty($value, $message = '')
55
 * @method static void nullOrTrue($value, $message = '')
56
 * @method static void nullOrFalse($value, $message = '')
57
 * @method static void nullOrNotFalse($value, $message = '')
58
 * @method static void nullOrIp($value, $message = '')
59
 * @method static void nullOrIpv4($value, $message = '')
60
 * @method static void nullOrIpv6($value, $message = '')
61
 * @method static void nullOrEmail($value, $message = '')
62
 * @method static void nullOrUniqueValues($values, $message = '')
63
 * @method static void nullOrEq($value, $expect, $message = '')
64
 * @method static void nullOrNotEq($value, $expect, $message = '')
65
 * @method static void nullOrSame($value, $expect, $message = '')
66
 * @method static void nullOrNotSame($value, $expect, $message = '')
67
 * @method static void nullOrGreaterThan($value, $limit, $message = '')
68
 * @method static void nullOrGreaterThanEq($value, $limit, $message = '')
69
 * @method static void nullOrLessThan($value, $limit, $message = '')
70
 * @method static void nullOrLessThanEq($value, $limit, $message = '')
71
 * @method static void nullOrRange($value, $min, $max, $message = '')
72
 * @method static void nullOrOneOf($value, $values, $message = '')
73
 * @method static void nullOrContains($value, $subString, $message = '')
74
 * @method static void nullOrNotContains($value, $subString, $message = '')
75
 * @method static void nullOrNotWhitespaceOnly($value, $message = '')
76
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
77
 * @method static void nullOrStartsWithLetter($value, $message = '')
78
 * @method static void nullOrEndsWith($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 allStartsWithLetter($values, $message = '')
167
 * @method static void allEndsWith($values, $suffix, $message = '')
168
 * @method static void allRegex($values, $pattern, $message = '')
169
 * @method static void allNotRegex($values, $pattern, $message = '')
170
 * @method static void allUnicodeLetters($values, $message = '')
171
 * @method static void allAlpha($values, $message = '')
172
 * @method static void allDigits($values, $message = '')
173
 * @method static void allAlnum($values, $message = '')
174
 * @method static void allLower($values, $message = '')
175
 * @method static void allUpper($values, $message = '')
176
 * @method static void allLength($values, $length, $message = '')
177
 * @method static void allMinLength($values, $min, $message = '')
178
 * @method static void allMaxLength($values, $max, $message = '')
179
 * @method static void allLengthBetween($values, $min, $max, $message = '')
180
 * @method static void allFileExists($values, $message = '')
181
 * @method static void allFile($values, $message = '')
182
 * @method static void allDirectory($values, $message = '')
183
 * @method static void allReadable($values, $message = '')
184
 * @method static void allWritable($values, $message = '')
185
 * @method static void allClassExists($values, $message = '')
186
 * @method static void allSubclassOf($values, $class, $message = '')
187
 * @method static void allInterfaceExists($values, $message = '')
188
 * @method static void allImplementsInterface($values, $interface, $message = '')
189
 * @method static void allPropertyExists($values, $property, $message = '')
190
 * @method static void allPropertyNotExists($values, $property, $message = '')
191
 * @method static void allMethodExists($values, $method, $message = '')
192
 * @method static void allMethodNotExists($values, $method, $message = '')
193
 * @method static void allKeyExists($values, $key, $message = '')
194
 * @method static void allKeyNotExists($values, $key, $message = '')
195
 * @method static void allValidArrayKey($values, $message = '')
196
 * @method static void allCount($values, $key, $message = '')
197
 * @method static void allMinCount($values, $min, $message = '')
198
 * @method static void allMaxCount($values, $max, $message = '')
199
 * @method static void allCountBetween($values, $min, $max, $message = '')
200
 * @method static void allIsList($values, $message = '')
201
 * @method static void allIsNonEmptyList($values, $message = '')
202
 * @method static void allIsMap($values, $message = '')
203
 * @method static void allIsNonEmptyMap($values, $message = '')
204
 * @method static void allUuid($values, $message = '')
205
 * @method static void allThrows($expressions, $class = 'Exception', $message = '')
206
 *
207
 * @since  1.0
208
 *
209
 * @author Bernhard Schussek <[email protected]>
210
 */
211
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...
212
{
213
    /**
214
     * @psalm-pure
215
     * @psalm-assert string $value
216
     *
217
     * @param mixed  $value
218
     * @param string $message
219
     *
220
     * @throws InvalidArgumentException
221
     */
222 241
    public static function string($value, $message = '')
223
    {
224 241
        if (!\is_string($value)) {
225 45
            static::reportInvalidArgument(\sprintf(
226 45
                $message ?: 'Expected a string. Got: %s',
227 45
                static::typeToString($value)
228
            ));
229
        }
230 196
    }
231
232
    /**
233
     * @psalm-pure
234
     * @psalm-assert string $value
235
     * @psalm-assert !empty $value
236
     *
237
     * @param mixed  $value
238
     * @param string $message
239
     *
240
     * @throws InvalidArgumentException
241
     */
242 16
    public static function stringNotEmpty($value, $message = '')
243
    {
244 16
        static::string($value, $message);
245 12
        static::notEq($value, '', $message);
246 8
    }
247
248
    /**
249
     * @psalm-pure
250
     * @psalm-assert int $value
251
     *
252
     * @param mixed  $value
253
     * @param string $message
254
     *
255
     * @throws InvalidArgumentException
256
     */
257 17
    public static function integer($value, $message = '')
258
    {
259 17
        if (!\is_int($value)) {
260 13
            static::reportInvalidArgument(\sprintf(
261 13
                $message ?: 'Expected an integer. Got: %s',
262 13
                static::typeToString($value)
263
            ));
264
        }
265 4
    }
266
267
    /**
268
     * @psalm-pure
269
     * @psalm-assert numeric $value
270
     *
271
     * @param mixed  $value
272
     * @param string $message
273
     *
274
     * @throws InvalidArgumentException
275
     */
276 16
    public static function integerish($value, $message = '')
277
    {
278 16
        if (!\is_numeric($value) || $value != (int) $value) {
279 4
            static::reportInvalidArgument(\sprintf(
280 4
                $message ?: 'Expected an integerish value. Got: %s',
281 4
                static::typeToString($value)
282
            ));
283
        }
284 12
    }
285
286
    /**
287
     * @psalm-pure
288
     * @psalm-assert float $value
289
     *
290
     * @param mixed  $value
291
     * @param string $message
292
     *
293
     * @throws InvalidArgumentException
294
     */
295 16
    public static function float($value, $message = '')
296
    {
297 16
        if (!\is_float($value)) {
298 8
            static::reportInvalidArgument(\sprintf(
299 8
                $message ?: 'Expected a float. Got: %s',
300 8
                static::typeToString($value)
301
            ));
302
        }
303 8
    }
304
305
    /**
306
     * @psalm-pure
307
     * @psalm-assert numeric $value
308
     *
309
     * @param mixed  $value
310
     * @param string $message
311
     *
312
     * @throws InvalidArgumentException
313
     */
314 20
    public static function numeric($value, $message = '')
315
    {
316 20
        if (!\is_numeric($value)) {
317 4
            static::reportInvalidArgument(\sprintf(
318 4
                $message ?: 'Expected a numeric. Got: %s',
319 4
                static::typeToString($value)
320
            ));
321
        }
322 16
    }
323
324
    /**
325
     * @psalm-pure
326
     * @psalm-assert int $value
327
     *
328
     * @param mixed  $value
329
     * @param string $message
330
     *
331
     * @throws InvalidArgumentException
332
     */
333 24
    public static function natural($value, $message = '')
334
    {
335 24
        if (!\is_int($value) || $value < 0) {
336 16
            static::reportInvalidArgument(\sprintf(
337 16
                $message ?: 'Expected a non-negative integer. Got %s',
338 16
                static::valueToString($value)
339
            ));
340
        }
341 8
    }
342
343
    /**
344
     * @psalm-pure
345
     * @psalm-assert bool $value
346
     *
347
     * @param mixed  $value
348
     * @param string $message
349
     *
350
     * @throws InvalidArgumentException
351
     */
352 16
    public static function boolean($value, $message = '')
353
    {
354 16
        if (!\is_bool($value)) {
355 8
            static::reportInvalidArgument(\sprintf(
356 8
                $message ?: 'Expected a boolean. Got: %s',
357 8
                static::typeToString($value)
358
            ));
359
        }
360 8
    }
361
362
    /**
363
     * @psalm-pure
364
     * @psalm-assert scalar $value
365
     *
366
     * @param mixed  $value
367
     * @param string $message
368
     *
369
     * @throws InvalidArgumentException
370
     */
371 23
    public static function scalar($value, $message = '')
372
    {
373 23
        if (!\is_scalar($value)) {
374 11
            static::reportInvalidArgument(\sprintf(
375 11
                $message ?: 'Expected a scalar. Got: %s',
376 11
                static::typeToString($value)
377
            ));
378
        }
379 12
    }
380
381
    /**
382
     * @psalm-pure
383
     * @psalm-assert object $value
384
     *
385
     * @param mixed  $value
386
     * @param string $message
387
     *
388
     * @throws InvalidArgumentException
389
     */
390 23
    public static function object($value, $message = '')
391
    {
392 23
        if (!\is_object($value)) {
393 15
            static::reportInvalidArgument(\sprintf(
394 15
                $message ?: 'Expected an object. Got: %s',
395 15
                static::typeToString($value)
396
            ));
397
        }
398 8
    }
399
400
    /**
401
     * @psalm-pure
402
     * @psalm-assert resource $value
403
     *
404
     * @param mixed       $value
405
     * @param string|null $type    type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php
406
     * @param string      $message
407
     *
408
     * @throws InvalidArgumentException
409
     */
410 16
    public static function resource($value, $type = null, $message = '')
411
    {
412 16
        if (!\is_resource($value)) {
413 4
            static::reportInvalidArgument(\sprintf(
414 4
                $message ?: 'Expected a resource. Got: %s',
415 4
                static::typeToString($value)
416
            ));
417
        }
418
419 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...
420 4
            static::reportInvalidArgument(\sprintf(
421 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
422 4
                static::typeToString($value),
423 4
                $type
424
            ));
425
        }
426 8
    }
427
428
    /**
429
     * @psalm-pure
430
     * @psalm-assert callable $value
431
     *
432
     * @param mixed  $value
433
     * @param string $message
434
     *
435
     * @throws InvalidArgumentException
436
     */
437 20
    public static function isCallable($value, $message = '')
438
    {
439 20
        if (!\is_callable($value)) {
440 8
            static::reportInvalidArgument(\sprintf(
441 8
                $message ?: 'Expected a callable. Got: %s',
442 8
                static::typeToString($value)
443
            ));
444
        }
445 12
    }
446
447
    /**
448
     * @psalm-pure
449
     * @psalm-assert array $value
450
     *
451
     * @param mixed  $value
452
     * @param string $message
453
     *
454
     * @throws InvalidArgumentException
455
     */
456 20
    public static function isArray($value, $message = '')
457
    {
458 20
        if (!\is_array($value)) {
459 12
            static::reportInvalidArgument(\sprintf(
460 12
                $message ?: 'Expected an array. Got: %s',
461 12
                static::typeToString($value)
462
            ));
463
        }
464 8
    }
465
466
    /**
467
     * @psalm-pure
468
     * @psalm-assert iterable $value
469
     *
470
     * @deprecated use "isIterable" or "isInstanceOf" instead
471
     *
472
     * @param mixed  $value
473
     * @param string $message
474
     *
475
     * @throws InvalidArgumentException
476
     */
477 20
    public static function isTraversable($value, $message = '')
478
    {
479 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...
480 20
            \sprintf(
481 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.',
482 20
                __METHOD__
483
            ),
484 20
            \E_USER_DEPRECATED
485
        );
486
487 20
        if (!\is_array($value) && !($value instanceof Traversable)) {
488 8
            static::reportInvalidArgument(\sprintf(
489 8
                $message ?: 'Expected a traversable. Got: %s',
490 8
                static::typeToString($value)
491
            ));
492
        }
493 12
    }
494
495
    /**
496
     * @psalm-pure
497
     * @psalm-assert array|ArrayAccess $value
498
     *
499
     * @param mixed  $value
500
     * @param string $message
501
     *
502
     * @throws InvalidArgumentException
503
     */
504 20 View Code Duplication
    public static function isArrayAccessible($value, $message = '')
505
    {
506 20
        if (!\is_array($value) && !($value instanceof ArrayAccess)) {
507 8
            static::reportInvalidArgument(\sprintf(
508 8
                $message ?: 'Expected an array accessible. Got: %s',
509 8
                static::typeToString($value)
510
            ));
511
        }
512 12
    }
513
514
    /**
515
     * @psalm-pure
516
     * @psalm-assert countable $value
517
     *
518
     * @param mixed  $value
519
     * @param string $message
520
     *
521
     * @throws InvalidArgumentException
522
     */
523 28
    public static function isCountable($value, $message = '')
524
    {
525
        if (
526 28
            !\is_array($value)
527 28
            && !($value instanceof Countable)
528 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...
529 28
            && !($value instanceof SimpleXMLElement)
530
        ) {
531 12
            static::reportInvalidArgument(\sprintf(
532 12
                $message ?: 'Expected a countable. Got: %s',
533 12
                static::typeToString($value)
534
            ));
535
        }
536 16
    }
537
538
    /**
539
     * @psalm-pure
540
     * @psalm-assert iterable $value
541
     *
542
     * @param mixed  $value
543
     * @param string $message
544
     *
545
     * @throws InvalidArgumentException
546
     */
547 996 View Code Duplication
    public static function isIterable($value, $message = '')
548
    {
549 996
        if (!\is_array($value) && !($value instanceof Traversable)) {
550 8
            static::reportInvalidArgument(\sprintf(
551 8
                $message ?: 'Expected an iterable. Got: %s',
552 8
                static::typeToString($value)
553
            ));
554
        }
555 992
    }
556
557
    /**
558
     * @psalm-pure
559
     * @psalm-template ExpectedType of object
560
     * @psalm-param class-string<ExpectedType> $class
561
     * @psalm-assert ExpectedType $value
562
     *
563
     * @param mixed         $value
564
     * @param string|object $class
565
     * @param string        $message
566
     *
567
     * @throws InvalidArgumentException
568
     */
569 19
    public static function isInstanceOf($value, $class, $message = '')
570
    {
571 19
        if (!($value instanceof $class)) {
572 15
            static::reportInvalidArgument(\sprintf(
573 15
                $message ?: 'Expected an instance of %2$s. Got: %s',
574 15
                static::typeToString($value),
575 15
                $class
576
            ));
577
        }
578 4
    }
579
580
    /**
581
     * @psalm-pure
582
     * @psalm-template ExpectedType of object
583
     * @psalm-param class-string<ExpectedType> $class
584
     * @psalm-assert !ExpectedType $value
585
     *
586
     * @param mixed         $value
587
     * @param string|object $class
588
     * @param string        $message
589
     *
590
     * @throws InvalidArgumentException
591
     */
592 16
    public static function notInstanceOf($value, $class, $message = '')
593
    {
594 16
        if ($value instanceof $class) {
595 4
            static::reportInvalidArgument(\sprintf(
596 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
597 4
                static::typeToString($value),
598 4
                $class
599
            ));
600
        }
601 12
    }
602
603
    /**
604
     * @psalm-pure
605
     * @psalm-param array<class-string> $classes
606
     *
607
     * @param mixed                $value
608
     * @param array<object|string> $classes
609
     * @param string               $message
610
     *
611
     * @throws InvalidArgumentException
612
     */
613 20
    public static function isInstanceOfAny($value, array $classes, $message = '')
614
    {
615 20
        foreach ($classes as $class) {
616 20
            if ($value instanceof $class) {
617 8
                return;
618
            }
619
        }
620
621 12
        static::reportInvalidArgument(\sprintf(
622 12
            $message ?: 'Expected an instance of any of %2$s. Got: %s',
623 12
            static::typeToString($value),
624 12
            \implode(', ', \array_map(array('static', 'valueToString'), $classes))
625
        ));
626
    }
627
628
    /**
629
     * @psalm-pure
630
     * @psalm-template ExpectedType of object
631
     * @psalm-param class-string<ExpectedType> $class
632
     * @psalm-assert ExpectedType|class-string<ExpectedType> $value
633
     *
634
     * @param object|string $value
635
     * @param string        $class
636
     * @param string        $message
637
     *
638
     * @throws InvalidArgumentException
639
     */
640 20 View Code Duplication
    public static function isAOf($value, $class, $message = '')
641
    {
642 20
        static::string($class, 'Expected class as a string. Got: %s');
643
644 16
        if (!\is_a($value, $class, \is_string($value))) {
645 12
            static::reportInvalidArgument(sprintf(
646 12
                $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s',
647 12
                static::typeToString($value),
648
                $class
649
            ));
650
        }
651 4
    }
652
653
    /**
654
     * @psalm-pure
655
     * @psalm-template UnexpectedType of object
656
     * @psalm-param class-string<UnexpectedType> $class
657
     * @psalm-assert !UnexpectedType $value
658
     * @psalm-assert !class-string<UnexpectedType> $value
659
     *
660
     * @param object|string $value
661
     * @param string        $class
662
     * @param string        $message
663
     *
664
     * @throws InvalidArgumentException
665
     */
666 20 View Code Duplication
    public static function isNotA($value, $class, $message = '')
667
    {
668 20
        static::string($class, 'Expected class as a string. Got: %s');
669
670 16
        if (\is_a($value, $class, \is_string($value))) {
671 4
            static::reportInvalidArgument(sprintf(
672 4
                $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s',
673 4
                static::typeToString($value),
674
                $class
675
            ));
676
        }
677 12
    }
678
679
    /**
680
     * @psalm-pure
681
     * @psalm-param array<class-string> $classes
682
     *
683
     * @param object|string $value
684
     * @param string[]      $classes
685
     * @param string        $message
686
     *
687
     * @throws InvalidArgumentException
688
     */
689 24
    public static function isAnyOf($value, array $classes, $message = '')
690
    {
691 24
        foreach ($classes as $class) {
692 24
            static::string($class, 'Expected class as a string. Got: %s');
693
694 20
            if (\is_a($value, $class, \is_string($value))) {
695 8
                return;
696
            }
697
        }
698
699 12
        static::reportInvalidArgument(sprintf(
700 12
            $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s',
701 12
            static::typeToString($value),
702 12
            \implode(', ', \array_map(array('static', 'valueToString'), $classes))
703
        ));
704
    }
705
706
    /**
707
     * @psalm-pure
708
     * @psalm-assert empty $value
709
     *
710
     * @param mixed  $value
711
     * @param string $message
712
     *
713
     * @throws InvalidArgumentException
714
     */
715 23
    public static function isEmpty($value, $message = '')
716
    {
717 23
        if (!empty($value)) {
718 8
            static::reportInvalidArgument(\sprintf(
719 8
                $message ?: 'Expected an empty value. Got: %s',
720 8
                static::valueToString($value)
721
            ));
722
        }
723 15
    }
724
725
    /**
726
     * @psalm-pure
727
     * @psalm-assert !empty $value
728
     *
729
     * @param mixed  $value
730
     * @param string $message
731
     *
732
     * @throws InvalidArgumentException
733
     */
734 55
    public static function notEmpty($value, $message = '')
735
    {
736 55
        if (empty($value)) {
737 23
            static::reportInvalidArgument(\sprintf(
738 23
                $message ?: 'Expected a non-empty value. Got: %s',
739 23
                static::valueToString($value)
740
            ));
741
        }
742 32
    }
743
744
    /**
745
     * @psalm-pure
746
     * @psalm-assert null $value
747
     *
748
     * @param mixed  $value
749
     * @param string $message
750
     *
751
     * @throws InvalidArgumentException
752
     */
753 11
    public static function null($value, $message = '')
754
    {
755 11
        if (null !== $value) {
756 8
            static::reportInvalidArgument(\sprintf(
757 8
                $message ?: 'Expected null. Got: %s',
758 8
                static::valueToString($value)
759
            ));
760
        }
761 3
    }
762
763
    /**
764
     * @psalm-pure
765
     * @psalm-assert !null $value
766
     *
767
     * @param mixed  $value
768
     * @param string $message
769
     *
770
     * @throws InvalidArgumentException
771
     */
772 11
    public static function notNull($value, $message = '')
773
    {
774 11
        if (null === $value) {
775 3
            static::reportInvalidArgument(
776 3
                $message ?: 'Expected a value other than null.'
777
            );
778
        }
779 8
    }
780
781
    /**
782
     * @psalm-pure
783
     * @psalm-assert true $value
784
     *
785
     * @param mixed  $value
786
     * @param string $message
787
     *
788
     * @throws InvalidArgumentException
789
     */
790 15
    public static function true($value, $message = '')
791
    {
792 15
        if (true !== $value) {
793 11
            static::reportInvalidArgument(\sprintf(
794 11
                $message ?: 'Expected a value to be true. Got: %s',
795 11
                static::valueToString($value)
796
            ));
797
        }
798 4
    }
799
800
    /**
801
     * @psalm-pure
802
     * @psalm-assert false $value
803
     *
804
     * @param mixed  $value
805
     * @param string $message
806
     *
807
     * @throws InvalidArgumentException
808
     */
809 19
    public static function false($value, $message = '')
810
    {
811 19
        if (false !== $value) {
812 15
            static::reportInvalidArgument(\sprintf(
813 15
                $message ?: 'Expected a value to be false. Got: %s',
814 15
                static::valueToString($value)
815
            ));
816
        }
817 4
    }
818
819
    /**
820
     * @psalm-assert !false $value
821
     *
822
     * @param mixed  $value
823
     * @param string $message
824
     *
825
     * @throws InvalidArgumentException
826
     */
827 19
    public static function notFalse($value, $message = '')
828
    {
829 19
        if (false === $value) {
830 4
            static::reportInvalidArgument(
831 4
                $message ?: 'Expected a value other than false.'
832
            );
833
        }
834 15
    }
835
836
    /**
837
     * @param mixed  $value
838
     * @param string $message
839
     *
840
     * @throws InvalidArgumentException
841
     */
842 51 View Code Duplication
    public static function ip($value, $message = '')
843
    {
844 51
        if (false === \filter_var($value, \FILTER_VALIDATE_IP)) {
845 19
            static::reportInvalidArgument(\sprintf(
846 19
                $message ?: 'Expected a value to be an IP. Got: %s',
847 19
                static::valueToString($value)
848
            ));
849
        }
850 32
    }
851
852
    /**
853
     * @param mixed  $value
854
     * @param string $message
855
     *
856
     * @throws InvalidArgumentException
857
     */
858 51 View Code Duplication
    public static function ipv4($value, $message = '')
859
    {
860 51
        if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
861 35
            static::reportInvalidArgument(\sprintf(
862 35
                $message ?: 'Expected a value to be an IPv4. Got: %s',
863 35
                static::valueToString($value)
864
            ));
865
        }
866 16
    }
867
868
    /**
869
     * @param mixed  $value
870
     * @param string $message
871
     *
872
     * @throws InvalidArgumentException
873
     */
874 51 View Code Duplication
    public static function ipv6($value, $message = '')
875
    {
876 51
        if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
877 31
            static::reportInvalidArgument(\sprintf(
878 31
                $message ?: 'Expected a value to be an IPv6. Got %s',
879 31
                static::valueToString($value)
880
            ));
881
        }
882 20
    }
883
884
    /**
885
     * @param mixed  $value
886
     * @param string $message
887
     *
888
     * @throws InvalidArgumentException
889
     */
890 20 View Code Duplication
    public static function email($value, $message = '')
891
    {
892 20
        if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) {
893 12
            static::reportInvalidArgument(\sprintf(
894 12
                $message ?: 'Expected a value to be a valid e-mail address. Got %s',
895 12
                static::valueToString($value)
896
            ));
897
        }
898 8
    }
899
900
    /**
901
     * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion.
902
     *
903
     * @param array  $values
904
     * @param string $message
905
     *
906
     * @throws InvalidArgumentException
907
     */
908 12
    public static function uniqueValues(array $values, $message = '')
909
    {
910 12
        $allValues = \count($values);
911 12
        $uniqueValues = \count(\array_unique($values));
912
913 12
        if ($allValues !== $uniqueValues) {
914 8
            $difference = $allValues - $uniqueValues;
915
916 8
            static::reportInvalidArgument(\sprintf(
917 8
                $message ?: 'Expected an array of unique values, but %s of them %s duplicated',
918 8
                $difference,
919 8
                (1 === $difference ? 'is' : 'are')
920
            ));
921
        }
922 4
    }
923
924
    /**
925
     * @param mixed  $value
926
     * @param mixed  $expect
927
     * @param string $message
928
     *
929
     * @throws InvalidArgumentException
930
     */
931 33
    public static function eq($value, $expect, $message = '')
932
    {
933 33
        if ($expect != $value) {
934 17
            static::reportInvalidArgument(\sprintf(
935 17
                $message ?: 'Expected a value equal to %2$s. Got: %s',
936 17
                static::valueToString($value),
937 17
                static::valueToString($expect)
938
            ));
939
        }
940 16
    }
941
942
    /**
943
     * @param mixed  $value
944
     * @param mixed  $expect
945
     * @param string $message
946
     *
947
     * @throws InvalidArgumentException
948
     */
949 28
    public static function notEq($value, $expect, $message = '')
950
    {
951 28
        if ($expect == $value) {
952 16
            static::reportInvalidArgument(\sprintf(
953 16
                $message ?: 'Expected a different value than %s.',
954 16
                static::valueToString($expect)
955
            ));
956
        }
957 12
    }
958
959
    /**
960
     * @psalm-pure
961
     *
962
     * @param mixed  $value
963
     * @param mixed  $expect
964
     * @param string $message
965
     *
966
     * @throws InvalidArgumentException
967
     */
968 16
    public static function same($value, $expect, $message = '')
969
    {
970 16
        if ($expect !== $value) {
971 12
            static::reportInvalidArgument(\sprintf(
972 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
973 12
                static::valueToString($value),
974 12
                static::valueToString($expect)
975
            ));
976
        }
977 4
    }
978
979
    /**
980
     * @psalm-pure
981
     *
982
     * @param mixed  $value
983
     * @param mixed  $expect
984
     * @param string $message
985
     *
986
     * @throws InvalidArgumentException
987
     */
988 16
    public static function notSame($value, $expect, $message = '')
989
    {
990 16
        if ($expect === $value) {
991 4
            static::reportInvalidArgument(\sprintf(
992 4
                $message ?: 'Expected a value not identical to %s.',
993 4
                static::valueToString($expect)
994
            ));
995
        }
996 12
    }
997
998
    /**
999
     * @psalm-pure
1000
     *
1001
     * @param mixed  $value
1002
     * @param mixed  $limit
1003
     * @param string $message
1004
     *
1005
     * @throws InvalidArgumentException
1006
     */
1007 8
    public static function greaterThan($value, $limit, $message = '')
1008
    {
1009 8
        if ($value <= $limit) {
1010 4
            static::reportInvalidArgument(\sprintf(
1011 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
1012 4
                static::valueToString($value),
1013 4
                static::valueToString($limit)
1014
            ));
1015
        }
1016 4
    }
1017
1018
    /**
1019
     * @psalm-pure
1020
     *
1021
     * @param mixed  $value
1022
     * @param mixed  $limit
1023
     * @param string $message
1024
     *
1025
     * @throws InvalidArgumentException
1026
     */
1027 12
    public static function greaterThanEq($value, $limit, $message = '')
1028
    {
1029 12
        if ($value < $limit) {
1030 4
            static::reportInvalidArgument(\sprintf(
1031 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
1032 4
                static::valueToString($value),
1033 4
                static::valueToString($limit)
1034
            ));
1035
        }
1036 8
    }
1037
1038
    /**
1039
     * @psalm-pure
1040
     *
1041
     * @param mixed  $value
1042
     * @param mixed  $limit
1043
     * @param string $message
1044
     *
1045
     * @throws InvalidArgumentException
1046
     */
1047 9
    public static function lessThan($value, $limit, $message = '')
1048
    {
1049 9
        if ($value >= $limit) {
1050 5
            static::reportInvalidArgument(\sprintf(
1051 5
                $message ?: 'Expected a value less than %2$s. Got: %s',
1052 5
                static::valueToString($value),
1053 5
                static::valueToString($limit)
1054
            ));
1055
        }
1056 4
    }
1057
1058
    /**
1059
     * @psalm-pure
1060
     *
1061
     * @param mixed  $value
1062
     * @param mixed  $limit
1063
     * @param string $message
1064
     *
1065
     * @throws InvalidArgumentException
1066
     */
1067 12
    public static function lessThanEq($value, $limit, $message = '')
1068
    {
1069 12
        if ($value > $limit) {
1070 4
            static::reportInvalidArgument(\sprintf(
1071 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
1072 4
                static::valueToString($value),
1073 4
                static::valueToString($limit)
1074
            ));
1075
        }
1076 8
    }
1077
1078
    /**
1079
     * Inclusive range, so Assert::(3, 3, 5) passes.
1080
     *
1081
     * @psalm-pure
1082
     *
1083
     * @param mixed  $value
1084
     * @param mixed  $min
1085
     * @param mixed  $max
1086
     * @param string $message
1087
     *
1088
     * @throws InvalidArgumentException
1089
     */
1090 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
1091
    {
1092 16
        if ($value < $min || $value > $max) {
1093 8
            static::reportInvalidArgument(\sprintf(
1094 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
1095 8
                static::valueToString($value),
1096 8
                static::valueToString($min),
1097 8
                static::valueToString($max)
1098
            ));
1099
        }
1100 8
    }
1101
1102
    /**
1103
     * A more human-readable alias of Assert::inArray().
1104
     *
1105
     * @psalm-pure
1106
     *
1107
     * @param mixed  $value
1108
     * @param array  $values
1109
     * @param string $message
1110
     *
1111
     * @throws InvalidArgumentException
1112
     */
1113 8
    public static function oneOf($value, array $values, $message = '')
1114
    {
1115 8
        static::inArray($value, $values, $message);
1116 4
    }
1117
1118
    /**
1119
     * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion.
1120
     *
1121
     * @psalm-pure
1122
     *
1123
     * @param mixed  $value
1124
     * @param array  $values
1125
     * @param string $message
1126
     *
1127
     * @throws InvalidArgumentException
1128
     */
1129 16
    public static function inArray($value, array $values, $message = '')
1130
    {
1131 16
        if (!\in_array($value, $values, true)) {
1132 8
            static::reportInvalidArgument(\sprintf(
1133 8
                $message ?: 'Expected one of: %2$s. Got: %s',
1134 8
                static::valueToString($value),
1135 8
                \implode(', ', \array_map(array('static', 'valueToString'), $values))
1136
            ));
1137
        }
1138 8
    }
1139
1140
    /**
1141
     * @psalm-pure
1142
     *
1143
     * @param string $value
1144
     * @param string $subString
1145
     * @param string $message
1146
     *
1147
     * @throws InvalidArgumentException
1148
     */
1149 80 View Code Duplication
    public static function contains($value, $subString, $message = '')
1150
    {
1151 80
        if (false === \strpos($value, $subString)) {
1152 32
            static::reportInvalidArgument(\sprintf(
1153 32
                $message ?: 'Expected a value to contain %2$s. Got: %s',
1154 32
                static::valueToString($value),
1155 32
                static::valueToString($subString)
1156
            ));
1157
        }
1158 48
    }
1159
1160
    /**
1161
     * @psalm-pure
1162
     *
1163
     * @param string $value
1164
     * @param string $subString
1165
     * @param string $message
1166
     *
1167
     * @throws InvalidArgumentException
1168
     */
1169 80 View Code Duplication
    public static function notContains($value, $subString, $message = '')
1170
    {
1171 80
        if (false !== \strpos($value, $subString)) {
1172 48
            static::reportInvalidArgument(\sprintf(
1173 48
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
1174 48
                static::valueToString($value),
1175 48
                static::valueToString($subString)
1176
            ));
1177
        }
1178 32
    }
1179
1180
    /**
1181
     * @psalm-pure
1182
     *
1183
     * @param string $value
1184
     * @param string $message
1185
     *
1186
     * @throws InvalidArgumentException
1187
     */
1188 40
    public static function notWhitespaceOnly($value, $message = '')
1189
    {
1190 40
        if (\preg_match('/^\s*$/', $value)) {
1191 24
            static::reportInvalidArgument(\sprintf(
1192 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
1193 24
                static::valueToString($value)
1194
            ));
1195
        }
1196 16
    }
1197
1198
    /**
1199
     * @psalm-pure
1200
     *
1201
     * @param string $value
1202
     * @param string $prefix
1203
     * @param string $message
1204
     *
1205
     * @throws InvalidArgumentException
1206
     */
1207 48 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
1208
    {
1209 48
        if (0 !== \strpos($value, $prefix)) {
1210 32
            static::reportInvalidArgument(\sprintf(
1211 32
                $message ?: 'Expected a value to start with %2$s. Got: %s',
1212 32
                static::valueToString($value),
1213 32
                static::valueToString($prefix)
1214
            ));
1215
        }
1216 16
    }
1217
1218
    /**
1219
     * @psalm-pure
1220
     *
1221
     * @param mixed  $value
1222
     * @param string $message
1223
     *
1224
     * @throws InvalidArgumentException
1225
     */
1226 35
    public static function startsWithLetter($value, $message = '')
1227
    {
1228 35
        static::string($value);
1229
1230 24
        $valid = isset($value[0]);
1231
1232 24
        if ($valid) {
1233 20
            $locale = \setlocale(LC_CTYPE, 0);
1234 20
            \setlocale(LC_CTYPE, 'C');
1235 20
            $valid = \ctype_alpha($value[0]);
1236 20
            \setlocale(LC_CTYPE, $locale);
1237
        }
1238
1239 24
        if (!$valid) {
1240 12
            static::reportInvalidArgument(\sprintf(
1241 12
                $message ?: 'Expected a value to start with a letter. Got: %s',
1242 12
                static::valueToString($value)
1243
            ));
1244
        }
1245 12
    }
1246
1247
    /**
1248
     * @psalm-pure
1249
     *
1250
     * @param string $value
1251
     * @param string $suffix
1252
     * @param string $message
1253
     *
1254
     * @throws InvalidArgumentException
1255
     */
1256 48 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
1257
    {
1258 48
        if ($suffix !== \substr($value, -\strlen($suffix))) {
1259 32
            static::reportInvalidArgument(\sprintf(
1260 32
                $message ?: 'Expected a value to end with %2$s. Got: %s',
1261 32
                static::valueToString($value),
1262 32
                static::valueToString($suffix)
1263
            ));
1264
        }
1265 16
    }
1266
1267
    /**
1268
     * @psalm-pure
1269
     *
1270
     * @param string $value
1271
     * @param string $pattern
1272
     * @param string $message
1273
     *
1274
     * @throws InvalidArgumentException
1275
     */
1276 12
    public static function regex($value, $pattern, $message = '')
1277
    {
1278 12
        if (!\preg_match($pattern, $value)) {
1279 8
            static::reportInvalidArgument(\sprintf(
1280 8
                $message ?: 'The value %s does not match the expected pattern.',
1281 8
                static::valueToString($value)
1282
            ));
1283
        }
1284 4
    }
1285
1286
    /**
1287
     * @psalm-pure
1288
     *
1289
     * @param string $value
1290
     * @param string $pattern
1291
     * @param string $message
1292
     *
1293
     * @throws InvalidArgumentException
1294
     */
1295 12
    public static function notRegex($value, $pattern, $message = '')
1296
    {
1297 12
        if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
1298 4
            static::reportInvalidArgument(\sprintf(
1299 4
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
1300 4
                static::valueToString($value),
1301 4
                static::valueToString($pattern),
1302 4
                $matches[0][1]
1303
            ));
1304
        }
1305 8
    }
1306
1307
    /**
1308
     * @psalm-pure
1309
     *
1310
     * @param mixed  $value
1311
     * @param string $message
1312
     *
1313
     * @throws InvalidArgumentException
1314
     */
1315 28 View Code Duplication
    public static function unicodeLetters($value, $message = '')
1316
    {
1317 28
        static::string($value);
1318
1319 28
        if (!\preg_match('/^\p{L}+$/u', $value)) {
1320 16
            static::reportInvalidArgument(\sprintf(
1321 16
                $message ?: 'Expected a value to contain only Unicode letters. Got: %s',
1322 16
                static::valueToString($value)
1323
            ));
1324
        }
1325 12
    }
1326
1327
    /**
1328
     * @psalm-pure
1329
     *
1330
     * @param mixed  $value
1331
     * @param string $message
1332
     *
1333
     * @throws InvalidArgumentException
1334
     */
1335 20 View Code Duplication
    public static function alpha($value, $message = '')
1336
    {
1337 20
        static::string($value);
1338
1339 12
        $locale = \setlocale(LC_CTYPE, 0);
1340 12
        \setlocale(LC_CTYPE, 'C');
1341 12
        $valid = !\ctype_alpha($value);
1342 12
        \setlocale(LC_CTYPE, $locale);
1343
1344 12
        if ($valid) {
1345 8
            static::reportInvalidArgument(\sprintf(
1346 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
1347 8
                static::valueToString($value)
1348
            ));
1349
        }
1350 4
    }
1351
1352
    /**
1353
     * @psalm-pure
1354
     *
1355
     * @param string $value
1356
     * @param string $message
1357
     *
1358
     * @throws InvalidArgumentException
1359
     */
1360 12 View Code Duplication
    public static function digits($value, $message = '')
1361
    {
1362 12
        $locale = \setlocale(LC_CTYPE, 0);
1363 12
        \setlocale(LC_CTYPE, 'C');
1364 12
        $valid = !\ctype_digit($value);
1365 12
        \setlocale(LC_CTYPE, $locale);
1366
1367 12
        if ($valid) {
1368 8
            static::reportInvalidArgument(\sprintf(
1369 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
1370 8
                static::valueToString($value)
1371
            ));
1372
        }
1373 4
    }
1374
1375
    /**
1376
     * @psalm-pure
1377
     *
1378
     * @param string $value
1379
     * @param string $message
1380
     *
1381
     * @throws InvalidArgumentException
1382
     */
1383 12 View Code Duplication
    public static function alnum($value, $message = '')
1384
    {
1385 12
        $locale = \setlocale(LC_CTYPE, 0);
1386 12
        \setlocale(LC_CTYPE, 'C');
1387 12
        $valid = !\ctype_alnum($value);
1388 12
        \setlocale(LC_CTYPE, $locale);
1389
1390 12
        if ($valid) {
1391 8
            static::reportInvalidArgument(\sprintf(
1392 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
1393 8
                static::valueToString($value)
1394
            ));
1395
        }
1396 4
    }
1397
1398
    /**
1399
     * @psalm-pure
1400
     * @psalm-assert lowercase-string $value
1401
     *
1402
     * @param string $value
1403
     * @param string $message
1404
     *
1405
     * @throws InvalidArgumentException
1406
     */
1407 16 View Code Duplication
    public static function lower($value, $message = '')
1408
    {
1409 16
        $locale = \setlocale(LC_CTYPE, 0);
1410 16
        \setlocale(LC_CTYPE, 'C');
1411 16
        $valid = !\ctype_lower($value);
1412 16
        \setlocale(LC_CTYPE, $locale);
1413
1414 16
        if ($valid) {
1415 12
            static::reportInvalidArgument(\sprintf(
1416 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
1417 12
                static::valueToString($value)
1418
            ));
1419
        }
1420 4
    }
1421
1422
    /**
1423
     * @psalm-pure
1424
     * @psalm-assert !lowercase-string $value
1425
     *
1426
     * @param string $value
1427
     * @param string $message
1428
     *
1429
     * @throws InvalidArgumentException
1430
     */
1431 16 View Code Duplication
    public static function upper($value, $message = '')
1432
    {
1433 16
        $locale = \setlocale(LC_CTYPE, 0);
1434 16
        \setlocale(LC_CTYPE, 'C');
1435 16
        $valid = !\ctype_upper($value);
1436 16
        \setlocale(LC_CTYPE, $locale);
1437
1438 16
        if ($valid) {
1439 12
            static::reportInvalidArgument(\sprintf(
1440 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
1441 12
                static::valueToString($value)
1442
            ));
1443
        }
1444 4
    }
1445
1446
    /**
1447
     * @psalm-pure
1448
     *
1449
     * @param string $value
1450
     * @param int    $length
1451
     * @param string $message
1452
     *
1453
     * @throws InvalidArgumentException
1454
     */
1455 36
    public static function length($value, $length, $message = '')
1456
    {
1457 36
        if ($length !== static::strlen($value)) {
1458 24
            static::reportInvalidArgument(\sprintf(
1459 24
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
1460 24
                static::valueToString($value),
1461 24
                $length
1462
            ));
1463
        }
1464 12
    }
1465
1466
    /**
1467
     * Inclusive min.
1468
     *
1469
     * @psalm-pure
1470
     *
1471
     * @param string    $value
1472
     * @param int|float $min
1473
     * @param string    $message
1474
     *
1475
     * @throws InvalidArgumentException
1476
     */
1477 36
    public static function minLength($value, $min, $message = '')
1478
    {
1479 36
        if (static::strlen($value) < $min) {
1480 12
            static::reportInvalidArgument(\sprintf(
1481 12
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
1482 12
                static::valueToString($value),
1483 12
                $min
1484
            ));
1485
        }
1486 24
    }
1487
1488
    /**
1489
     * Inclusive max.
1490
     *
1491
     * @psalm-pure
1492
     *
1493
     * @param string    $value
1494
     * @param int|float $max
1495
     * @param string    $message
1496
     *
1497
     * @throws InvalidArgumentException
1498
     */
1499 36
    public static function maxLength($value, $max, $message = '')
1500
    {
1501 36
        if (static::strlen($value) > $max) {
1502 12
            static::reportInvalidArgument(\sprintf(
1503 12
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
1504 12
                static::valueToString($value),
1505 12
                $max
1506
            ));
1507
        }
1508 24
    }
1509
1510
    /**
1511
     * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion.
1512
     *
1513
     * @psalm-pure
1514
     *
1515
     * @param string    $value
1516
     * @param int|float $min
1517
     * @param int|float $max
1518
     * @param string    $message
1519
     *
1520
     * @throws InvalidArgumentException
1521
     */
1522 60 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
1523
    {
1524 60
        $length = static::strlen($value);
1525
1526 60
        if ($length < $min || $length > $max) {
1527 24
            static::reportInvalidArgument(\sprintf(
1528 24
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
1529 24
                static::valueToString($value),
1530 24
                $min,
1531 24
                $max
1532
            ));
1533
        }
1534 36
    }
1535
1536
    /**
1537
     * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file.
1538
     *
1539
     * @param mixed  $value
1540
     * @param string $message
1541
     *
1542
     * @throws InvalidArgumentException
1543
     */
1544 36 View Code Duplication
    public static function fileExists($value, $message = '')
1545
    {
1546 36
        static::string($value);
1547
1548 36
        if (!\file_exists($value)) {
1549 12
            static::reportInvalidArgument(\sprintf(
1550 12
                $message ?: 'The file %s does not exist.',
1551 12
                static::valueToString($value)
1552
            ));
1553
        }
1554 24
    }
1555
1556
    /**
1557
     * @param mixed  $value
1558
     * @param string $message
1559
     *
1560
     * @throws InvalidArgumentException
1561
     */
1562 12 View Code Duplication
    public static function file($value, $message = '')
1563
    {
1564 12
        static::fileExists($value, $message);
1565
1566 8
        if (!\is_file($value)) {
1567 4
            static::reportInvalidArgument(\sprintf(
1568 4
                $message ?: 'The path %s is not a file.',
1569 4
                static::valueToString($value)
1570
            ));
1571
        }
1572 4
    }
1573
1574
    /**
1575
     * @param mixed  $value
1576
     * @param string $message
1577
     *
1578
     * @throws InvalidArgumentException
1579
     */
1580 12 View Code Duplication
    public static function directory($value, $message = '')
1581
    {
1582 12
        static::fileExists($value, $message);
1583
1584 8
        if (!\is_dir($value)) {
1585 4
            static::reportInvalidArgument(\sprintf(
1586 4
                $message ?: 'The path %s is no directory.',
1587 4
                static::valueToString($value)
1588
            ));
1589
        }
1590 4
    }
1591
1592
    /**
1593
     * @param string $value
1594
     * @param string $message
1595
     *
1596
     * @throws InvalidArgumentException
1597
     */
1598
    public static function readable($value, $message = '')
1599
    {
1600
        if (!\is_readable($value)) {
1601
            static::reportInvalidArgument(\sprintf(
1602
                $message ?: 'The path %s is not readable.',
1603
                static::valueToString($value)
1604
            ));
1605
        }
1606
    }
1607
1608
    /**
1609
     * @param string $value
1610
     * @param string $message
1611
     *
1612
     * @throws InvalidArgumentException
1613
     */
1614
    public static function writable($value, $message = '')
1615
    {
1616
        if (!\is_writable($value)) {
1617
            static::reportInvalidArgument(\sprintf(
1618
                $message ?: 'The path %s is not writable.',
1619
                static::valueToString($value)
1620
            ));
1621
        }
1622
    }
1623
1624
    /**
1625
     * @param mixed  $value
1626
     * @param string $message
1627
     *
1628
     * @throws InvalidArgumentException
1629
     */
1630 8
    public static function classExists($value, $message = '')
1631
    {
1632 8
        if (!\class_exists($value)) {
1633 4
            static::reportInvalidArgument(\sprintf(
1634 4
                $message ?: 'Expected an existing class name. Got: %s',
1635 4
                static::valueToString($value)
1636
            ));
1637
        }
1638 4
    }
1639
1640
    /**
1641
     * @psalm-pure
1642
     * @psalm-template ExpectedType of object
1643
     * @psalm-param class-string<ExpectedType> $class
1644
     * @psalm-assert class-string<ExpectedType>|ExpectedType $value
1645
     *
1646
     * @param mixed         $value
1647
     * @param string|object $class
1648
     * @param string        $message
1649
     *
1650
     * @throws InvalidArgumentException
1651
     */
1652 8
    public static function subclassOf($value, $class, $message = '')
1653
    {
1654 8
        if (!\is_subclass_of($value, $class)) {
1655 4
            static::reportInvalidArgument(\sprintf(
1656 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
1657 4
                static::valueToString($value),
1658 4
                static::valueToString($class)
1659
            ));
1660
        }
1661 4
    }
1662
1663
    /**
1664
     * @param mixed  $value
1665
     * @param string $message
1666
     *
1667
     * @throws InvalidArgumentException
1668
     */
1669 8
    public static function interfaceExists($value, $message = '')
1670
    {
1671 8
        if (!\interface_exists($value)) {
1672 4
            static::reportInvalidArgument(\sprintf(
1673 4
                $message ?: 'Expected an existing interface name. got %s',
1674 4
                static::valueToString($value)
1675
            ));
1676
        }
1677 4
    }
1678
1679
    /**
1680
     * @psalm-pure
1681
     * @psalm-template ExpectedType of object
1682
     * @psalm-param class-string<ExpectedType> $interface
1683
     * @psalm-assert class-string<ExpectedType> $value
1684
     *
1685
     * @param mixed  $value
1686
     * @param mixed  $interface
1687
     * @param string $message
1688
     *
1689
     * @throws InvalidArgumentException
1690
     */
1691 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
1692
    {
1693 8
        if (!\in_array($interface, \class_implements($value))) {
1694 4
            static::reportInvalidArgument(\sprintf(
1695 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
1696 4
                static::valueToString($value),
1697 4
                static::valueToString($interface)
1698
            ));
1699
        }
1700 4
    }
1701
1702
    /**
1703
     * @psalm-pure
1704
     * @psalm-param class-string|object $classOrObject
1705
     *
1706
     * @param string|object $classOrObject
1707
     * @param mixed         $property
1708
     * @param string        $message
1709
     *
1710
     * @throws InvalidArgumentException
1711
     */
1712 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
1713
    {
1714 12
        if (!\property_exists($classOrObject, $property)) {
1715 4
            static::reportInvalidArgument(\sprintf(
1716 4
                $message ?: 'Expected the property %s to exist.',
1717 4
                static::valueToString($property)
1718
            ));
1719
        }
1720 8
    }
1721
1722
    /**
1723
     * @psalm-pure
1724
     * @psalm-param class-string|object $classOrObject
1725
     *
1726
     * @param string|object $classOrObject
1727
     * @param mixed         $property
1728
     * @param string        $message
1729
     *
1730
     * @throws InvalidArgumentException
1731
     */
1732 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
1733
    {
1734 12
        if (\property_exists($classOrObject, $property)) {
1735 8
            static::reportInvalidArgument(\sprintf(
1736 8
                $message ?: 'Expected the property %s to not exist.',
1737 8
                static::valueToString($property)
1738
            ));
1739
        }
1740 4
    }
1741
1742
    /**
1743
     * @psalm-pure
1744
     * @psalm-param class-string|object $classOrObject
1745
     *
1746
     * @param string|object $classOrObject
1747
     * @param mixed         $method
1748
     * @param string        $message
1749
     *
1750
     * @throws InvalidArgumentException
1751
     */
1752 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
1753
    {
1754 27
        if (!\method_exists($classOrObject, $method)) {
1755 19
            static::reportInvalidArgument(\sprintf(
1756 19
                $message ?: 'Expected the method %s to exist.',
1757 19
                static::valueToString($method)
1758
            ));
1759
        }
1760 8
    }
1761
1762
    /**
1763
     * @psalm-pure
1764
     * @psalm-param class-string|object $classOrObject
1765
     *
1766
     * @param string|object $classOrObject
1767
     * @param mixed         $method
1768
     * @param string        $message
1769
     *
1770
     * @throws InvalidArgumentException
1771
     */
1772 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
1773
    {
1774 27
        if (\method_exists($classOrObject, $method)) {
1775 8
            static::reportInvalidArgument(\sprintf(
1776 8
                $message ?: 'Expected the method %s to not exist.',
1777 8
                static::valueToString($method)
1778
            ));
1779
        }
1780 19
    }
1781
1782
    /**
1783
     * @psalm-pure
1784
     *
1785
     * @param array      $array
1786
     * @param string|int $key
1787
     * @param string     $message
1788
     *
1789
     * @throws InvalidArgumentException
1790
     */
1791 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
1792
    {
1793 12
        if (!(isset($array[$key]) || \array_key_exists($key, $array))) {
1794 4
            static::reportInvalidArgument(\sprintf(
1795 4
                $message ?: 'Expected the key %s to exist.',
1796 4
                static::valueToString($key)
1797
            ));
1798
        }
1799 8
    }
1800
1801
    /**
1802
     * @psalm-pure
1803
     *
1804
     * @param array      $array
1805
     * @param string|int $key
1806
     * @param string     $message
1807
     *
1808
     * @throws InvalidArgumentException
1809
     */
1810 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
1811
    {
1812 12
        if (isset($array[$key]) || \array_key_exists($key, $array)) {
1813 8
            static::reportInvalidArgument(\sprintf(
1814 8
                $message ?: 'Expected the key %s to not exist.',
1815 8
                static::valueToString($key)
1816
            ));
1817
        }
1818 4
    }
1819
1820
    /**
1821
     * Checks if a value is a valid array key (int or string).
1822
     *
1823
     * @psalm-pure
1824
     * @psalm-assert array-key $value
1825
     *
1826
     * @param mixed  $value
1827
     * @param string $message
1828
     *
1829
     * @throws InvalidArgumentException
1830
     */
1831 28
    public static function validArrayKey($value, $message = '')
1832
    {
1833 28
        if (!(\is_int($value) || \is_string($value))) {
1834 20
            static::reportInvalidArgument(\sprintf(
1835 20
                $message ?: 'Expected string or integer. Got: %s',
1836 20
                static::typeToString($value)
1837
            ));
1838
        }
1839 8
    }
1840
1841
    /**
1842
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1843
     *
1844
     * @param Countable|array $array
1845
     * @param int             $number
1846
     * @param string          $message
1847
     *
1848
     * @throws InvalidArgumentException
1849
     */
1850 8
    public static function count($array, $number, $message = '')
1851
    {
1852 8
        static::eq(
1853 8
            \count($array),
1854
            $number,
1855 8
            \sprintf(
1856 8
                $message ?: 'Expected an array to contain %d elements. Got: %d.',
1857 8
                $number,
1858 8
                \count($array)
1859
            )
1860
        );
1861 4
    }
1862
1863
    /**
1864
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1865
     *
1866
     * @param Countable|array $array
1867
     * @param int|float       $min
1868
     * @param string          $message
1869
     *
1870
     * @throws InvalidArgumentException
1871
     */
1872 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
1873
    {
1874 12
        if (\count($array) < $min) {
1875 4
            static::reportInvalidArgument(\sprintf(
1876 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
1877 4
                \count($array),
1878 4
                $min
1879
            ));
1880
        }
1881 8
    }
1882
1883
    /**
1884
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1885
     *
1886
     * @param Countable|array $array
1887
     * @param int|float       $max
1888
     * @param string          $message
1889
     *
1890
     * @throws InvalidArgumentException
1891
     */
1892 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
1893
    {
1894 12
        if (\count($array) > $max) {
1895 4
            static::reportInvalidArgument(\sprintf(
1896 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
1897 4
                \count($array),
1898 4
                $max
1899
            ));
1900
        }
1901 8
    }
1902
1903
    /**
1904
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1905
     *
1906
     * @param Countable|array $array
1907
     * @param int|float       $min
1908
     * @param int|float       $max
1909
     * @param string          $message
1910
     *
1911
     * @throws InvalidArgumentException
1912
     */
1913 20
    public static function countBetween($array, $min, $max, $message = '')
1914
    {
1915 20
        $count = \count($array);
1916
1917 20
        if ($count < $min || $count > $max) {
1918 8
            static::reportInvalidArgument(\sprintf(
1919 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
1920 8
                $count,
1921 8
                $min,
1922 8
                $max
1923
            ));
1924
        }
1925 12
    }
1926
1927
    /**
1928
     * @psalm-pure
1929
     * @psalm-assert list $array
1930
     *
1931
     * @param mixed  $array
1932
     * @param string $message
1933
     *
1934
     * @throws InvalidArgumentException
1935
     */
1936 80 View Code Duplication
    public static function isList($array, $message = '')
1937
    {
1938 80
        if (!\is_array($array) || $array !== \array_values($array)) {
1939 32
            static::reportInvalidArgument(
1940 32
                $message ?: 'Expected list - non-associative array.'
1941
            );
1942
        }
1943 48
    }
1944
1945
    /**
1946
     * @psalm-pure
1947
     * @psalm-assert list $array
1948
     * @psalm-assert !empty $array
1949
     *
1950
     * @param mixed  $array
1951
     * @param string $message
1952
     *
1953
     * @throws InvalidArgumentException
1954
     */
1955 40
    public static function isNonEmptyList($array, $message = '')
1956
    {
1957 40
        static::isList($array, $message);
1958 24
        static::notEmpty($array, $message);
1959 20
    }
1960
1961
    /**
1962
     * @psalm-pure
1963
     * @psalm-template T
1964
     * @psalm-param mixed|array<T> $array
1965
     * @psalm-assert array<string, T> $array
1966
     *
1967
     * @param mixed  $array
1968
     * @param string $message
1969
     *
1970
     * @throws InvalidArgumentException
1971
     */
1972 32
    public static function isMap($array, $message = '')
1973
    {
1974
        if (
1975 32
            !\is_array($array) ||
1976 32
            \array_keys($array) !== \array_filter(\array_keys($array), '\is_string')
1977
        ) {
1978 16
            static::reportInvalidArgument(
1979 16
                $message ?: 'Expected map - associative array with string keys.'
1980
            );
1981
        }
1982 16
    }
1983
1984
    /**
1985
     * @psalm-pure
1986
     * @psalm-template T
1987
     * @psalm-param mixed|array<T> $array
1988
     * @psalm-assert array<string, T> $array
1989
     * @psalm-assert !empty $array
1990
     *
1991
     * @param mixed  $array
1992
     * @param string $message
1993
     *
1994
     * @throws InvalidArgumentException
1995
     */
1996 16
    public static function isNonEmptyMap($array, $message = '')
1997
    {
1998 16
        static::isMap($array, $message);
1999 8
        static::notEmpty($array, $message);
2000 4
    }
2001
2002
    /**
2003
     * @psalm-pure
2004
     *
2005
     * @param string $value
2006
     * @param string $message
2007
     *
2008
     * @throws InvalidArgumentException
2009
     */
2010 56
    public static function uuid($value, $message = '')
2011
    {
2012 56
        $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
2013
2014
        // The nil UUID is special form of UUID that is specified to have all
2015
        // 128 bits set to zero.
2016 56
        if ('00000000-0000-0000-0000-000000000000' === $value) {
2017 4
            return;
2018
        }
2019
2020 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)) {
2021 20
            static::reportInvalidArgument(\sprintf(
2022 20
                $message ?: 'Value %s is not a valid UUID.',
2023 20
                static::valueToString($value)
2024
            ));
2025
        }
2026 32
    }
2027
2028
    /**
2029
     * @psalm-param class-string<Throwable>
2030
     *
2031
     * @param Closure $expression
2032
     * @param string  $class
2033
     * @param string  $message
2034
     *
2035
     * @throws InvalidArgumentException
2036
     */
2037 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
2038
    {
2039 24
        static::string($class);
2040
2041 24
        $actual = 'none';
2042
2043
        try {
2044 24
            $expression();
2045 24
        } catch (Exception $e) {
2046 20
            $actual = \get_class($e);
2047 20
            if ($e instanceof $class) {
2048 20
                return;
2049
            }
2050 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...
2051 4
            $actual = \get_class($e);
2052 4
            if ($e instanceof $class) {
2053 4
                return;
2054
            }
2055
        }
2056
2057 8
        static::reportInvalidArgument($message ?: \sprintf(
2058 8
            'Expected to throw "%s", got "%s"',
2059 8
            $class,
2060 8
            $actual
2061
        ));
2062
    }
2063
2064
    /**
2065
     * @throws BadMethodCallException
2066
     */
2067 1568
    public static function __callStatic($name, $arguments)
2068
    {
2069 1568
        if ('nullOr' === \substr($name, 0, 6)) {
2070 581
            if (null !== $arguments[0]) {
2071 477
                $method = \lcfirst(\substr($name, 6));
2072 477
                \call_user_func_array(array('static', $method), $arguments);
2073
            }
2074
2075 336
            return;
2076
        }
2077
2078 987
        if ('all' === \substr($name, 0, 3)) {
2079 986
            static::isIterable($arguments[0]);
2080
2081 986
            $method = \lcfirst(\substr($name, 3));
2082 986
            $args = $arguments;
2083
2084 986
            foreach ($arguments[0] as $entry) {
2085 986
                $args[0] = $entry;
2086
2087 986
                \call_user_func_array(array('static', $method), $args);
2088
            }
2089
2090 472
            return;
2091
        }
2092
2093 1
        throw new BadMethodCallException('No such method: '.$name);
2094
    }
2095
2096
    /**
2097
     * @param mixed $value
2098
     *
2099
     * @return string
2100
     */
2101 719
    protected static function valueToString($value)
2102
    {
2103 719
        if (null === $value) {
2104 20
            return 'null';
2105
        }
2106
2107 701
        if (true === $value) {
2108 15
            return 'true';
2109
        }
2110
2111 691
        if (false === $value) {
2112 25
            return 'false';
2113
        }
2114
2115 666
        if (\is_array($value)) {
2116 21
            return 'array';
2117
        }
2118
2119 645
        if (\is_object($value)) {
2120 3
            if (\method_exists($value, '__toString')) {
2121 1
                return \get_class($value).': '.self::valueToString($value->__toString());
2122
            }
2123
2124 2
            if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
2125 1
                return \get_class($value).': '.self::valueToString($value->format('c'));
2126
            }
2127
2128 1
            return \get_class($value);
2129
        }
2130
2131 644
        if (\is_resource($value)) {
2132 1
            return 'resource';
2133
        }
2134
2135 644
        if (\is_string($value)) {
2136 542
            return '"'.$value.'"';
2137
        }
2138
2139 114
        return (string) $value;
2140
    }
2141
2142
    /**
2143
     * @param mixed $value
2144
     *
2145
     * @return string
2146
     */
2147 251
    protected static function typeToString($value)
2148
    {
2149 251
        return \is_object($value) ? \get_class($value) : \gettype($value);
2150
    }
2151
2152 168
    protected static function strlen($value)
2153
    {
2154 168
        if (!\function_exists('mb_detect_encoding')) {
2155
            return \strlen($value);
2156
        }
2157
2158 168
        if (false === $encoding = \mb_detect_encoding($value)) {
2159
            return \strlen($value);
2160
        }
2161
2162 168
        return \mb_strlen($value, $encoding);
2163
    }
2164
2165
    /**
2166
     * @param string $message
2167
     *
2168
     * @throws InvalidArgumentException
2169
     *
2170
     * @psalm-pure this method is not supposed to perform side-effects
2171
     */
2172 1033
    protected static function reportInvalidArgument($message)
2173
    {
2174 1033
        throw new InvalidArgumentException($message);
2175
    }
2176
2177
    private function __construct()
2178
    {
2179
    }
2180
}
2181