Failed Conditions
Pull Request — master (#221)
by
unknown
20:20
created

Assert::resource()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 7
cts 7
cp 1
rs 8.6506
c 0
b 0
f 0
cc 7
nc 4
nop 3
crap 7
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(array('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(array('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 array  $values
789
     * @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...
790
     *
791
     * @throws InvalidArgumentException
792
     */
793 16
    public static function uniqueValues(array $values, $message = '')
794
    {
795 16
        $allValues = \count($values);
796 12
        $uniqueValues = \count(\array_unique($values));
797 12
798 12
        if ($allValues !== $uniqueValues) {
799 12
            $difference = $allValues - $uniqueValues;
800
801
            static::reportInvalidArgument(\sprintf(
802 4
                $message ?: 'Expected an array of unique values, but %s of them %s duplicated',
803
                $difference,
804
                (1 === $difference ? 'is' : 'are')
805
            ));
806
        }
807
    }
808
809
    /**
810
     * @param mixed                    $value
811
     * @param mixed  $expect
812
     * @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...
813 16
     *
814
     * @throws InvalidArgumentException
815 16
     */
816 4
    public static function eq($value, $expect, $message = '')
817 4
    {
818 4
        if ($expect != $value) {
819
            static::reportInvalidArgument(\sprintf(
820
                $message ?: 'Expected a value equal to %2$s. Got: %s',
821 12
                static::valueToString($value),
822
                static::valueToString($expect)
823
            ));
824
        }
825
    }
826
827
    /**
828
     * @param mixed                    $value
829
     * @param mixed  $expect
830
     * @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...
831
     *
832 8
     * @throws InvalidArgumentException
833
     */
834 8
    public static function notEq($value, $expect, $message = '')
835 4
    {
836 4
        if ($expect == $value) {
837 4
            static::reportInvalidArgument(\sprintf(
838 4
                $message ?: 'Expected a different value than %s.',
839
                static::valueToString($expect)
840
            ));
841 4
        }
842
    }
843
844
    /**
845
     * @psalm-pure
846
     *
847
     * @param mixed                    $value
848
     * @param mixed  $expect
849
     * @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...
850
     *
851
     * @throws InvalidArgumentException
852 12
     */
853
    public static function same($value, $expect, $message = '')
854 12
    {
855 4
        if ($expect !== $value) {
856 4
            static::reportInvalidArgument(\sprintf(
857 4
                $message ?: 'Expected a value identical to %2$s. Got: %s',
858 4
                static::valueToString($value),
859
                static::valueToString($expect)
860
            ));
861 8
        }
862
    }
863
864
    /**
865
     * @psalm-pure
866
     *
867
     * @param mixed                    $value
868
     * @param mixed  $expect
869
     * @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...
870
     *
871
     * @throws InvalidArgumentException
872 9
     */
873
    public static function notSame($value, $expect, $message = '')
874 9
    {
875 5
        if ($expect === $value) {
876 5
            static::reportInvalidArgument(\sprintf(
877 5
                $message ?: 'Expected a value not identical to %s.',
878 5
                static::valueToString($expect)
879
            ));
880
        }
881 4
    }
882
883
    /**
884
     * @psalm-pure
885
     *
886
     * @param mixed                    $value
887
     * @param mixed  $limit
888
     * @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...
889
     *
890
     * @throws InvalidArgumentException
891
     */
892 12
    public static function greaterThan($value, $limit, $message = '')
893
    {
894 12
        if ($value <= $limit) {
895 4
            static::reportInvalidArgument(\sprintf(
896 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
897 4
                static::valueToString($value),
898 4
                static::valueToString($limit)
899
            ));
900
        }
901 8
    }
902
903
    /**
904
     * @psalm-pure
905
     *
906
     * @param mixed                    $value
907
     * @param mixed  $limit
908
     * @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...
909
     *
910
     * @throws InvalidArgumentException
911
     */
912
    public static function greaterThanEq($value, $limit, $message = '')
913
    {
914
        if ($value < $limit) {
915 16
            static::reportInvalidArgument(\sprintf(
916
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
917 16
                static::valueToString($value),
918 8
                static::valueToString($limit)
919 8
            ));
920 8
        }
921 8
    }
922 8
923
    /**
924
     * @psalm-pure
925 8
     *
926
     * @param mixed                    $value
927
     * @param mixed  $limit
928
     * @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...
929
     *
930
     * @throws InvalidArgumentException
931
     */
932
    public static function lessThan($value, $limit, $message = '')
933
    {
934
        if ($value >= $limit) {
935
            static::reportInvalidArgument(\sprintf(
936
                $message ?: 'Expected a value less than %2$s. Got: %s',
937
                static::valueToString($value),
938 8
                static::valueToString($limit)
939
            ));
940 8
        }
941 4
    }
942
943
    /**
944
     * @psalm-pure
945
     *
946
     * @param mixed                    $value
947
     * @param mixed  $limit
948
     * @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...
949
     *
950
     * @throws InvalidArgumentException
951
     */
952
    public static function lessThanEq($value, $limit, $message = '')
953
    {
954 16
        if ($value > $limit) {
955
            static::reportInvalidArgument(\sprintf(
956 16
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
957 8
                static::valueToString($value),
958 8
                static::valueToString($limit)
959 8
            ));
960 8
        }
961
    }
962
963 8
    /**
964
     * Inclusive range, so Assert::(3, 3, 5) passes.
965
     *
966
     * @psalm-pure
967
     *
968
     * @param mixed                    $value
969
     * @param mixed  $min
970
     * @param mixed  $max
971
     * @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...
972
     *
973
     * @throws InvalidArgumentException
974 80
     */
975 View Code Duplication
    public static function range($value, $min, $max, $message = '')
976 80
    {
977 32
        if ($value < $min || $value > $max) {
978 32
            static::reportInvalidArgument(\sprintf(
979 32
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
980 32
                static::valueToString($value),
981
                static::valueToString($min),
982
                static::valueToString($max)
983 48
            ));
984
        }
985
    }
986
987
    /**
988
     * A more human-readable alias of Assert::inArray().
989
     *
990
     * @psalm-pure
991
     *
992
     * @param mixed                    $value
993
     * @param array  $values
994 80
     * @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...
995
     *
996 80
     * @throws InvalidArgumentException
997 48
     */
998 48
    public static function oneOf($value, array $values, $message = '')
999 48
    {
1000 48
        static::inArray($value, $values, $message);
1001
    }
1002
1003 32
    /**
1004
     * Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion.
1005
     *
1006
     * @psalm-pure
1007
     *
1008
     * @param mixed                    $value
1009
     * @param array  $values
1010
     * @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...
1011
     *
1012
     * @throws InvalidArgumentException
1013 40
     */
1014
    public static function inArray($value, array $values, $message = '')
1015 40
    {
1016 24
        if (!\in_array($value, $values, true)) {
1017 24
            static::reportInvalidArgument(\sprintf(
1018 24
                $message ?: 'Expected one of: %2$s. Got: %s',
1019
                static::valueToString($value),
1020
                \implode(', ', \array_map(array('static', 'valueToString'), $values))
1021 16
            ));
1022
        }
1023
    }
1024
1025
    /**
1026
     * @psalm-pure
1027
     *
1028
     * @param string $value
1029
     * @param string $subString
1030
     * @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...
1031
     *
1032 48
     * @throws InvalidArgumentException
1033
     */
1034 48 View Code Duplication
    public static function contains($value, $subString, $message = '')
1035 32
    {
1036 32
        if (false === \strpos($value, $subString)) {
1037 32
            static::reportInvalidArgument(\sprintf(
1038 32
                $message ?: 'Expected a value to contain %2$s. Got: %s',
1039
                static::valueToString($value),
1040
                static::valueToString($subString)
1041 16
            ));
1042
        }
1043
    }
1044
1045
    /**
1046
     * @psalm-pure
1047
     *
1048
     * @param string $value
1049
     * @param string $subString
1050
     * @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...
1051
     *
1052 48
     * @throws InvalidArgumentException
1053
     */
1054 48 View Code Duplication
    public static function notContains($value, $subString, $message = '')
1055 16
    {
1056 16
        if (false !== \strpos($value, $subString)) {
1057 16
            static::reportInvalidArgument(\sprintf(
1058 16
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
1059
                static::valueToString($value),
1060
                static::valueToString($subString)
1061 32
            ));
1062
        }
1063
    }
1064
1065
    /**
1066
     * @psalm-pure
1067
     *
1068
     * @param string $value
1069
     * @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...
1070
     *
1071 35
     * @throws InvalidArgumentException
1072
     */
1073 35
    public static function notWhitespaceOnly($value, $message = '')
1074
    {
1075 24
        if (\preg_match('/^\s*$/', $value)) {
1076
            static::reportInvalidArgument(\sprintf(
1077 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
1078 20
                static::valueToString($value)
1079 20
            ));
1080 20
        }
1081 20
    }
1082
1083
    /**
1084 24
     * @psalm-pure
1085 12
     *
1086 12
     * @param string $value
1087 12
     * @param string $prefix
1088
     * @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...
1089
     *
1090 12
     * @throws InvalidArgumentException
1091
     */
1092 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
1093
    {
1094
        if (0 !== \strpos($value, $prefix)) {
1095
            static::reportInvalidArgument(\sprintf(
1096
                $message ?: 'Expected a value to start with %2$s. Got: %s',
1097
                static::valueToString($value),
1098
                static::valueToString($prefix)
1099
            ));
1100
        }
1101 48
    }
1102
1103 48
    /**
1104 32
     * @psalm-pure
1105 32
     *
1106 32
     * @param string $value
1107 32
     * @param string $prefix
1108
     * @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...
1109
     *
1110 16
     * @throws InvalidArgumentException
1111
     */
1112 View Code Duplication
    public static function notStartsWith($value, $prefix, $message = '')
1113
    {
1114
        if (0 === \strpos($value, $prefix)) {
1115
            static::reportInvalidArgument(\sprintf(
1116
                $message ?: 'Expected a value not to start with %2$s. Got: %s',
1117
                static::valueToString($value),
1118
                static::valueToString($prefix)
1119
            ));
1120
        }
1121 48
    }
1122
1123 48
    /**
1124 16
     * @psalm-pure
1125 16
     *
1126 16
     * @param mixed                    $value
1127 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...
1128
     *
1129
     * @throws InvalidArgumentException
1130 32
     */
1131
    public static function startsWithLetter($value, $message = '')
1132
    {
1133
        static::string($value);
1134
1135
        $valid = isset($value[0]);
1136
1137
        if ($valid) {
1138
            $locale = \setlocale(LC_CTYPE, 0);
1139
            \setlocale(LC_CTYPE, 'C');
1140
            $valid = \ctype_alpha($value[0]);
1141 12
            \setlocale(LC_CTYPE, $locale);
1142
        }
1143 12
1144 8
        if (!$valid) {
1145 8
            static::reportInvalidArgument(\sprintf(
1146 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
1147
                static::valueToString($value)
1148
            ));
1149 4
        }
1150
    }
1151
1152
    /**
1153
     * @psalm-pure
1154
     *
1155
     * @param string $value
1156
     * @param string $suffix
1157
     * @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...
1158
     *
1159
     * @throws InvalidArgumentException
1160 12
     */
1161 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
1162 12
    {
1163 4
        if ($suffix !== \substr($value, -\strlen($suffix))) {
1164 4
            static::reportInvalidArgument(\sprintf(
1165 4
                $message ?: 'Expected a value to end with %2$s. Got: %s',
1166 4
                static::valueToString($value),
1167 4
                static::valueToString($suffix)
1168
            ));
1169
        }
1170 8
    }
1171
1172
    /**
1173
     * @psalm-pure
1174
     *
1175
     * @param string $value
1176
     * @param string $suffix
1177
     * @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...
1178
     *
1179
     * @throws InvalidArgumentException
1180 28
     */
1181 View Code Duplication
    public static function notEndsWith($value, $suffix, $message = '')
1182 28
    {
1183
        if ($suffix === \substr($value, -\strlen($suffix))) {
1184 28
            static::reportInvalidArgument(\sprintf(
1185 16
                $message ?: 'Expected a value not to end with %2$s. Got: %s',
1186 16
                static::valueToString($value),
1187 16
                static::valueToString($suffix)
1188
            ));
1189
        }
1190 12
    }
1191
1192
    /**
1193
     * @psalm-pure
1194
     *
1195
     * @param string $value
1196
     * @param string $pattern
1197
     * @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...
1198
     *
1199
     * @throws InvalidArgumentException
1200 20
     */
1201
    public static function regex($value, $pattern, $message = '')
1202 20
    {
1203
        if (!\preg_match($pattern, $value)) {
1204 12
            static::reportInvalidArgument(\sprintf(
1205 12
                $message ?: 'The value %s does not match the expected pattern.',
1206 12
                static::valueToString($value)
1207 12
            ));
1208
        }
1209 12
    }
1210 8
1211 8
    /**
1212 8
     * @psalm-pure
1213
     *
1214
     * @param string $value
1215 4
     * @param string $pattern
1216
     * @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...
1217
     *
1218
     * @throws InvalidArgumentException
1219
     */
1220
    public static function notRegex($value, $pattern, $message = '')
1221
    {
1222
        if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
1223
            static::reportInvalidArgument(\sprintf(
1224
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
1225 12
                static::valueToString($value),
1226
                static::valueToString($pattern),
1227 12
                $matches[0][1]
1228 12
            ));
1229 12
        }
1230 12
    }
1231
1232 12
    /**
1233 8
     * @psalm-pure
1234 8
     *
1235 8
     * @param mixed                    $value
1236
     * @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...
1237
     *
1238 4
     * @throws InvalidArgumentException
1239
     */
1240 View Code Duplication
    public static function unicodeLetters($value, $message = '')
1241
    {
1242
        static::string($value);
1243
1244
        if (!\preg_match('/^\p{L}+$/u', $value)) {
1245
            static::reportInvalidArgument(\sprintf(
1246
                $message ?: 'Expected a value to contain only Unicode letters. Got: %s',
1247
                static::valueToString($value)
1248 12
            ));
1249
        }
1250 12
    }
1251 12
1252 12
    /**
1253 12
     * @psalm-pure
1254
     *
1255 12
     * @param mixed                    $value
1256 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...
1257 8
     *
1258 8
     * @throws InvalidArgumentException
1259
     */
1260 View Code Duplication
    public static function alpha($value, $message = '')
1261 4
    {
1262
        static::string($value);
1263
1264
        $locale = \setlocale(LC_CTYPE, 0);
1265
        \setlocale(LC_CTYPE, 'C');
1266
        $valid = !\ctype_alpha($value);
1267
        \setlocale(LC_CTYPE, $locale);
1268
1269
        if ($valid) {
1270
            static::reportInvalidArgument(\sprintf(
1271
                $message ?: 'Expected a value to contain only letters. Got: %s',
1272 16
                static::valueToString($value)
1273
            ));
1274 16
        }
1275 16
    }
1276 16
1277 16
    /**
1278
     * @psalm-pure
1279 16
     *
1280 12
     * @param string $value
1281 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...
1282 12
     *
1283
     * @throws InvalidArgumentException
1284
     */
1285 4 View Code Duplication
    public static function digits($value, $message = '')
1286
    {
1287
        $locale = \setlocale(LC_CTYPE, 0);
1288
        \setlocale(LC_CTYPE, 'C');
1289
        $valid = !\ctype_digit($value);
1290
        \setlocale(LC_CTYPE, $locale);
1291
1292
        if ($valid) {
1293
            static::reportInvalidArgument(\sprintf(
1294
                $message ?: 'Expected a value to contain digits only. Got: %s',
1295
                static::valueToString($value)
1296 16
            ));
1297
        }
1298 16
    }
1299 16
1300 16
    /**
1301 16
     * @psalm-pure
1302
     *
1303 16
     * @param string $value
1304 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...
1305 12
     *
1306 12
     * @throws InvalidArgumentException
1307
     */
1308 View Code Duplication
    public static function alnum($value, $message = '')
1309 4
    {
1310
        $locale = \setlocale(LC_CTYPE, 0);
1311
        \setlocale(LC_CTYPE, 'C');
1312
        $valid = !\ctype_alnum($value);
1313
        \setlocale(LC_CTYPE, $locale);
1314
1315
        if ($valid) {
1316
            static::reportInvalidArgument(\sprintf(
1317
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
1318
                static::valueToString($value)
1319
            ));
1320 36
        }
1321
    }
1322 36
1323 24
    /**
1324 24
     * @psalm-pure
1325 24
     * @psalm-assert lowercase-string $value
1326 24
     *
1327
     * @param string $value
1328
     * @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...
1329 12
     *
1330
     * @throws InvalidArgumentException
1331
     */
1332 View Code Duplication
    public static function lower($value, $message = '')
1333
    {
1334
        $locale = \setlocale(LC_CTYPE, 0);
1335
        \setlocale(LC_CTYPE, 'C');
1336
        $valid = !\ctype_lower($value);
1337
        \setlocale(LC_CTYPE, $locale);
1338
1339
        if ($valid) {
1340
            static::reportInvalidArgument(\sprintf(
1341
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
1342 36
                static::valueToString($value)
1343
            ));
1344 36
        }
1345 12
    }
1346 12
1347 12
    /**
1348 12
     * @psalm-pure
1349
     * @psalm-assert !lowercase-string $value
1350
     *
1351 24
     * @param string $value
1352
     * @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...
1353
     *
1354
     * @throws InvalidArgumentException
1355
     */
1356 View Code Duplication
    public static function upper($value, $message = '')
1357
    {
1358
        $locale = \setlocale(LC_CTYPE, 0);
1359
        \setlocale(LC_CTYPE, 'C');
1360
        $valid = !\ctype_upper($value);
1361
        \setlocale(LC_CTYPE, $locale);
1362
1363
        if ($valid) {
1364 36
            static::reportInvalidArgument(\sprintf(
1365
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
1366 36
                static::valueToString($value)
1367 12
            ));
1368 12
        }
1369 12
    }
1370 12
1371
    /**
1372
     * @psalm-pure
1373 24
     *
1374
     * @param string $value
1375
     * @param int    $length
1376
     * @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...
1377
     *
1378
     * @throws InvalidArgumentException
1379
     */
1380 View Code Duplication
    public static function length($value, $length, $message = '')
1381
    {
1382
        if ($length !== static::strlen($value)) {
1383
            static::reportInvalidArgument(\sprintf(
1384
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
1385
                static::valueToString($value),
1386
                $length
1387 60
            ));
1388
        }
1389 60
    }
1390
1391 60
    /**
1392 24
     * Inclusive min.
1393 24
     *
1394 24
     * @psalm-pure
1395 24
     *
1396 24
     * @param string    $value
1397
     * @param int|float $min
1398
     * @param string    $message
1399 36
     *
1400
     * @throws InvalidArgumentException
1401
     */
1402 View Code Duplication
    public static function minLength($value, $min, $message = '')
1403
    {
1404
        if (static::strlen($value) < $min) {
1405
            static::reportInvalidArgument(\sprintf(
1406
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
1407
                static::valueToString($value),
1408
                $min
1409 36
            ));
1410
        }
1411 36
    }
1412
1413 36
    /**
1414 12
     * Inclusive max.
1415 12
     *
1416 12
     * @psalm-pure
1417
     *
1418
     * @param string    $value
1419 24
     * @param int|float $max
1420
     * @param string    $message
1421
     *
1422
     * @throws InvalidArgumentException
1423
     */
1424 View Code Duplication
    public static function maxLength($value, $max, $message = '')
1425
    {
1426
        if (static::strlen($value) > $max) {
1427 12
            static::reportInvalidArgument(\sprintf(
1428
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
1429 12
                static::valueToString($value),
1430
                $max
1431 8
            ));
1432 4
        }
1433 4
    }
1434 4
1435
    /**
1436
     * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion.
1437 4
     *
1438
     * @psalm-pure
1439
     *
1440
     * @param string    $value
1441
     * @param int|float $min
1442
     * @param int|float $max
1443
     * @param string    $message
1444
     *
1445 12
     * @throws InvalidArgumentException
1446
     */
1447 12 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
1448
    {
1449 8
        $length = static::strlen($value);
1450 4
1451 4
        if ($length < $min || $length > $max) {
1452 4
            static::reportInvalidArgument(\sprintf(
1453
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
1454
                static::valueToString($value),
1455 4
                $min,
1456
                $max
1457
            ));
1458
        }
1459
    }
1460
1461
    /**
1462
     * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file.
1463
     *
1464
     * @param mixed                    $value
1465
     * @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...
1466
     *
1467
     * @throws InvalidArgumentException
1468
     */
1469 View Code Duplication
    public static function fileExists($value, $message = '')
1470
    {
1471
        static::string($value);
1472
1473
        if (!\file_exists($value)) {
1474
            static::reportInvalidArgument(\sprintf(
1475
                $message ?: 'The file %s does not exist.',
1476
                static::valueToString($value)
1477
            ));
1478
        }
1479
    }
1480
1481
    /**
1482
     * @param mixed                    $value
1483
     * @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...
1484
     *
1485
     * @throws InvalidArgumentException
1486
     */
1487 View Code Duplication
    public static function file($value, $message = '')
1488
    {
1489
        static::fileExists($value, $message);
1490
1491
        if (!\is_file($value)) {
1492
            static::reportInvalidArgument(\sprintf(
1493
                $message ?: 'The path %s is not a file.',
1494
                static::valueToString($value)
1495
            ));
1496
        }
1497 8
    }
1498
1499 8
    /**
1500 4
     * @param mixed                    $value
1501 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...
1502 4
     *
1503
     * @throws InvalidArgumentException
1504
     */
1505 4 View Code Duplication
    public static function directory($value, $message = '')
1506
    {
1507
        static::fileExists($value, $message);
1508
1509
        if (!\is_dir($value)) {
1510
            static::reportInvalidArgument(\sprintf(
1511
                $message ?: 'The path %s is no directory.',
1512
                static::valueToString($value)
1513
            ));
1514
        }
1515
    }
1516
1517
    /**
1518
     * @param string $value
1519 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...
1520
     *
1521 8
     * @throws InvalidArgumentException
1522 4
     */
1523 4
    public static function readable($value, $message = '')
1524 4
    {
1525 4
        if (!\is_readable($value)) {
1526
            static::reportInvalidArgument(\sprintf(
1527
                $message ?: 'The path %s is not readable.',
1528 4
                static::valueToString($value)
1529
            ));
1530
        }
1531
    }
1532
1533
    /**
1534
     * @param string $value
1535
     * @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...
1536
     *
1537
     * @throws InvalidArgumentException
1538 8
     */
1539
    public static function writable($value, $message = '')
1540 8
    {
1541 4
        if (!\is_writable($value)) {
1542 4
            static::reportInvalidArgument(\sprintf(
1543 4
                $message ?: 'The path %s is not writable.',
1544
                static::valueToString($value)
1545
            ));
1546 4
        }
1547
    }
1548
1549
    /**
1550
     * @psalm-assert class-string $value
1551
     *
1552
     * @param mixed                    $value
1553
     * @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...
1554
     *
1555
     * @throws InvalidArgumentException
1556
     */
1557
    public static function classExists($value, $message = '')
1558
    {
1559
        if (!\class_exists($value)) {
1560 8
            static::reportInvalidArgument(\sprintf(
1561
                $message ?: 'Expected an existing class name. Got: %s',
1562 8
                static::valueToString($value)
1563 4
            ));
1564 4
        }
1565 4
    }
1566 4
1567
    /**
1568
     * @psalm-pure
1569 4
     * @psalm-template ExpectedType of object
1570
     * @psalm-param class-string<ExpectedType> $class
1571
     * @psalm-assert class-string<ExpectedType>|ExpectedType $value
1572
     *
1573
     * @param mixed         $value
1574
     * @param string|object $class
1575
     * @param string        $message
1576
     *
1577
     * @throws InvalidArgumentException
1578
     */
1579
    public static function subclassOf($value, $class, $message = '')
1580
    {
1581 12
        if (!\is_subclass_of($value, $class)) {
1582
            static::reportInvalidArgument(\sprintf(
1583 12
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
1584 4
                static::valueToString($value),
1585 4
                static::valueToString($class)
1586 4
            ));
1587
        }
1588
    }
1589 8
1590
    /**
1591
     * @psalm-assert class-string $value
1592
     *
1593
     * @param mixed                    $value
1594
     * @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...
1595
     *
1596
     * @throws InvalidArgumentException
1597
     */
1598
    public static function interfaceExists($value, $message = '')
1599
    {
1600
        if (!\interface_exists($value)) {
1601 12
            static::reportInvalidArgument(\sprintf(
1602
                $message ?: 'Expected an existing interface name. got %s',
1603 12
                static::valueToString($value)
1604 8
            ));
1605 8
        }
1606 8
    }
1607
1608
    /**
1609 4
     * @psalm-pure
1610
     * @psalm-template ExpectedType of object
1611
     * @psalm-param class-string<ExpectedType> $interface
1612
     * @psalm-assert class-string<ExpectedType> $value
1613
     *
1614
     * @param mixed                    $value
1615
     * @param mixed  $interface
1616
     * @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...
1617
     *
1618
     * @throws InvalidArgumentException
1619
     */
1620 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
1621 27
    {
1622
        if (!\in_array($interface, \class_implements($value))) {
1623 27
            static::reportInvalidArgument(\sprintf(
1624 19
                $message ?: 'Expected an implementation of %2$s. Got: %s',
1625 19
                static::valueToString($value),
1626 19
                static::valueToString($interface)
1627
            ));
1628
        }
1629 8
    }
1630
1631
    /**
1632
     * @psalm-pure
1633
     * @psalm-param class-string|object $classOrObject
1634
     *
1635
     * @param string|object $classOrObject
1636
     * @param mixed         $property
1637
     * @param string        $message
1638
     *
1639
     * @throws InvalidArgumentException
1640
     */
1641 27 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
1642
    {
1643 27
        if (!\property_exists($classOrObject, $property)) {
1644 8
            static::reportInvalidArgument(\sprintf(
1645 8
                $message ?: 'Expected the property %s to exist.',
1646 8
                static::valueToString($property)
1647
            ));
1648
        }
1649 19
    }
1650
1651
    /**
1652
     * @psalm-pure
1653
     * @psalm-param class-string|object $classOrObject
1654
     *
1655
     * @param string|object $classOrObject
1656
     * @param mixed         $property
1657
     * @param string        $message
1658
     *
1659
     * @throws InvalidArgumentException
1660 12
     */
1661 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
1662 12
    {
1663 4
        if (\property_exists($classOrObject, $property)) {
1664 4
            static::reportInvalidArgument(\sprintf(
1665 4
                $message ?: 'Expected the property %s to not exist.',
1666
                static::valueToString($property)
1667
            ));
1668 8
        }
1669
    }
1670
1671
    /**
1672
     * @psalm-pure
1673
     * @psalm-param class-string|object $classOrObject
1674
     *
1675
     * @param string|object $classOrObject
1676
     * @param mixed         $method
1677
     * @param string        $message
1678
     *
1679 12
     * @throws InvalidArgumentException
1680
     */
1681 12 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
1682 8
    {
1683 8
        if (!(\is_string($classOrObject) || \is_object($classOrObject)) || !\method_exists($classOrObject, $method)) {
1684 8
            static::reportInvalidArgument(\sprintf(
1685
                $message ?: 'Expected the method %s to exist.',
1686
                static::valueToString($method)
1687 4
            ));
1688
        }
1689
    }
1690
1691
    /**
1692
     * @psalm-pure
1693
     * @psalm-param class-string|object $classOrObject
1694
     *
1695
     * @param string|object $classOrObject
1696
     * @param mixed         $method
1697
     * @param string        $message
1698
     *
1699
     * @throws InvalidArgumentException
1700 28
     */
1701 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
1702 28
    {
1703 20
        if ((\is_string($classOrObject) || \is_object($classOrObject)) && \method_exists($classOrObject, $method)) {
1704 20
            static::reportInvalidArgument(\sprintf(
1705 20
                $message ?: 'Expected the method %s to not exist.',
1706
                static::valueToString($method)
1707
            ));
1708 8
        }
1709
    }
1710
1711
    /**
1712
     * @psalm-pure
1713
     *
1714
     * @param array      $array
1715
     * @param string|int $key
1716
     * @param string     $message
1717
     *
1718
     * @throws InvalidArgumentException
1719 8
     */
1720 View Code Duplication
    public static function keyExists($array, $key, $message = '')
1721 8
    {
1722 8
        if (!(isset($array[$key]) || \array_key_exists($key, $array))) {
1723
            static::reportInvalidArgument(\sprintf(
1724 8
                $message ?: 'Expected the key %s to exist.',
1725 8
                static::valueToString($key)
1726 8
            ));
1727 8
        }
1728
    }
1729
1730 4
    /**
1731
     * @psalm-pure
1732
     *
1733
     * @param array      $array
1734
     * @param string|int $key
1735
     * @param string     $message
1736
     *
1737
     * @throws InvalidArgumentException
1738
     */
1739 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
1740
    {
1741 12
        if (isset($array[$key]) || \array_key_exists($key, $array)) {
1742
            static::reportInvalidArgument(\sprintf(
1743 12
                $message ?: 'Expected the key %s to not exist.',
1744 4
                static::valueToString($key)
1745 4
            ));
1746 4
        }
1747 4
    }
1748
1749
    /**
1750 8
     * Checks if a value is a valid array key (int or string).
1751
     *
1752
     * @psalm-pure
1753
     * @psalm-assert array-key $value
1754
     *
1755
     * @param mixed                    $value
1756
     * @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...
1757
     *
1758
     * @throws InvalidArgumentException
1759
     */
1760
    public static function validArrayKey($value, $message = '')
1761 12
    {
1762
        if (!(\is_int($value) || \is_string($value))) {
1763 12
            static::reportInvalidArgument(
1764 4
                is_callable($message)
1765 4
                    ? $message()
1766 4
                    : sprintf(
1767 4
                        $message ?: 'Expected string or integer. Got: %s',
1768
                        static::typeToString($value)
1769
                    )
1770 8
            );
1771
        }
1772
    }
1773
1774
    /**
1775
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1776
     *
1777
     * @param Countable|array $array
1778
     * @param int             $number
1779
     * @param string          $message
1780
     *
1781
     * @throws InvalidArgumentException
1782 20
     */
1783
    public static function count($array, $number, $message = '')
1784 20
    {
1785
        static::eq(
1786 20
            \count($array),
1787 8
            $number,
1788 8
            \sprintf(
1789 8
                $message ?: 'Expected an array to contain %d elements. Got: %d.',
1790 8
                $number,
1791 8
                \count($array)
1792
            )
1793
        );
1794 12
    }
1795
1796
    /**
1797
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1798
     *
1799
     * @param Countable|array $array
1800
     * @param int|float       $min
1801
     * @param string          $message
1802
     *
1803
     * @throws InvalidArgumentException
1804
     */
1805 80 View Code Duplication
    public static function minCount($array, $min, $message = '')
1806
    {
1807 80
        if (\count($array) < $min) {
1808 32
            static::reportInvalidArgument(\sprintf(
1809 32
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
1810
                \count($array),
1811
                $min
1812 48
            ));
1813
        }
1814
    }
1815
1816
    /**
1817
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1818
     *
1819
     * @param Countable|array $array
1820
     * @param int|float       $max
1821
     * @param string          $message
1822
     *
1823 40
     * @throws InvalidArgumentException
1824
     */
1825 40 View Code Duplication
    public static function maxCount($array, $max, $message = '')
1826 24
    {
1827 20
        if (\count($array) > $max) {
1828
            static::reportInvalidArgument(\sprintf(
1829
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
1830
                \count($array),
1831
                $max
1832
            ));
1833
        }
1834
    }
1835
1836
    /**
1837
     * Does not check if $array is countable, this can generate a warning on php versions after 7.2.
1838
     *
1839
     * @param Countable|array $array
1840 32
     * @param int|float       $min
1841
     * @param int|float       $max
1842
     * @param string          $message
1843 32
     *
1844 32
     * @throws InvalidArgumentException
1845
     */
1846 16
    public static function countBetween($array, $min, $max, $message = '')
1847 16
    {
1848
        $count = \count($array);
1849
1850 16
        if ($count < $min || $count > $max) {
1851
            static::reportInvalidArgument(\sprintf(
1852
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
1853
                $count,
1854
                $min,
1855
                $max
1856
            ));
1857
        }
1858
    }
1859
1860
    /**
1861
     * @psalm-pure
1862
     * @psalm-assert list $array
1863
     *
1864 16
     * @param mixed  $array
1865
     * @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...
1866 16
     *
1867 8
     * @throws InvalidArgumentException
1868 4
     */
1869 View Code Duplication
    public static function isList($array, $message = '')
1870
    {
1871
        if (!\is_array($array) || $array !== \array_values($array)) {
1872
            static::reportInvalidArgument(
1873
                $message ?: 'Expected list - non-associative array.'
1874
            );
1875
        }
1876
    }
1877
1878 56
    /**
1879
     * @psalm-pure
1880 56
     * @psalm-assert non-empty-list $array
1881
     *
1882
     * @param mixed  $array
1883
     * @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...
1884 56
     *
1885 4
     * @throws InvalidArgumentException
1886
     */
1887
    public static function isNonEmptyList($array, $message = '')
1888 52
    {
1889 20
        static::isList($array, $message);
1890 20
        static::notEmpty($array, $message);
1891 20
    }
1892
1893
    /**
1894 32
     * @psalm-pure
1895
     * @psalm-template T
1896
     * @psalm-param mixed|array<T> $array
1897
     * @psalm-assert array<string, T> $array
1898
     *
1899
     * @param mixed  $array
1900
     * @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...
1901
     *
1902
     * @throws InvalidArgumentException
1903
     */
1904 View Code Duplication
    public static function isMap($array, $message = '')
1905 24
    {
1906
        if (
1907 24
            !\is_array($array) ||
1908
            \array_keys($array) !== \array_filter(\array_keys($array), '\is_string')
1909 24
        ) {
1910
            static::reportInvalidArgument(
1911
                $message ?: 'Expected map - associative array with string keys.'
1912 24
            );
1913 24
        }
1914 20
    }
1915 20
1916 20
    /**
1917
     * @psalm-pure
1918 4
     * @psalm-template T
1919 4
     * @psalm-param mixed|array<T> $array
1920 4
     * @psalm-assert array<string, T> $array
1921 4
     * @psalm-assert !empty $array
1922
     *
1923
     * @param mixed  $array
1924
     * @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...
1925 8
     *
1926 8
     * @throws InvalidArgumentException
1927 8
     */
1928 8
    public static function isNonEmptyMap($array, $message = '')
1929
    {
1930
        static::isMap($array, $message);
1931
        static::notEmpty($array, $message);
1932
    }
1933
1934
    /**
1935 1642
     * @psalm-pure
1936
     *
1937 1642
     * @param string $value
1938 607
     * @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...
1939 501
     *
1940 501
     * @throws InvalidArgumentException
1941
     */
1942
    public static function uuid($value, $message = '')
1943 354
    {
1944
        $value = \str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
1945
1946 1035
        // The nil UUID is special form of UUID that is specified to have all
1947 1034
        // 128 bits set to zero.
1948
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1949 1034
            return;
1950 1034
        }
1951
1952 1034
        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)) {
1953 1034
            static::reportInvalidArgument(\sprintf(
1954
                $message ?: 'Value %s is not a valid UUID.',
1955 1034
                static::valueToString($value)
1956
            ));
1957
        }
1958 504
    }
1959
1960
    /**
1961 1
     * @psalm-param class-string<Throwable> $class
1962
     *
1963
     * @param Closure $expression
1964
     * @param string  $class
1965
     * @param string  $message
1966
     *
1967
     * @throws InvalidArgumentException
1968
     */
1969 751
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1970
    {
1971 751
        static::string($class);
1972 20
1973
        $actual = 'none';
1974
1975 733
        try {
1976 15
            $expression();
1977
        } catch (Exception $e) {
1978
            $actual = \get_class($e);
1979 723
            if ($e instanceof $class) {
1980 25
                return;
1981
            }
1982
        } 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...
1983 698
            $actual = \get_class($e);
1984 21
            if ($e instanceof $class) {
1985
                return;
1986
            }
1987 677
        }
1988 3
1989 1
        static::reportInvalidArgument($message ?: \sprintf(
1990
            'Expected to throw "%s", got "%s"',
1991
            $class,
1992 2
            $actual
1993 1
        ));
1994
    }
1995
1996 1
    /**
1997
     * @throws BadMethodCallException
1998
     */
1999 676
    public static function __callStatic($name, $arguments)
2000 1
    {
2001
        if ('nullOr' === \substr($name, 0, 6)) {
2002
            if (null !== $arguments[0]) {
2003 676
                $method = \lcfirst(\substr($name, 6));
2004 574
                \call_user_func_array(array('static', $method), $arguments);
2005
            }
2006
2007 114
            return;
2008
        }
2009
2010
        if ('all' === \substr($name, 0, 3)) {
2011
            static::isIterable($arguments[0]);
2012
2013
            $method = \lcfirst(\substr($name, 3));
2014
            $args = $arguments;
2015 251
2016
            foreach ($arguments[0] as $entry) {
2017 251
                $args[0] = $entry;
2018
2019
                \call_user_func_array(array('static', $method), $args);
2020 168
            }
2021
2022 168
            return;
2023
        }
2024
2025
        throw new BadMethodCallException('No such method: '.$name);
2026 168
    }
2027
2028
    /**
2029
     * @param mixed $value
2030 168
     *
2031
     * @return string
2032
     */
2033
    protected static function valueToString($value)
2034
    {
2035
        if (null === $value) {
2036
            return 'null';
2037
        }
2038
2039
        if (true === $value) {
2040 1065
            return 'true';
2041
        }
2042 1065
2043
        if (false === $value) {
2044
            return 'false';
2045
        }
2046
2047
        if (\is_array($value)) {
2048
            return 'array';
2049
        }
2050
2051
        if (\is_object($value)) {
2052
            if (\method_exists($value, '__toString')) {
2053
                return \get_class($value).': '.self::valueToString($value->__toString());
2054
            }
2055
2056
            if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
2057
                return \get_class($value).': '.self::valueToString($value->format('c'));
2058
            }
2059
2060
            return \get_class($value);
2061
        }
2062
2063
        if (\is_resource($value)) {
2064
            return 'resource';
2065
        }
2066
2067
        if (\is_string($value)) {
2068
            return '"'.$value.'"';
2069
        }
2070
2071
        return (string) $value;
2072
    }
2073
2074
    /**
2075
     * @param mixed $value
2076
     *
2077
     * @return string
2078
     */
2079
    protected static function typeToString($value)
2080
    {
2081
        return \is_object($value) ? \get_class($value) : \gettype($value);
2082
    }
2083
2084
    protected static function strlen($value)
2085
    {
2086
        if (!\function_exists('mb_detect_encoding')) {
2087
            return \strlen($value);
2088
        }
2089
2090
        if (false === $encoding = \mb_detect_encoding($value)) {
2091
            return \strlen($value);
2092
        }
2093
2094
        return \mb_strlen($value, $encoding);
2095
    }
2096
2097
    /**
2098
     * @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...
2099
     *
2100
     * @throws InvalidArgumentException
2101
     *
2102
     * @psalm-pure this method is not supposed to perform side-effects
2103
     */
2104
    protected static function reportInvalidArgument($message)
2105
    {
2106
        if (is_callable($message)) {
2107
            $message = $message();
2108
        }
2109
2110
        throw new InvalidArgumentException($message);
2111
    }
2112
2113
    private function __construct()
2114
    {
2115
    }
2116
}
2117