Failed Conditions
Pull Request — master (#221)
by
unknown
32:03 queued 12:03
created

Assert::typeToString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
 * @mixin Mixin
31
 *
32
 * @since  1.0
33
 *
34
 * @author Bernhard Schussek <[email protected]>
35
 */
36
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...
37
{
38
    /**
39
     * @psalm-pure
40
     * @psalm-assert string $value
41
     *
42
     * @param mixed                    $value
43
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
44
     *
45
     * @throws InvalidArgumentException
46
     */
47 241
    public static function string($value, $message = '')
48
    {
49 241
        if (!\is_string($value)) {
50 45
            static::reportInvalidArgument(
51 45
                is_callable($message)
52 45
                    ? $message()
53
                    : \sprintf(
54
                        $message ?: 'Expected a string. Got: %s',
55 196
                        static::typeToString($value)
56
                    )
57
            );
58
        }
59
    }
60
61
    /**
62
     * @psalm-pure
63
     * @psalm-assert non-empty-string $value
64
     *
65
     * @param mixed                    $value
66 16
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
67
     *
68 16
     * @throws InvalidArgumentException
69 12
     */
70 8
    public static function stringNotEmpty($value, $message = '')
71
    {
72
        static::string($value, $message);
73
        static::notEq($value, '', $message);
74
    }
75
76
    /**
77
     * @psalm-pure
78
     * @psalm-assert int $value
79
     *
80
     * @param mixed                    $value
81 17
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
82
     *
83 17
     * @throws InvalidArgumentException
84 13
     */
85 13
    public static function integer($value, $message = '')
86 13
    {
87
        if (!\is_int($value)) {
88
            static::reportInvalidArgument(
89 4
                is_callable($message)
90
                    ? $message()
91
                    : sprintf(
92
                        $message ?: 'Expected an integer. Got: %s',
93
                        static::typeToString($value)
94
                    )
95
            );
96
        }
97
    }
98
99
    /**
100 16
     * @psalm-pure
101
     * @psalm-assert numeric $value
102 16
     *
103 4
     * @param mixed                    $value
104 4
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
105 4
     *
106
     * @throws InvalidArgumentException
107
     */
108 12 View Code Duplication
    public static function integerish($value, $message = '')
109
    {
110
        if (!\is_numeric($value) || $value != (int) $value) {
111
            static::reportInvalidArgument(
112
                is_callable($message)
113
                    ? $message()
114
                    : sprintf(
115
                        $message ?: 'Expected an integerish value. Got: %s',
116
                        static::typeToString($value)
117
                    )
118
            );
119 16
        }
120
    }
121 16
122 8
    /**
123 8
     * @psalm-pure
124 8
     * @psalm-assert float $value
125
     *
126
     * @param mixed                    $value
127 8
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
128
     *
129
     * @throws InvalidArgumentException
130
     */
131
    public static function float($value, $message = '')
132
    {
133
        if (!\is_float($value)) {
134
            static::reportInvalidArgument(
135
                is_callable($message)
136
                    ? $message()
137
                    : sprintf(
138 20
                        $message ?: 'Expected a float. Got: %s',
139
                        static::typeToString($value)
140 20
                    )
141 4
            );
142 4
        }
143 4
    }
144
145
    /**
146 16
     * @psalm-pure
147
     * @psalm-assert numeric $value
148
     *
149
     * @param mixed                    $value
150
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
151
     *
152
     * @throws InvalidArgumentException
153
     */
154
    public static function numeric($value, $message = '')
155
    {
156
        if (!\is_numeric($value)) {
157 24
            static::reportInvalidArgument(
158
                is_callable($message)
159 24
                    ? $message()
160 16
                    : sprintf(
161 16
                        $message ?: 'Expected a numeric. Got: %s',
162 16
                        static::typeToString($value)
163
                    )
164
            );
165 8
        }
166
    }
167
168
    /**
169
     * @psalm-pure
170
     * @psalm-assert int $value
171
     *
172
     * @param mixed                    $value
173
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
174
     *
175
     * @throws InvalidArgumentException
176 16
     */
177
    public static function natural($value, $message = '')
178 16
    {
179 8
        if (!\is_int($value) || $value < 0) {
180 8
            static::reportInvalidArgument(\sprintf(
181 8
                $message ?: 'Expected a non-negative integer. Got: %s',
182
                static::valueToString($value)
183
            ));
184 8
        }
185
    }
186
187
    /**
188
     * @psalm-pure
189
     * @psalm-assert bool $value
190
     *
191
     * @param mixed                    $value
192
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
193
     *
194
     * @throws InvalidArgumentException
195 23
     */
196
    public static function boolean($value, $message = '')
197 23
    {
198 11
        if (!\is_bool($value)) {
199 11
            static::reportInvalidArgument(
200 11
                is_callable($message)
201
                    ? $message()
202
                    : sprintf(
203 12
                        $message ?: 'Expected a boolean. Got: %s',
204
                        static::typeToString($value)
205
                    )
206
            );
207
        }
208
    }
209
210
    /**
211
     * @psalm-pure
212
     * @psalm-assert scalar $value
213
     *
214 23
     * @param mixed                    $value
215
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
216 23
     *
217 15
     * @throws InvalidArgumentException
218 15
     */
219 15
    public static function scalar($value, $message = '')
220
    {
221
        if (!\is_scalar($value)) {
222 8
            static::reportInvalidArgument(
223
                is_callable($message)
224
                    ? $message()
225
                    : sprintf(
226
                        $message ?: 'Expected a scalar. Got: %s',
227
                        static::typeToString($value)
228
                    )
229
            );
230
        }
231
    }
232
233
    /**
234 16
     * @psalm-pure
235
     * @psalm-assert object $value
236 16
     *
237 4
     * @param mixed                    $value
238 4
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
239 4
     *
240
     * @throws InvalidArgumentException
241
     */
242
    public static function object($value, $message = '')
243 12
    {
244 4
        if (!\is_object($value)) {
245 4
            static::reportInvalidArgument(
246 4
                is_callable($message)
247 4
                    ? $message()
248
                    : sprintf(
249
                        $message ?: 'Expected an object. Got: %s',
250 8
                        static::typeToString($value)
251
                    )
252
            );
253
        }
254
    }
255
256
    /**
257
     * @psalm-pure
258
     * @psalm-assert resource $value
259
     *
260
     * @param mixed       $value
261 20
     * @param string|null $type    type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php
262
     * @param string      $message
263 20
     *
264 8
     * @throws InvalidArgumentException
265 8
     */
266 8
    public static function resource($value, $type = null, $message = '')
267
    {
268
        if (!\is_resource($value)) {
269 12
            static::reportInvalidArgument(
270
                is_callable($message)
271
                    ? $message()
272
                    : sprintf(
273
                        $message ?: 'Expected a resource. Got: %s',
274
                        static::typeToString($value)
275
                    )
276
            );
277
        }
278
279
        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...
280 20
            static::reportInvalidArgument(\sprintf(
281
                $message ?: 'Expected a resource of type %2$s. Got: %s',
282 20
                static::typeToString($value),
283 12
                $type
284 12
            ));
285 12
        }
286
    }
287
288 8
    /**
289
     * @psalm-pure
290
     * @psalm-assert callable $value
291
     *
292
     * @param mixed                    $value
293
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
294
     *
295
     * @throws InvalidArgumentException
296
     */
297
    public static function isCallable($value, $message = '')
298
    {
299
        if (!\is_callable($value)) {
300
            static::reportInvalidArgument(
301 20
                is_callable($message)
302
                    ? $message()
303 20
                    : sprintf(
304 20
                        $message ?: 'Expected a callable. Got: %s',
305 20
                        static::typeToString($value)
306 20
                    )
307
            );
308 20
        }
309
    }
310
311 20
    /**
312 8
     * @psalm-pure
313 8
     * @psalm-assert array $value
314 8
     *
315
     * @param mixed                    $value
316
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
317 12
     *
318
     * @throws InvalidArgumentException
319
     */
320 View Code Duplication
    public static function isArray($value, $message = '')
321
    {
322
        if (!\is_array($value)) {
323
            static::reportInvalidArgument(
324
                is_callable($message)
325
                    ? $message()
326
                    : sprintf(
327
                        $message ?: 'Expected an array. Got: %s',
328 20
                        static::typeToString($value)
329
                    )
330 20
            );
331 8
        }
332 8
    }
333 8
334
    /**
335
     * @psalm-pure
336 12
     * @psalm-assert iterable $value
337
     *
338
     * @deprecated use "isIterable" or "isInstanceOf" instead
339
     *
340
     * @param mixed                    $value
341
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
342
     *
343
     * @throws InvalidArgumentException
344
     */
345
    public static function isTraversable($value, $message = '')
346
    {
347 28
        @\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...
348
            \sprintf(
349
                '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.',
350 28
                __METHOD__
351 28
            ),
352 28
            \E_USER_DEPRECATED
353 28
        );
354
355 12
        if (!\is_array($value) && !($value instanceof Traversable)) {
356 12
            static::reportInvalidArgument(
357 12
                is_callable($message)
358
                    ? $message()
359
                    : sprintf(
360 16
                        $message ?: 'Expected a traversable. Got: %s',
361
                        static::typeToString($value)
362
                    )
363
            );
364
        }
365
    }
366
367
    /**
368
     * @psalm-pure
369
     * @psalm-assert array|ArrayAccess $value
370
     *
371 1044
     * @param mixed                    $value
372
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
373 1044
     *
374 8
     * @throws InvalidArgumentException
375 8
     */
376 8
    public static function isArrayAccessible($value, $message = '')
377
    {
378
        if (!\is_array($value) && !($value instanceof ArrayAccess)) {
379 1040
            static::reportInvalidArgument(
380
                is_callable($message)
381
                    ? $message()
382
                    : sprintf(
383
                        $message ?: 'Expected an array accessible. Got: %s',
384
                        static::typeToString($value)
385
                    )
386
            );
387
        }
388
    }
389
390
    /**
391
     * @psalm-pure
392
     * @psalm-assert countable $value
393 19
     *
394
     * @param mixed                    $value
395 19
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
396 15
     *
397 15
     * @throws InvalidArgumentException
398 15
     */
399 15
    public static function isCountable($value, $message = '')
400
    {
401
        if (
402 4
            !\is_array($value)
403
            && !($value instanceof Countable)
404
            && !($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...
405
            && !($value instanceof SimpleXMLElement)
406
        ) {
407
            static::reportInvalidArgument(
408
                is_callable($message)
409
                    ? $message()
410
                    : sprintf(
411
                        $message ?: 'Expected a countable. Got: %s',
412
                        static::typeToString($value)
413
                    )
414
            );
415
        }
416 16
    }
417
418 16
    /**
419 4
     * @psalm-pure
420 4
     * @psalm-assert iterable $value
421 4
     *
422 4
     * @param mixed                    $value
423
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
424
     *
425 12
     * @throws InvalidArgumentException
426
     */
427
    public static function isIterable($value, $message = '')
428
    {
429
        if (!\is_array($value) && !($value instanceof Traversable)) {
430
            static::reportInvalidArgument(
431
                is_callable($message)
432
                    ? $message()
433
                    : sprintf(
434
                        $message ?: 'Expected an iterable. Got: %s',
435
                        static::typeToString($value)
436
                    )
437 20
            );
438
        }
439 20
    }
440 20
441 8
    /**
442
     * @psalm-pure
443
     * @psalm-template ExpectedType of object
444
     * @psalm-param class-string<ExpectedType> $class
445 12
     * @psalm-assert ExpectedType $value
446 12
     *
447 12
     * @param mixed         $value
448 12
     * @param string|object $class
449
     * @param string        $message
450
     *
451
     * @throws InvalidArgumentException
452
     */
453 View Code Duplication
    public static function isInstanceOf($value, $class, $message = '')
454
    {
455
        if (!($value instanceof $class)) {
456
            static::reportInvalidArgument(\sprintf(
457
                $message ?: 'Expected an instance of %2$s. Got: %s',
458
                static::typeToString($value),
459
                $class
460
            ));
461
        }
462
    }
463
464 20
    /**
465
     * @psalm-pure
466 20
     * @psalm-template ExpectedType of object
467
     * @psalm-param class-string<ExpectedType> $class
468 16
     * @psalm-assert !ExpectedType $value
469 12
     *
470 12
     * @param mixed         $value
471 12
     * @param string|object $class
472
     * @param string        $message
473
     *
474
     * @throws InvalidArgumentException
475 4
     */
476 View Code Duplication
    public static function notInstanceOf($value, $class, $message = '')
477
    {
478
        if ($value instanceof $class) {
479
            static::reportInvalidArgument(\sprintf(
480
                $message ?: 'Expected an instance other than %2$s. Got: %s',
481
                static::typeToString($value),
482
                $class
483
            ));
484
        }
485
    }
486
487
    /**
488
     * @psalm-pure
489
     * @psalm-param array<class-string> $classes
490 20
     *
491
     * @param mixed                $value
492 20
     * @param array<object|string> $classes
493
     * @param string               $message
494 16
     *
495 4
     * @throws InvalidArgumentException
496 4
     */
497 4
    public static function isInstanceOfAny($value, array $classes, $message = '')
498
    {
499
        foreach ($classes as $class) {
500
            if ($value instanceof $class) {
501 12
                return;
502
            }
503
        }
504
505
        static::reportInvalidArgument(\sprintf(
506
            $message ?: 'Expected an instance of any of %2$s. Got: %s',
507
            static::typeToString($value),
508
            \implode(', ', \array_map(['static', 'valueToString'], $classes))
509
        ));
510
    }
511
512
    /**
513 24
     * @psalm-pure
514
     * @psalm-template ExpectedType of object
515 24
     * @psalm-param class-string<ExpectedType> $class
516 24
     * @psalm-assert ExpectedType|class-string<ExpectedType> $value
517
     *
518 20
     * @param object|string $value
519 8
     * @param string        $class
520
     * @param string        $message
521
     *
522
     * @throws InvalidArgumentException
523 12
     */
524 12 View Code Duplication
    public static function isAOf($value, $class, $message = '')
525 12
    {
526 12
        static::string($class, 'Expected class as a string. Got: %s');
527
528
        if (!\is_a($value, $class, \is_string($value))) {
529
            static::reportInvalidArgument(sprintf(
530
                $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s',
531
                static::typeToString($value),
532
                $class
533
            ));
534
        }
535
    }
536
537
    /**
538
     * @psalm-pure
539 23
     * @psalm-template UnexpectedType of object
540
     * @psalm-param class-string<UnexpectedType> $class
541 23
     * @psalm-assert !UnexpectedType $value
542 8
     * @psalm-assert !class-string<UnexpectedType> $value
543 8
     *
544 8
     * @param object|string $value
545
     * @param string        $class
546
     * @param string        $message
547 15
     *
548
     * @throws InvalidArgumentException
549
     */
550 View Code Duplication
    public static function isNotA($value, $class, $message = '')
551
    {
552
        static::string($class, 'Expected class as a string. Got: %s');
553
554
        if (\is_a($value, $class, \is_string($value))) {
555
            static::reportInvalidArgument(sprintf(
556
                $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s',
557
                static::typeToString($value),
558 55
                $class
559
            ));
560 55
        }
561 23
    }
562 23
563 23
    /**
564
     * @psalm-pure
565
     * @psalm-param array<class-string> $classes
566 32
     *
567
     * @param object|string $value
568
     * @param string[]      $classes
569
     * @param string        $message
570
     *
571
     * @throws InvalidArgumentException
572
     */
573
    public static function isAnyOf($value, array $classes, $message = '')
574
    {
575
        foreach ($classes as $class) {
576
            static::string($class, 'Expected class as a string. Got: %s');
577 11
578
            if (\is_a($value, $class, \is_string($value))) {
579 11
                return;
580 8
            }
581 8
        }
582 8
583
        static::reportInvalidArgument(sprintf(
584
            $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s',
585 3
            static::typeToString($value),
586
            \implode(', ', \array_map(['static', 'valueToString'], $classes))
587
        ));
588
    }
589
590
    /**
591
     * @psalm-pure
592
     * @psalm-assert empty $value
593
     *
594
     * @param mixed                    $value
595
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
596 11
     *
597
     * @throws InvalidArgumentException
598 11
     */
599 3
    public static function isEmpty($value, $message = '')
600 3
    {
601
        if (!empty($value)) {
602
            static::reportInvalidArgument(\sprintf(
603 8
                $message ?: 'Expected an empty value. Got: %s',
604
                static::valueToString($value)
605
            ));
606
        }
607
    }
608
609
    /**
610
     * @psalm-pure
611
     * @psalm-assert !empty $value
612
     *
613
     * @param mixed                    $value
614 15
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
615
     *
616 15
     * @throws InvalidArgumentException
617 11
     */
618 11
    public static function notEmpty($value, $message = '')
619 11
    {
620
        if (empty($value)) {
621
            static::reportInvalidArgument(\sprintf(
622 4
                $message ?: 'Expected a non-empty value. Got: %s',
623
                static::valueToString($value)
624
            ));
625
        }
626
    }
627
628
    /**
629
     * @psalm-pure
630
     * @psalm-assert null $value
631
     *
632
     * @param mixed                    $value
633 19
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
634
     *
635 19
     * @throws InvalidArgumentException
636 15
     */
637 15 View Code Duplication
    public static function null($value, $message = '')
638 15
    {
639
        if (null !== $value) {
640
            static::reportInvalidArgument(\sprintf(
641 4
                $message ?: 'Expected null. Got: %s',
642
                static::valueToString($value)
643
            ));
644
        }
645
    }
646
647
    /**
648
     * @psalm-pure
649
     * @psalm-assert !null $value
650
     *
651
     * @param mixed                    $value
652 19
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
653
     *
654 19
     * @throws InvalidArgumentException
655 4
     */
656 4
    public static function notNull($value, $message = '')
657
    {
658
        if (null === $value) {
659 15
            static::reportInvalidArgument(
660
                $message ?: 'Expected a value other than null.'
661
            );
662
        }
663
    }
664
665
    /**
666
     * @psalm-pure
667 51
     * @psalm-assert true $value
668
     *
669 51
     * @param mixed                    $value
670 19
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
671 19
     *
672 19
     * @throws InvalidArgumentException
673
     */
674 View Code Duplication
    public static function true($value, $message = '')
675 32
    {
676
        if (true !== $value) {
677
            static::reportInvalidArgument(\sprintf(
678
                $message ?: 'Expected a value to be true. Got: %s',
679
                static::valueToString($value)
680
            ));
681
        }
682
    }
683 51
684
    /**
685 51
     * @psalm-pure
686 35
     * @psalm-assert false $value
687 35
     *
688 35
     * @param mixed                    $value
689
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
690
     *
691 16
     * @throws InvalidArgumentException
692
     */
693 View Code Duplication
    public static function false($value, $message = '')
694
    {
695
        if (false !== $value) {
696
            static::reportInvalidArgument(\sprintf(
697
                $message ?: 'Expected a value to be false. Got: %s',
698
                static::valueToString($value)
699 51
            ));
700
        }
701 51
    }
702 31
703 31
    /**
704 31
     * @psalm-pure
705
     * @psalm-assert !false $value
706
     *
707 20
     * @param mixed                    $value
708
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
709
     *
710
     * @throws InvalidArgumentException
711
     */
712
    public static function notFalse($value, $message = '')
713
    {
714
        if (false === $value) {
715 20
            static::reportInvalidArgument(
716
                $message ?: 'Expected a value other than false.'
717 20
            );
718 12
        }
719 12
    }
720 12
721
    /**
722
     * @param mixed                    $value
723 8
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
724
     *
725
     * @throws InvalidArgumentException
726
     */
727 View Code Duplication
    public static function ip($value, $message = '')
728
    {
729
        if (false === \filter_var($value, \FILTER_VALIDATE_IP)) {
730
            static::reportInvalidArgument(\sprintf(
731
                $message ?: 'Expected a value to be an IP. Got: %s',
732
                static::valueToString($value)
733 12
            ));
734
        }
735 12
    }
736 12
737
    /**
738 12
     * @param mixed                    $value
739 8
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
740
     *
741 8
     * @throws InvalidArgumentException
742 8
     */
743 8 View Code Duplication
    public static function ipv4($value, $message = '')
744 8
    {
745
        if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
746
            static::reportInvalidArgument(\sprintf(
747 4
                $message ?: 'Expected a value to be an IPv4. Got: %s',
748
                static::valueToString($value)
749
            ));
750
        }
751
    }
752
753
    /**
754
     * @param mixed                    $value
755
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
756 33
     *
757
     * @throws InvalidArgumentException
758 33
     */
759 17 View Code Duplication
    public static function ipv6($value, $message = '')
760 17
    {
761 17
        if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
762 17
            static::reportInvalidArgument(\sprintf(
763
                $message ?: 'Expected a value to be an IPv6. Got: %s',
764
                static::valueToString($value)
765 16
            ));
766
        }
767
    }
768
769
    /**
770
     * @param mixed                    $value
771
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
772
     *
773
     * @throws InvalidArgumentException
774 28
     */
775 View Code Duplication
    public static function email($value, $message = '')
776 28
    {
777 16
        if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) {
778 16
            static::reportInvalidArgument(\sprintf(
779 16
                $message ?: 'Expected a value to be a valid e-mail address. Got: %s',
780
                static::valueToString($value)
781
            ));
782 12
        }
783
    }
784
785
    /**
786
     * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion.
787
     *
788
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
789
     *
790
     * @throws InvalidArgumentException
791
     */
792
    public static function uniqueValues(array $values, $message = '')
793 16
    {
794
        $allValues = \count($values);
795 16
        $uniqueValues = \count(\array_unique($values));
796 12
797 12
        if ($allValues !== $uniqueValues) {
798 12
            $difference = $allValues - $uniqueValues;
799 12
800
            static::reportInvalidArgument(\sprintf(
801
                $message ?: 'Expected an array of unique values, but %s of them %s duplicated',
802 4
                $difference,
803
                (1 === $difference ? 'is' : 'are')
804
            ));
805
        }
806
    }
807
808
    /**
809
     * @param mixed                    $value
810
     * @param mixed  $expect
811
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
812
     *
813 16
     * @throws InvalidArgumentException
814
     */
815 16
    public static function eq($value, $expect, $message = '')
816 4
    {
817 4
        if ($expect != $value) {
818 4
            static::reportInvalidArgument(\sprintf(
819
                $message ?: 'Expected a value equal to %2$s. Got: %s',
820
                static::valueToString($value),
821 12
                static::valueToString($expect)
822
            ));
823
        }
824
    }
825
826
    /**
827
     * @param mixed                    $value
828
     * @param mixed  $expect
829
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
830
     *
831
     * @throws InvalidArgumentException
832 8
     */
833
    public static function notEq($value, $expect, $message = '')
834 8
    {
835 4
        if ($expect == $value) {
836 4
            static::reportInvalidArgument(\sprintf(
837 4
                $message ?: 'Expected a different value than %s.',
838 4
                static::valueToString($expect)
839
            ));
840
        }
841 4
    }
842
843
    /**
844
     * @psalm-pure
845
     *
846
     * @param mixed                    $value
847
     * @param mixed  $expect
848
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
849
     *
850
     * @throws InvalidArgumentException
851
     */
852 12
    public static function same($value, $expect, $message = '')
853
    {
854 12
        if ($expect !== $value) {
855 4
            static::reportInvalidArgument(\sprintf(
856 4
                $message ?: 'Expected a value identical to %2$s. Got: %s',
857 4
                static::valueToString($value),
858 4
                static::valueToString($expect)
859
            ));
860
        }
861 8
    }
862
863
    /**
864
     * @psalm-pure
865
     *
866
     * @param mixed                    $value
867
     * @param mixed  $expect
868
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
869
     *
870
     * @throws InvalidArgumentException
871
     */
872 9
    public static function notSame($value, $expect, $message = '')
873
    {
874 9
        if ($expect === $value) {
875 5
            static::reportInvalidArgument(\sprintf(
876 5
                $message ?: 'Expected a value not identical to %s.',
877 5
                static::valueToString($expect)
878 5
            ));
879
        }
880
    }
881 4
882
    /**
883
     * @psalm-pure
884
     *
885
     * @param mixed                    $value
886
     * @param mixed                    $limit
887
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
888
     *
889
     * @throws InvalidArgumentException
890
     */
891
    public static function greaterThan($value, $limit, $message = '')
892 12
    {
893
        if ($value <= $limit) {
894 12
            static::reportInvalidArgument(\sprintf(
895 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
896 4
                static::valueToString($value),
897 4
                static::valueToString($limit)
898 4
            ));
899
        }
900
    }
901 8
902
    /**
903
     * @psalm-pure
904
     *
905
     * @param mixed                    $value
906
     * @param mixed                    $limit
907
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
908
     *
909
     * @throws InvalidArgumentException
910
     */
911
    public static function greaterThanEq($value, $limit, $message = '')
912
    {
913
        if ($value < $limit) {
914
            static::reportInvalidArgument(\sprintf(
915 16
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
916
                static::valueToString($value),
917 16
                static::valueToString($limit)
918 8
            ));
919 8
        }
920 8
    }
921 8
922 8
    /**
923
     * @psalm-pure
924
     *
925 8
     * @param mixed                    $value
926
     * @param mixed                    $limit
927
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
928
     *
929
     * @throws InvalidArgumentException
930
     */
931
    public static function lessThan($value, $limit, $message = '')
932
    {
933
        if ($value >= $limit) {
934
            static::reportInvalidArgument(\sprintf(
935
                $message ?: 'Expected a value less than %2$s. Got: %s',
936
                static::valueToString($value),
937
                static::valueToString($limit)
938 8
            ));
939
        }
940 8
    }
941 4
942
    /**
943
     * @psalm-pure
944
     *
945
     * @param mixed                    $value
946
     * @param mixed                    $limit
947
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
948
     *
949
     * @throws InvalidArgumentException
950
     */
951
    public static function lessThanEq($value, $limit, $message = '')
952
    {
953
        if ($value > $limit) {
954 16
            static::reportInvalidArgument(\sprintf(
955
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
956 16
                static::valueToString($value),
957 8
                static::valueToString($limit)
958 8
            ));
959 8
        }
960 8
    }
961
962
    /**
963 8
     * Inclusive range, so Assert::(3, 3, 5) passes.
964
     *
965
     * @psalm-pure
966
     *
967
     * @param mixed                    $value
968
     * @param mixed  $min
969
     * @param mixed  $max
970
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
971
     *
972
     * @throws InvalidArgumentException
973
     */
974 80 View Code Duplication
    public static function range($value, $min, $max, $message = '')
975
    {
976 80
        if ($value < $min || $value > $max) {
977 32
            static::reportInvalidArgument(\sprintf(
978 32
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
979 32
                static::valueToString($value),
980 32
                static::valueToString($min),
981
                static::valueToString($max)
982
            ));
983 48
        }
984
    }
985
986
    /**
987
     * A more human-readable alias of Assert::inArray().
988
     *
989
     * @psalm-pure
990
     *
991
     * @param mixed                    $value
992
     * @param array                    $values
993
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
994 80
     *
995
     * @throws InvalidArgumentException
996 80
     */
997 48
    public static function oneOf($value, array $values, $message = '')
998 48
    {
999 48
        static::inArray($value, $values, $message);
1000 48
    }
1001
1002
    /**
1003 32
     * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion.
1004
     *
1005
     * @psalm-pure
1006
     *
1007
     * @param mixed                    $value
1008
     * @param array                    $values
1009
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1010
     *
1011
     * @throws InvalidArgumentException
1012
     */
1013 40
    public static function inArray($value, array $values, $message = '')
1014
    {
1015 40
        if (!\in_array($value, $values, true)) {
1016 24
            static::reportInvalidArgument(\sprintf(
1017 24
                $message ?: 'Expected one of: %2$s. Got: %s',
1018 24
                static::valueToString($value),
1019
                \implode(', ', \array_map(array('static', 'valueToString'), $values))
1020
            ));
1021 16
        }
1022
    }
1023
1024
    /**
1025
     * @psalm-pure
1026
     *
1027
     * @param string                   $value
1028
     * @param string                   $subString
1029
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1030
     *
1031
     * @throws InvalidArgumentException
1032 48
     */
1033 View Code Duplication
    public static function contains($value, $subString, $message = '')
1034 48
    {
1035 32
        if (false === \strpos($value, $subString)) {
1036 32
            static::reportInvalidArgument(\sprintf(
1037 32
                $message ?: 'Expected a value to contain %2$s. Got: %s',
1038 32
                static::valueToString($value),
1039
                static::valueToString($subString)
1040
            ));
1041 16
        }
1042
    }
1043
1044
    /**
1045
     * @psalm-pure
1046
     *
1047
     * @param string                   $value
1048
     * @param string                   $subString
1049
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1050
     *
1051
     * @throws InvalidArgumentException
1052 48
     */
1053 View Code Duplication
    public static function notContains($value, $subString, $message = '')
1054 48
    {
1055 16
        if (false !== \strpos($value, $subString)) {
1056 16
            static::reportInvalidArgument(\sprintf(
1057 16
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
1058 16
                static::valueToString($value),
1059
                static::valueToString($subString)
1060
            ));
1061 32
        }
1062
    }
1063
1064
    /**
1065
     * @psalm-pure
1066
     *
1067
     * @param string                   $value
1068
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1069
     *
1070
     * @throws InvalidArgumentException
1071 35
     */
1072
    public static function notWhitespaceOnly($value, $message = '')
1073 35
    {
1074
        if (\preg_match('/^\s*$/', $value)) {
1075 24
            static::reportInvalidArgument(\sprintf(
1076
                $message ?: 'Expected a non-whitespace string. Got: %s',
1077 24
                static::valueToString($value)
1078 20
            ));
1079 20
        }
1080 20
    }
1081 20
1082
    /**
1083
     * @psalm-pure
1084 24
     *
1085 12
     * @param string                   $value
1086 12
     * @param string                   $prefix
1087 12
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1088
     *
1089
     * @throws InvalidArgumentException
1090 12
     */
1091 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
1092
    {
1093
        if (0 !== \strpos($value, $prefix)) {
1094
            static::reportInvalidArgument(\sprintf(
1095
                $message ?: 'Expected a value to start with %2$s. Got: %s',
1096
                static::valueToString($value),
1097
                static::valueToString($prefix)
1098
            ));
1099
        }
1100
    }
1101 48
1102
    /**
1103 48
     * @psalm-pure
1104 32
     *
1105 32
     * @param string                   $value
1106 32
     * @param string                   $prefix
1107 32
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1108
     *
1109
     * @throws InvalidArgumentException
1110 16
     */
1111 View Code Duplication
    public static function notStartsWith($value, $prefix, $message = '')
1112
    {
1113
        if (0 === \strpos($value, $prefix)) {
1114
            static::reportInvalidArgument(\sprintf(
1115
                $message ?: 'Expected a value not to start with %2$s. Got: %s',
1116
                static::valueToString($value),
1117
                static::valueToString($prefix)
1118
            ));
1119
        }
1120
    }
1121 48
1122
    /**
1123 48
     * @psalm-pure
1124 16
     *
1125 16
     * @param mixed                    $value
1126 16
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1127 16
     *
1128
     * @throws InvalidArgumentException
1129
     */
1130 32
    public static function startsWithLetter($value, $message = '')
1131
    {
1132
        static::string($value);
1133
1134
        $valid = isset($value[0]);
1135
1136
        if ($valid) {
1137
            $locale = \setlocale(LC_CTYPE, 0);
1138
            \setlocale(LC_CTYPE, 'C');
1139
            $valid = \ctype_alpha($value[0]);
1140
            \setlocale(LC_CTYPE, $locale);
1141 12
        }
1142
1143 12
        if (!$valid) {
1144 8
            static::reportInvalidArgument(\sprintf(
1145 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
1146 8
                static::valueToString($value)
1147
            ));
1148
        }
1149 4
    }
1150
1151
    /**
1152
     * @psalm-pure
1153
     *
1154
     * @param string                   $value
1155
     * @param string                   $suffix
1156
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1157
     *
1158
     * @throws InvalidArgumentException
1159
     */
1160 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
1161
    {
1162 12
        if ($suffix !== \substr($value, -\strlen($suffix))) {
1163 4
            static::reportInvalidArgument(\sprintf(
1164 4
                $message ?: 'Expected a value to end with %2$s. Got: %s',
1165 4
                static::valueToString($value),
1166 4
                static::valueToString($suffix)
1167 4
            ));
1168
        }
1169
    }
1170 8
1171
    /**
1172
     * @psalm-pure
1173
     *
1174
     * @param string                   $value
1175
     * @param string                   $suffix
1176
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1177
     *
1178
     * @throws InvalidArgumentException
1179
     */
1180 28 View Code Duplication
    public static function notEndsWith($value, $suffix, $message = '')
1181
    {
1182 28
        if ($suffix === \substr($value, -\strlen($suffix))) {
1183
            static::reportInvalidArgument(\sprintf(
1184 28
                $message ?: 'Expected a value not to end with %2$s. Got: %s',
1185 16
                static::valueToString($value),
1186 16
                static::valueToString($suffix)
1187 16
            ));
1188
        }
1189
    }
1190 12
1191
    /**
1192
     * @psalm-pure
1193
     *
1194
     * @param string $value
1195
     * @param string $pattern
1196
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1197
     *
1198
     * @throws InvalidArgumentException
1199
     */
1200 20
    public static function regex($value, $pattern, $message = '')
1201
    {
1202 20
        if (!\preg_match($pattern, $value)) {
1203
            static::reportInvalidArgument(\sprintf(
1204 12
                $message ?: 'The value %s does not match the expected pattern.',
1205 12
                static::valueToString($value)
1206 12
            ));
1207 12
        }
1208
    }
1209 12
1210 8
    /**
1211 8
     * @psalm-pure
1212 8
     *
1213
     * @param string $value
1214
     * @param string $pattern
1215 4
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1216
     *
1217
     * @throws InvalidArgumentException
1218
     */
1219
    public static function notRegex($value, $pattern, $message = '')
1220
    {
1221
        if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
1222
            static::reportInvalidArgument(\sprintf(
1223
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
1224
                static::valueToString($value),
1225 12
                static::valueToString($pattern),
1226
                $matches[0][1]
1227 12
            ));
1228 12
        }
1229 12
    }
1230 12
1231
    /**
1232 12
     * @psalm-pure
1233 8
     *
1234 8
     * @param mixed                    $value
1235 8
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1236
     *
1237
     * @throws InvalidArgumentException
1238 4
     */
1239 View Code Duplication
    public static function unicodeLetters($value, $message = '')
1240
    {
1241
        static::string($value);
1242
1243
        if (!\preg_match('/^\p{L}+$/u', $value)) {
1244
            static::reportInvalidArgument(\sprintf(
1245
                $message ?: 'Expected a value to contain only Unicode letters. Got: %s',
1246
                static::valueToString($value)
1247
            ));
1248 12
        }
1249
    }
1250 12
1251 12
    /**
1252 12
     * @psalm-pure
1253 12
     *
1254
     * @param mixed                    $value
1255 12
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1256 8
     *
1257 8
     * @throws InvalidArgumentException
1258 8
     */
1259 View Code Duplication
    public static function alpha($value, $message = '')
1260
    {
1261 4
        static::string($value);
1262
1263
        $locale = \setlocale(LC_CTYPE, 0);
1264
        \setlocale(LC_CTYPE, 'C');
1265
        $valid = !\ctype_alpha($value);
1266
        \setlocale(LC_CTYPE, $locale);
1267
1268
        if ($valid) {
1269
            static::reportInvalidArgument(\sprintf(
1270
                $message ?: 'Expected a value to contain only letters. Got: %s',
1271
                static::valueToString($value)
1272 16
            ));
1273
        }
1274 16
    }
1275 16
1276 16
    /**
1277 16
     * @psalm-pure
1278
     *
1279 16
     * @param string                   $value
1280 12
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1281 12
     *
1282 12
     * @throws InvalidArgumentException
1283
     */
1284 View Code Duplication
    public static function digits($value, $message = '')
1285 4
    {
1286
        $locale = \setlocale(LC_CTYPE, 0);
1287
        \setlocale(LC_CTYPE, 'C');
1288
        $valid = !\ctype_digit($value);
1289
        \setlocale(LC_CTYPE, $locale);
1290
1291
        if ($valid) {
1292
            static::reportInvalidArgument(\sprintf(
1293
                $message ?: 'Expected a value to contain digits only. Got: %s',
1294
                static::valueToString($value)
1295
            ));
1296 16
        }
1297
    }
1298 16
1299 16
    /**
1300 16
     * @psalm-pure
1301 16
     *
1302
     * @param string                   $value
1303 16
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1304 12
     *
1305 12
     * @throws InvalidArgumentException
1306 12
     */
1307 View Code Duplication
    public static function alnum($value, $message = '')
1308
    {
1309 4
        $locale = \setlocale(LC_CTYPE, 0);
1310
        \setlocale(LC_CTYPE, 'C');
1311
        $valid = !\ctype_alnum($value);
1312
        \setlocale(LC_CTYPE, $locale);
1313
1314
        if ($valid) {
1315
            static::reportInvalidArgument(\sprintf(
1316
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
1317
                static::valueToString($value)
1318
            ));
1319
        }
1320 36
    }
1321
1322 36
    /**
1323 24
     * @psalm-pure
1324 24
     * @psalm-assert lowercase-string $value
1325 24
     *
1326 24
     * @param string                   $value
1327
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1328
     *
1329 12
     * @throws InvalidArgumentException
1330
     */
1331 View Code Duplication
    public static function lower($value, $message = '')
1332
    {
1333
        $locale = \setlocale(LC_CTYPE, 0);
1334
        \setlocale(LC_CTYPE, 'C');
1335
        $valid = !\ctype_lower($value);
1336
        \setlocale(LC_CTYPE, $locale);
1337
1338
        if ($valid) {
1339
            static::reportInvalidArgument(\sprintf(
1340
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
1341
                static::valueToString($value)
1342 36
            ));
1343
        }
1344 36
    }
1345 12
1346 12
    /**
1347 12
     * @psalm-pure
1348 12
     * @psalm-assert !lowercase-string $value
1349
     *
1350
     * @param string                   $value
1351 24
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1352
     *
1353
     * @throws InvalidArgumentException
1354
     */
1355 View Code Duplication
    public static function upper($value, $message = '')
1356
    {
1357
        $locale = \setlocale(LC_CTYPE, 0);
1358
        \setlocale(LC_CTYPE, 'C');
1359
        $valid = !\ctype_upper($value);
1360
        \setlocale(LC_CTYPE, $locale);
1361
1362
        if ($valid) {
1363
            static::reportInvalidArgument(\sprintf(
1364 36
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
1365
                static::valueToString($value)
1366 36
            ));
1367 12
        }
1368 12
    }
1369 12
1370 12
    /**
1371
     * @psalm-pure
1372
     *
1373 24
     * @param string $value
1374
     * @param int    $length
1375
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1376
     *
1377
     * @throws InvalidArgumentException
1378
     */
1379 View Code Duplication
    public static function length($value, $length, $message = '')
1380
    {
1381
        if ($length !== static::strlen($value)) {
1382
            static::reportInvalidArgument(\sprintf(
1383
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
1384
                static::valueToString($value),
1385
                $length
1386
            ));
1387 60
        }
1388
    }
1389 60
1390
    /**
1391 60
     * Inclusive min.
1392 24
     *
1393 24
     * @psalm-pure
1394 24
     *
1395 24
     * @param string    $value
1396 24
     * @param int|float $min
1397
     * @param string    $message
1398
     *
1399 36
     * @throws InvalidArgumentException
1400
     */
1401 View Code Duplication
    public static function minLength($value, $min, $message = '')
1402
    {
1403
        if (static::strlen($value) < $min) {
1404
            static::reportInvalidArgument(\sprintf(
1405
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
1406
                static::valueToString($value),
1407
                $min
1408
            ));
1409 36
        }
1410
    }
1411 36
1412
    /**
1413 36
     * Inclusive max.
1414 12
     *
1415 12
     * @psalm-pure
1416 12
     *
1417
     * @param string    $value
1418
     * @param int|float $max
1419 24
     * @param string    $message
1420
     *
1421
     * @throws InvalidArgumentException
1422
     */
1423 View Code Duplication
    public static function maxLength($value, $max, $message = '')
1424
    {
1425
        if (static::strlen($value) > $max) {
1426
            static::reportInvalidArgument(\sprintf(
1427 12
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
1428
                static::valueToString($value),
1429 12
                $max
1430
            ));
1431 8
        }
1432 4
    }
1433 4
1434 4
    /**
1435
     * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion.
1436
     *
1437 4
     * @psalm-pure
1438
     *
1439
     * @param string    $value
1440
     * @param int|float $min
1441
     * @param int|float $max
1442
     * @param string    $message
1443
     *
1444
     * @throws InvalidArgumentException
1445 12
     */
1446 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
1447 12
    {
1448
        $length = static::strlen($value);
1449 8
1450 4
        if ($length < $min || $length > $max) {
1451 4
            static::reportInvalidArgument(\sprintf(
1452 4
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
1453
                static::valueToString($value),
1454
                $min,
1455 4
                $max
1456
            ));
1457
        }
1458
    }
1459
1460
    /**
1461
     * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file.
1462
     *
1463
     * @param mixed                    $value
1464
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1465
     *
1466
     * @throws InvalidArgumentException
1467
     */
1468 View Code Duplication
    public static function fileExists($value, $message = '')
1469
    {
1470
        static::string($value);
1471
1472
        if (!\file_exists($value)) {
1473
            static::reportInvalidArgument(\sprintf(
1474
                $message ?: 'The file %s does not exist.',
1475
                static::valueToString($value)
1476
            ));
1477
        }
1478
    }
1479
1480
    /**
1481
     * @param mixed                    $value
1482
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1483
     *
1484
     * @throws InvalidArgumentException
1485
     */
1486 View Code Duplication
    public static function file($value, $message = '')
1487
    {
1488
        static::fileExists($value, $message);
1489
1490
        if (!\is_file($value)) {
1491
            static::reportInvalidArgument(\sprintf(
1492
                $message ?: 'The path %s is not a file.',
1493
                static::valueToString($value)
1494
            ));
1495
        }
1496
    }
1497 8
1498
    /**
1499 8
     * @param mixed                    $value
1500 4
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1501 4
     *
1502 4
     * @throws InvalidArgumentException
1503
     */
1504 View Code Duplication
    public static function directory($value, $message = '')
1505 4
    {
1506
        static::fileExists($value, $message);
1507
1508
        if (!\is_dir($value)) {
1509
            static::reportInvalidArgument(\sprintf(
1510
                $message ?: 'The path %s is no directory.',
1511
                static::valueToString($value)
1512
            ));
1513
        }
1514
    }
1515
1516
    /**
1517
     * @param string                   $value
1518
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1519 8
     *
1520
     * @throws InvalidArgumentException
1521 8
     */
1522 4
    public static function readable($value, $message = '')
1523 4
    {
1524 4
        if (!\is_readable($value)) {
1525 4
            static::reportInvalidArgument(\sprintf(
1526
                $message ?: 'The path %s is not readable.',
1527
                static::valueToString($value)
1528 4
            ));
1529
        }
1530
    }
1531
1532
    /**
1533
     * @param string                   $value
1534
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1535
     *
1536
     * @throws InvalidArgumentException
1537
     */
1538 8
    public static function writable($value, $message = '')
1539
    {
1540 8
        if (!\is_writable($value)) {
1541 4
            static::reportInvalidArgument(\sprintf(
1542 4
                $message ?: 'The path %s is not writable.',
1543 4
                static::valueToString($value)
1544
            ));
1545
        }
1546 4
    }
1547
1548
    /**
1549
     * @psalm-assert class-string $value
1550
     *
1551
     * @param mixed                    $value
1552
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1553
     *
1554
     * @throws InvalidArgumentException
1555
     */
1556
    public static function classExists($value, $message = '')
1557
    {
1558
        if (!\class_exists($value)) {
1559
            static::reportInvalidArgument(\sprintf(
1560 8
                $message ?: 'Expected an existing class name. Got: %s',
1561
                static::valueToString($value)
1562 8
            ));
1563 4
        }
1564 4
    }
1565 4
1566 4
    /**
1567
     * @psalm-pure
1568
     * @psalm-template ExpectedType of object
1569 4
     * @psalm-param class-string<ExpectedType> $class
1570
     * @psalm-assert class-string<ExpectedType>|ExpectedType $value
1571
     *
1572
     * @param mixed         $value
1573
     * @param string|object $class
1574
     * @param string        $message
1575
     *
1576
     * @throws InvalidArgumentException
1577
     */
1578
    public static function subclassOf($value, $class, $message = '')
1579
    {
1580
        if (!\is_subclass_of($value, $class)) {
1581 12
            static::reportInvalidArgument(\sprintf(
1582
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
1583 12
                static::valueToString($value),
1584 4
                static::valueToString($class)
1585 4
            ));
1586 4
        }
1587
    }
1588
1589 8
    /**
1590
     * @psalm-assert class-string $value
1591
     *
1592
     * @param mixed                    $value
1593
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1594
     *
1595
     * @throws InvalidArgumentException
1596
     */
1597
    public static function interfaceExists($value, $message = '')
1598
    {
1599
        if (!\interface_exists($value)) {
1600
            static::reportInvalidArgument(\sprintf(
1601 12
                $message ?: 'Expected an existing interface name. got %s',
1602
                static::valueToString($value)
1603 12
            ));
1604 8
        }
1605 8
    }
1606 8
1607
    /**
1608
     * @psalm-pure
1609 4
     * @psalm-template ExpectedType of object
1610
     * @psalm-param class-string<ExpectedType> $interface
1611
     * @psalm-assert class-string<ExpectedType> $value
1612
     *
1613
     * @param mixed                    $value
1614
     * @param mixed  $interface
1615
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1616
     *
1617
     * @throws InvalidArgumentException
1618
     */
1619 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
1620
    {
1621 27
        if (!\in_array($interface, \class_implements($value))) {
1622
            static::reportInvalidArgument(\sprintf(
1623 27
                $message ?: 'Expected an implementation of %2$s. Got: %s',
1624 19
                static::valueToString($value),
1625 19
                static::valueToString($interface)
1626 19
            ));
1627
        }
1628
    }
1629 8
1630
    /**
1631
     * @psalm-pure
1632
     * @psalm-param class-string|object $classOrObject
1633
     *
1634
     * @param string|object $classOrObject
1635
     * @param mixed         $property
1636
     * @param string        $message
1637
     *
1638
     * @throws InvalidArgumentException
1639
     */
1640 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
1641 27
    {
1642
        if (!\property_exists($classOrObject, $property)) {
1643 27
            static::reportInvalidArgument(\sprintf(
1644 8
                $message ?: 'Expected the property %s to exist.',
1645 8
                static::valueToString($property)
1646 8
            ));
1647
        }
1648
    }
1649 19
1650
    /**
1651
     * @psalm-pure
1652
     * @psalm-param class-string|object $classOrObject
1653
     *
1654
     * @param string|object $classOrObject
1655
     * @param mixed         $property
1656
     * @param string        $message
1657
     *
1658
     * @throws InvalidArgumentException
1659
     */
1660 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
1661
    {
1662 12
        if (\property_exists($classOrObject, $property)) {
1663 4
            static::reportInvalidArgument(\sprintf(
1664 4
                $message ?: 'Expected the property %s to not exist.',
1665 4
                static::valueToString($property)
1666
            ));
1667
        }
1668 8
    }
1669
1670
    /**
1671
     * @psalm-pure
1672
     * @psalm-param class-string|object $classOrObject
1673
     *
1674
     * @param string|object $classOrObject
1675
     * @param mixed         $method
1676
     * @param string        $message
1677
     *
1678
     * @throws InvalidArgumentException
1679 12
     */
1680 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
1681 12
    {
1682 8
        if (!(\is_string($classOrObject) || \is_object($classOrObject)) || !\method_exists($classOrObject, $method)) {
1683 8
            static::reportInvalidArgument(\sprintf(
1684 8
                $message ?: 'Expected the method %s to exist.',
1685
                static::valueToString($method)
1686
            ));
1687 4
        }
1688
    }
1689
1690
    /**
1691
     * @psalm-pure
1692
     * @psalm-param class-string|object $classOrObject
1693
     *
1694
     * @param string|object $classOrObject
1695
     * @param mixed         $method
1696
     * @param string        $message
1697
     *
1698
     * @throws InvalidArgumentException
1699
     */
1700 28 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
1701
    {
1702 28
        if ((\is_string($classOrObject) || \is_object($classOrObject)) && \method_exists($classOrObject, $method)) {
1703 20
            static::reportInvalidArgument(\sprintf(
1704 20
                $message ?: 'Expected the method %s to not exist.',
1705 20
                static::valueToString($method)
1706
            ));
1707
        }
1708 8
    }
1709
1710
    /**
1711
     * @psalm-pure
1712
     *
1713
     * @param array      $array
1714
     * @param string|int $key
1715
     * @param string     $message
1716
     *
1717
     * @throws InvalidArgumentException
1718
     */
1719 8 View Code Duplication
    public static function keyExists($array, $key, $message = '')
1720
    {
1721 8
        if (!(isset($array[$key]) || \array_key_exists($key, $array))) {
1722 8
            static::reportInvalidArgument(\sprintf(
1723
                $message ?: 'Expected the key %s to exist.',
1724 8
                static::valueToString($key)
1725 8
            ));
1726 8
        }
1727 8
    }
1728
1729
    /**
1730 4
     * @psalm-pure
1731
     *
1732
     * @param array      $array
1733
     * @param string|int $key
1734
     * @param string     $message
1735
     *
1736
     * @throws InvalidArgumentException
1737
     */
1738 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
1739
    {
1740
        if (isset($array[$key]) || \array_key_exists($key, $array)) {
1741 12
            static::reportInvalidArgument(\sprintf(
1742
                $message ?: 'Expected the key %s to not exist.',
1743 12
                static::valueToString($key)
1744 4
            ));
1745 4
        }
1746 4
    }
1747 4
1748
    /**
1749
     * Checks if a value is a valid array key (int or string).
1750 8
     *
1751
     * @psalm-pure
1752
     * @psalm-assert array-key $value
1753
     *
1754
     * @param mixed                    $value
1755
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1756
     *
1757
     * @throws InvalidArgumentException
1758
     */
1759
    public static function validArrayKey($value, $message = '')
1760
    {
1761 12
        if (!(\is_int($value) || \is_string($value))) {
1762
            static::reportInvalidArgument(
1763 12
                is_callable($message)
1764 4
                    ? $message()
1765 4
                    : sprintf(
1766 4
                        $message ?: 'Expected string or integer. Got: %s',
1767 4
                        static::typeToString($value)
1768
                    )
1769
            );
1770 8
        }
1771
    }
1772
1773
    /**
1774
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1775
     *
1776
     * @param Countable|array $array
1777
     * @param int             $number
1778
     * @param string          $message
1779
     *
1780
     * @throws InvalidArgumentException
1781
     */
1782 20
    public static function count($array, $number, $message = '')
1783
    {
1784 20
        static::eq(
1785
            \count($array),
1786 20
            $number,
1787 8
            \sprintf(
1788 8
                $message ?: 'Expected an array to contain %d elements. Got: %d.',
1789 8
                $number,
1790 8
                \count($array)
1791 8
            )
1792
        );
1793
    }
1794 12
1795
    /**
1796
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1797
     *
1798
     * @param Countable|array $array
1799
     * @param int|float       $min
1800
     * @param string          $message
1801
     *
1802
     * @throws InvalidArgumentException
1803
     */
1804 View Code Duplication
    public static function minCount($array, $min, $message = '')
1805 80
    {
1806
        if (\count($array) < $min) {
1807 80
            static::reportInvalidArgument(\sprintf(
1808 32
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
1809 32
                \count($array),
1810
                $min
1811
            ));
1812 48
        }
1813
    }
1814
1815
    /**
1816
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1817
     *
1818
     * @param Countable|array $array
1819
     * @param int|float       $max
1820
     * @param string          $message
1821
     *
1822
     * @throws InvalidArgumentException
1823 40
     */
1824 View Code Duplication
    public static function maxCount($array, $max, $message = '')
1825 40
    {
1826 24
        if (\count($array) > $max) {
1827 20
            static::reportInvalidArgument(\sprintf(
1828
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
1829
                \count($array),
1830
                $max
1831
            ));
1832
        }
1833
    }
1834
1835
    /**
1836
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1837
     *
1838
     * @param Countable|array $array
1839
     * @param int|float       $min
1840 32
     * @param int|float       $max
1841
     * @param string          $message
1842
     *
1843 32
     * @throws InvalidArgumentException
1844 32
     */
1845
    public static function countBetween($array, $min, $max, $message = '')
1846 16
    {
1847 16
        $count = \count($array);
1848
1849
        if ($count < $min || $count > $max) {
1850 16
            static::reportInvalidArgument(\sprintf(
1851
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
1852
                $count,
1853
                $min,
1854
                $max
1855
            ));
1856
        }
1857
    }
1858
1859
    /**
1860
     * @psalm-pure
1861
     * @psalm-assert list $array
1862
     *
1863
     * @param mixed  $array
1864 16
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1865
     *
1866 16
     * @throws InvalidArgumentException
1867 8
     */
1868 4 View Code Duplication
    public static function isList($array, $message = '')
1869
    {
1870
        if (!\is_array($array) || $array !== \array_values($array)) {
1871
            static::reportInvalidArgument(
1872
                $message ?: 'Expected list - non-associative array.'
1873
            );
1874
        }
1875
    }
1876
1877
    /**
1878 56
     * @psalm-pure
1879
     * @psalm-assert non-empty-list $array
1880 56
     *
1881
     * @param mixed  $array
1882
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1883
     *
1884 56
     * @throws InvalidArgumentException
1885 4
     */
1886
    public static function isNonEmptyList($array, $message = '')
1887
    {
1888 52
        static::isList($array, $message);
1889 20
        static::notEmpty($array, $message);
1890 20
    }
1891 20
1892
    /**
1893
     * @psalm-pure
1894 32
     * @psalm-template T
1895
     * @psalm-param mixed|array<T> $array
1896
     * @psalm-assert array<string, T> $array
1897
     *
1898
     * @param mixed  $array
1899
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1900
     *
1901
     * @throws InvalidArgumentException
1902
     */
1903 View Code Duplication
    public static function isMap($array, $message = '')
1904
    {
1905 24
        if (
1906
            !\is_array($array) ||
1907 24
            \array_keys($array) !== \array_filter(\array_keys($array), '\is_string')
1908
        ) {
1909 24
            static::reportInvalidArgument(
1910
                $message ?: 'Expected map - associative array with string keys.'
1911
            );
1912 24
        }
1913 24
    }
1914 20
1915 20
    /**
1916 20
     * @psalm-pure
1917
     * @psalm-template T
1918 4
     * @psalm-param mixed|array<T> $array
1919 4
     * @psalm-assert array<string, T> $array
1920 4
     * @psalm-assert !empty $array
1921 4
     *
1922
     * @param mixed  $array
1923
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1924
     *
1925 8
     * @throws InvalidArgumentException
1926 8
     */
1927 8
    public static function isNonEmptyMap($array, $message = '')
1928 8
    {
1929
        static::isMap($array, $message);
1930
        static::notEmpty($array, $message);
1931
    }
1932
1933
    /**
1934
     * @psalm-pure
1935 1642
     *
1936
     * @param string                   $value
1937 1642
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1938 607
     *
1939 501
     * @throws InvalidArgumentException
1940 501
     */
1941
    public static function uuid($value, $message = '')
1942
    {
1943 354
        $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
1944
1945
        // The nil UUID is special form of UUID that is specified to have all
1946 1035
        // 128 bits set to zero.
1947 1034
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1948
            return;
1949 1034
        }
1950 1034
1951
        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)) {
1952 1034
            static::reportInvalidArgument(\sprintf(
1953 1034
                $message ?: 'Value %s is not a valid UUID.',
1954
                static::valueToString($value)
1955 1034
            ));
1956
        }
1957
    }
1958 504
1959
    /**
1960
     * @psalm-param class-string<Throwable> $class
1961 1
     *
1962
     * @param Closure $expression
1963
     * @param string  $class
1964
     * @param string  $message
1965
     *
1966
     * @throws InvalidArgumentException
1967
     */
1968
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1969 751
    {
1970
        static::string($class);
1971 751
1972 20
        $actual = 'none';
1973
1974
        try {
1975 733
            $expression();
1976 15
        } catch (Exception $e) {
1977
            $actual = \get_class($e);
1978
            if ($e instanceof $class) {
1979 723
                return;
1980 25
            }
1981
        } 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...
1982
            $actual = \get_class($e);
1983 698
            if ($e instanceof $class) {
1984 21
                return;
1985
            }
1986
        }
1987 677
1988 3
        static::reportInvalidArgument($message ?: \sprintf(
1989 1
            'Expected to throw "%s", got "%s"',
1990
            $class,
1991
            $actual
1992 2
        ));
1993 1
    }
1994
1995
    /**
1996 1
     * @throws BadMethodCallException
1997
     */
1998
    public static function __callStatic($name, $arguments)
1999 676
    {
2000 1
        if ('nullOr' === \substr($name, 0, 6)) {
2001
            if (null !== $arguments[0]) {
2002
                $method = \lcfirst(\substr($name, 6));
2003 676
                \call_user_func_array(array('static', $method), $arguments);
2004 574
            }
2005
2006
            return;
2007 114
        }
2008
2009
        if ('all' === \substr($name, 0, 3)) {
2010
            static::isIterable($arguments[0]);
2011
2012
            $method = \lcfirst(\substr($name, 3));
2013
            $args = $arguments;
2014
2015 251
            foreach ($arguments[0] as $entry) {
2016
                $args[0] = $entry;
2017 251
2018
                \call_user_func_array(array('static', $method), $args);
2019
            }
2020 168
2021
            return;
2022 168
        }
2023
2024
        throw new BadMethodCallException('No such method: '.$name);
2025
    }
2026 168
2027
    /**
2028
     * @param mixed $value
2029
     *
2030 168
     * @return string
2031
     */
2032
    protected static function valueToString($value)
2033
    {
2034
        if (null === $value) {
2035
            return 'null';
2036
        }
2037
2038
        if (true === $value) {
2039
            return 'true';
2040 1065
        }
2041
2042 1065
        if (false === $value) {
2043
            return 'false';
2044
        }
2045
2046
        if (\is_array($value)) {
2047
            return 'array';
2048
        }
2049
2050
        if (\is_object($value)) {
2051
            if (\method_exists($value, '__toString')) {
2052
                return \get_class($value).': '.self::valueToString($value->__toString());
2053
            }
2054
2055
            if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
2056
                return \get_class($value).': '.self::valueToString($value->format('c'));
2057
            }
2058
2059
            return \get_class($value);
2060
        }
2061
2062
        if (\is_resource($value)) {
2063
            return 'resource';
2064
        }
2065
2066
        if (\is_string($value)) {
2067
            return '"'.$value.'"';
2068
        }
2069
2070
        return (string) $value;
2071
    }
2072
2073
    /**
2074
     * @param mixed $value
2075
     *
2076
     * @return string
2077
     */
2078
    protected static function typeToString($value)
2079
    {
2080
        return \is_object($value) ? \get_class($value) : \gettype($value);
2081
    }
2082
2083
    protected static function strlen($value)
2084
    {
2085
        if (!\function_exists('mb_detect_encoding')) {
2086
            return \strlen($value);
2087
        }
2088
2089
        if (false === $encoding = \mb_detect_encoding($value)) {
2090
            return \strlen($value);
2091
        }
2092
2093
        return \mb_strlen($value, $encoding);
2094
    }
2095
2096
    /**
2097
     * @param string|callable():string $message
0 ignored issues
show
Documentation introduced by
The doc-type string|callable():string could not be parsed: Expected "|" or "end of type", but got "(" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
2098
     *
2099
     * @throws InvalidArgumentException
2100
     *
2101
     * @psalm-pure this method is not supposed to perform side-effects
2102
     */
2103
    protected static function reportInvalidArgument($message)
2104
    {
2105
        if (is_callable($message)) {
2106
            $message = $message();
2107
        }
2108
2109
        throw new InvalidArgumentException($message);
2110
    }
2111
2112
    private function __construct()
2113
    {
2114
    }
2115
}
2116