Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Assert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Assert, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
215 | class Assert |
||
|
|||
216 | { |
||
217 | /** |
||
218 | * @psalm-pure |
||
219 | * @psalm-assert string $value |
||
220 | * |
||
221 | * @param mixed $value |
||
222 | * @param string $message |
||
223 | * |
||
224 | * @throws InvalidArgumentException |
||
225 | */ |
||
226 | 241 | public static function string($value, $message = '') |
|
227 | { |
||
228 | 241 | if (!\is_string($value)) { |
|
229 | 45 | static::reportInvalidArgument(\sprintf( |
|
230 | 45 | $message ?: 'Expected a string. Got: %s', |
|
231 | 45 | static::typeToString($value) |
|
232 | )); |
||
233 | } |
||
234 | 196 | } |
|
235 | |||
236 | /** |
||
237 | * @psalm-pure |
||
238 | * @psalm-assert string $value |
||
239 | * @psalm-assert !empty $value |
||
240 | * |
||
241 | * @param mixed $value |
||
242 | * @param string $message |
||
243 | * |
||
244 | * @throws InvalidArgumentException |
||
245 | */ |
||
246 | 16 | public static function stringNotEmpty($value, $message = '') |
|
247 | { |
||
248 | 16 | static::string($value, $message); |
|
249 | 12 | static::notEq($value, '', $message); |
|
250 | 8 | } |
|
251 | |||
252 | /** |
||
253 | * @psalm-pure |
||
254 | * @psalm-assert int $value |
||
255 | * |
||
256 | * @param mixed $value |
||
257 | * @param string $message |
||
258 | * |
||
259 | * @throws InvalidArgumentException |
||
260 | */ |
||
261 | 17 | public static function integer($value, $message = '') |
|
262 | { |
||
263 | 17 | if (!\is_int($value)) { |
|
264 | 13 | static::reportInvalidArgument(\sprintf( |
|
265 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
266 | 13 | static::typeToString($value) |
|
267 | )); |
||
268 | } |
||
269 | 4 | } |
|
270 | |||
271 | /** |
||
272 | * @psalm-pure |
||
273 | * @psalm-assert numeric $value |
||
274 | * |
||
275 | * @param mixed $value |
||
276 | * @param string $message |
||
277 | * |
||
278 | * @throws InvalidArgumentException |
||
279 | */ |
||
280 | 16 | public static function integerish($value, $message = '') |
|
281 | { |
||
282 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
283 | 4 | static::reportInvalidArgument(\sprintf( |
|
284 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
285 | 4 | static::typeToString($value) |
|
286 | )); |
||
287 | } |
||
288 | 12 | } |
|
289 | |||
290 | /** |
||
291 | * @psalm-pure |
||
292 | * @psalm-assert float $value |
||
293 | * |
||
294 | * @param mixed $value |
||
295 | * @param string $message |
||
296 | * |
||
297 | * @throws InvalidArgumentException |
||
298 | */ |
||
299 | 16 | public static function float($value, $message = '') |
|
300 | { |
||
301 | 16 | if (!\is_float($value)) { |
|
302 | 8 | static::reportInvalidArgument(\sprintf( |
|
303 | 8 | $message ?: 'Expected a float. Got: %s', |
|
304 | 8 | static::typeToString($value) |
|
305 | )); |
||
306 | } |
||
307 | 8 | } |
|
308 | |||
309 | /** |
||
310 | * @psalm-pure |
||
311 | * @psalm-assert numeric $value |
||
312 | * |
||
313 | * @param mixed $value |
||
314 | * @param string $message |
||
315 | * |
||
316 | * @throws InvalidArgumentException |
||
317 | */ |
||
318 | 20 | public static function numeric($value, $message = '') |
|
319 | { |
||
320 | 20 | if (!\is_numeric($value)) { |
|
321 | 4 | static::reportInvalidArgument(\sprintf( |
|
322 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
323 | 4 | static::typeToString($value) |
|
324 | )); |
||
325 | } |
||
326 | 16 | } |
|
327 | |||
328 | /** |
||
329 | * @psalm-pure |
||
330 | * @psalm-assert int $value |
||
331 | * |
||
332 | * @param mixed $value |
||
333 | * @param string $message |
||
334 | * |
||
335 | * @throws InvalidArgumentException |
||
336 | */ |
||
337 | 24 | public static function natural($value, $message = '') |
|
338 | { |
||
339 | 24 | if (!\is_int($value) || $value < 0) { |
|
340 | 16 | static::reportInvalidArgument(\sprintf( |
|
341 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
342 | 16 | static::valueToString($value) |
|
343 | )); |
||
344 | } |
||
345 | 8 | } |
|
346 | |||
347 | /** |
||
348 | * @psalm-pure |
||
349 | * @psalm-assert bool $value |
||
350 | * |
||
351 | * @param mixed $value |
||
352 | * @param string $message |
||
353 | * |
||
354 | * @throws InvalidArgumentException |
||
355 | */ |
||
356 | 16 | public static function boolean($value, $message = '') |
|
357 | { |
||
358 | 16 | if (!\is_bool($value)) { |
|
359 | 8 | static::reportInvalidArgument(\sprintf( |
|
360 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
361 | 8 | static::typeToString($value) |
|
362 | )); |
||
363 | } |
||
364 | 8 | } |
|
365 | |||
366 | /** |
||
367 | * @psalm-pure |
||
368 | * @psalm-assert scalar $value |
||
369 | * |
||
370 | * @param mixed $value |
||
371 | * @param string $message |
||
372 | * |
||
373 | * @throws InvalidArgumentException |
||
374 | */ |
||
375 | 23 | public static function scalar($value, $message = '') |
|
376 | { |
||
377 | 23 | if (!\is_scalar($value)) { |
|
378 | 11 | static::reportInvalidArgument(\sprintf( |
|
379 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
380 | 11 | static::typeToString($value) |
|
381 | )); |
||
382 | } |
||
383 | 12 | } |
|
384 | |||
385 | /** |
||
386 | * @psalm-pure |
||
387 | * @psalm-assert object $value |
||
388 | * |
||
389 | * @param mixed $value |
||
390 | * @param string $message |
||
391 | * |
||
392 | * @throws InvalidArgumentException |
||
393 | */ |
||
394 | 23 | public static function object($value, $message = '') |
|
395 | { |
||
396 | 23 | if (!\is_object($value)) { |
|
397 | 15 | static::reportInvalidArgument(\sprintf( |
|
398 | 15 | $message ?: 'Expected an object. Got: %s', |
|
399 | 15 | static::typeToString($value) |
|
400 | )); |
||
401 | } |
||
402 | 8 | } |
|
403 | |||
404 | /** |
||
405 | * @psalm-pure |
||
406 | * @psalm-assert resource $value |
||
407 | * |
||
408 | * @param mixed $value |
||
409 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
410 | * @param string $message |
||
411 | * |
||
412 | * @throws InvalidArgumentException |
||
413 | */ |
||
414 | 16 | public static function resource($value, $type = null, $message = '') |
|
415 | { |
||
416 | 16 | if (!\is_resource($value)) { |
|
417 | 4 | static::reportInvalidArgument(\sprintf( |
|
418 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
419 | 4 | static::typeToString($value) |
|
420 | )); |
||
421 | } |
||
422 | |||
423 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
424 | 4 | static::reportInvalidArgument(\sprintf( |
|
425 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
426 | 4 | static::typeToString($value), |
|
427 | 4 | $type |
|
428 | )); |
||
429 | } |
||
430 | 8 | } |
|
431 | |||
432 | /** |
||
433 | * @psalm-pure |
||
434 | * @psalm-assert callable $value |
||
435 | * |
||
436 | * @param mixed $value |
||
437 | * @param string $message |
||
438 | * |
||
439 | * @throws InvalidArgumentException |
||
440 | */ |
||
441 | 20 | public static function isCallable($value, $message = '') |
|
442 | { |
||
443 | 20 | if (!\is_callable($value)) { |
|
444 | 8 | static::reportInvalidArgument(\sprintf( |
|
445 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
446 | 8 | static::typeToString($value) |
|
447 | )); |
||
448 | } |
||
449 | 12 | } |
|
450 | |||
451 | /** |
||
452 | * @psalm-pure |
||
453 | * @psalm-assert array $value |
||
454 | * |
||
455 | * @param mixed $value |
||
456 | * @param string $message |
||
457 | * |
||
458 | * @throws InvalidArgumentException |
||
459 | */ |
||
460 | 20 | public static function isArray($value, $message = '') |
|
461 | { |
||
462 | 20 | if (!\is_array($value)) { |
|
463 | 12 | static::reportInvalidArgument(\sprintf( |
|
464 | 12 | $message ?: 'Expected an array. Got: %s', |
|
465 | 12 | static::typeToString($value) |
|
466 | )); |
||
467 | } |
||
468 | 8 | } |
|
469 | |||
470 | /** |
||
471 | * @psalm-pure |
||
472 | * @psalm-assert iterable $value |
||
473 | * |
||
474 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
475 | * |
||
476 | * @param mixed $value |
||
477 | * @param string $message |
||
478 | * |
||
479 | * @throws InvalidArgumentException |
||
480 | */ |
||
481 | 20 | public static function isTraversable($value, $message = '') |
|
482 | { |
||
483 | 20 | @\trigger_error( |
|
484 | 20 | \sprintf( |
|
485 | 20 | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', |
|
486 | 20 | __METHOD__ |
|
487 | ), |
||
488 | 20 | \E_USER_DEPRECATED |
|
489 | ); |
||
490 | |||
491 | 20 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
492 | 8 | static::reportInvalidArgument(\sprintf( |
|
493 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
494 | 8 | static::typeToString($value) |
|
495 | )); |
||
496 | } |
||
497 | 12 | } |
|
498 | |||
499 | /** |
||
500 | * @psalm-pure |
||
501 | * @psalm-assert array|ArrayAccess $value |
||
502 | * |
||
503 | * @param mixed $value |
||
504 | * @param string $message |
||
505 | * |
||
506 | * @throws InvalidArgumentException |
||
507 | */ |
||
508 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
509 | { |
||
510 | 20 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
|
511 | 8 | static::reportInvalidArgument(\sprintf( |
|
512 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
513 | 8 | static::typeToString($value) |
|
514 | )); |
||
515 | } |
||
516 | 12 | } |
|
517 | |||
518 | /** |
||
519 | * @psalm-pure |
||
520 | * @psalm-assert countable $value |
||
521 | * |
||
522 | * @param mixed $value |
||
523 | * @param string $message |
||
524 | * |
||
525 | * @throws InvalidArgumentException |
||
526 | */ |
||
527 | 28 | public static function isCountable($value, $message = '') |
|
528 | { |
||
529 | if ( |
||
530 | 28 | !\is_array($value) |
|
531 | 28 | && !($value instanceof Countable) |
|
532 | 28 | && !($value instanceof ResourceBundle) |
|
533 | 28 | && !($value instanceof SimpleXMLElement) |
|
534 | ) { |
||
535 | 12 | static::reportInvalidArgument(\sprintf( |
|
536 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
537 | 12 | static::typeToString($value) |
|
538 | )); |
||
539 | } |
||
540 | 16 | } |
|
541 | |||
542 | /** |
||
543 | * @psalm-pure |
||
544 | * @psalm-assert iterable $value |
||
545 | * |
||
546 | * @param mixed $value |
||
547 | * @param string $message |
||
548 | * |
||
549 | * @throws InvalidArgumentException |
||
550 | */ |
||
551 | 1040 | View Code Duplication | public static function isIterable($value, $message = '') |
552 | { |
||
553 | 1040 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
554 | 8 | static::reportInvalidArgument(\sprintf( |
|
555 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
556 | 8 | static::typeToString($value) |
|
557 | )); |
||
558 | } |
||
559 | 1036 | } |
|
560 | |||
561 | /** |
||
562 | * @psalm-pure |
||
563 | * @psalm-template ExpectedType of object |
||
564 | * @psalm-param class-string<ExpectedType> $class |
||
565 | * @psalm-assert ExpectedType $value |
||
566 | * |
||
567 | * @param mixed $value |
||
568 | * @param string|object $class |
||
569 | * @param string $message |
||
570 | * |
||
571 | * @throws InvalidArgumentException |
||
572 | */ |
||
573 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
574 | { |
||
575 | 19 | if (!($value instanceof $class)) { |
|
576 | 15 | static::reportInvalidArgument(\sprintf( |
|
577 | 15 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
578 | 15 | static::typeToString($value), |
|
579 | 15 | $class |
|
580 | )); |
||
581 | } |
||
582 | 4 | } |
|
583 | |||
584 | /** |
||
585 | * @psalm-pure |
||
586 | * @psalm-template ExpectedType of object |
||
587 | * @psalm-param class-string<ExpectedType> $class |
||
588 | * @psalm-assert !ExpectedType $value |
||
589 | * |
||
590 | * @param mixed $value |
||
591 | * @param string|object $class |
||
592 | * @param string $message |
||
593 | * |
||
594 | * @throws InvalidArgumentException |
||
595 | */ |
||
596 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
597 | { |
||
598 | 16 | if ($value instanceof $class) { |
|
599 | 4 | static::reportInvalidArgument(\sprintf( |
|
600 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
601 | 4 | static::typeToString($value), |
|
602 | 4 | $class |
|
603 | )); |
||
604 | } |
||
605 | 12 | } |
|
606 | |||
607 | /** |
||
608 | * @psalm-pure |
||
609 | * @psalm-param array<class-string> $classes |
||
610 | * |
||
611 | * @param mixed $value |
||
612 | * @param array<object|string> $classes |
||
613 | * @param string $message |
||
614 | * |
||
615 | * @throws InvalidArgumentException |
||
616 | */ |
||
617 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
618 | { |
||
619 | 20 | foreach ($classes as $class) { |
|
620 | 20 | if ($value instanceof $class) { |
|
621 | 8 | return; |
|
622 | } |
||
623 | } |
||
624 | |||
625 | 12 | static::reportInvalidArgument(\sprintf( |
|
626 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
627 | 12 | static::typeToString($value), |
|
628 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
629 | )); |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * @psalm-pure |
||
634 | * @psalm-template ExpectedType of object |
||
635 | * @psalm-param class-string<ExpectedType> $class |
||
636 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
||
637 | * |
||
638 | * @param object|string $value |
||
639 | * @param string $class |
||
640 | * @param string $message |
||
641 | * |
||
642 | * @throws InvalidArgumentException |
||
643 | */ |
||
644 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
645 | { |
||
646 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
647 | |||
648 | 16 | if (!\is_a($value, $class, \is_string($value))) { |
|
649 | 12 | static::reportInvalidArgument(sprintf( |
|
650 | 12 | $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', |
|
651 | 12 | static::typeToString($value), |
|
652 | $class |
||
653 | )); |
||
654 | } |
||
655 | 4 | } |
|
656 | |||
657 | /** |
||
658 | * @psalm-pure |
||
659 | * @psalm-template UnexpectedType of object |
||
660 | * @psalm-param class-string<UnexpectedType> $class |
||
661 | * @psalm-assert !UnexpectedType $value |
||
662 | * @psalm-assert !class-string<UnexpectedType> $value |
||
663 | * |
||
664 | * @param object|string $value |
||
665 | * @param string $class |
||
666 | * @param string $message |
||
667 | * |
||
668 | * @throws InvalidArgumentException |
||
669 | */ |
||
670 | 20 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
671 | { |
||
672 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
673 | |||
674 | 16 | if (\is_a($value, $class, \is_string($value))) { |
|
675 | 4 | static::reportInvalidArgument(sprintf( |
|
676 | 4 | $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
677 | 4 | static::typeToString($value), |
|
678 | $class |
||
679 | )); |
||
680 | } |
||
681 | 12 | } |
|
682 | |||
683 | /** |
||
684 | * @psalm-pure |
||
685 | * @psalm-param array<class-string> $classes |
||
686 | * |
||
687 | * @param object|string $value |
||
688 | * @param string[] $classes |
||
689 | * @param string $message |
||
690 | * |
||
691 | * @throws InvalidArgumentException |
||
692 | */ |
||
693 | 24 | public static function isAnyOf($value, array $classes, $message = '') |
|
694 | { |
||
695 | 24 | foreach ($classes as $class) { |
|
696 | 24 | static::string($class, 'Expected class as a string. Got: %s'); |
|
697 | |||
698 | 20 | if (\is_a($value, $class, \is_string($value))) { |
|
699 | 8 | return; |
|
700 | } |
||
701 | } |
||
702 | |||
703 | 12 | static::reportInvalidArgument(sprintf( |
|
704 | 12 | $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
705 | 12 | static::typeToString($value), |
|
706 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
707 | )); |
||
708 | } |
||
709 | |||
710 | /** |
||
711 | * @psalm-pure |
||
712 | * @psalm-assert empty $value |
||
713 | * |
||
714 | * @param mixed $value |
||
715 | * @param string $message |
||
716 | * |
||
717 | * @throws InvalidArgumentException |
||
718 | */ |
||
719 | 23 | public static function isEmpty($value, $message = '') |
|
720 | { |
||
721 | 23 | if (!empty($value)) { |
|
722 | 8 | static::reportInvalidArgument(\sprintf( |
|
723 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
724 | 8 | static::valueToString($value) |
|
725 | )); |
||
726 | } |
||
727 | 15 | } |
|
728 | |||
729 | /** |
||
730 | * @psalm-pure |
||
731 | * @psalm-assert !empty $value |
||
732 | * |
||
733 | * @param mixed $value |
||
734 | * @param string $message |
||
735 | * |
||
736 | * @throws InvalidArgumentException |
||
737 | */ |
||
738 | 55 | public static function notEmpty($value, $message = '') |
|
739 | { |
||
740 | 55 | if (empty($value)) { |
|
741 | 23 | static::reportInvalidArgument(\sprintf( |
|
742 | 23 | $message ?: 'Expected a non-empty value. Got: %s', |
|
743 | 23 | static::valueToString($value) |
|
744 | )); |
||
745 | } |
||
746 | 32 | } |
|
747 | |||
748 | /** |
||
749 | * @psalm-pure |
||
750 | * @psalm-assert null $value |
||
751 | * |
||
752 | * @param mixed $value |
||
753 | * @param string $message |
||
754 | * |
||
755 | * @throws InvalidArgumentException |
||
756 | */ |
||
757 | 11 | public static function null($value, $message = '') |
|
758 | { |
||
759 | 11 | if (null !== $value) { |
|
760 | 8 | static::reportInvalidArgument(\sprintf( |
|
761 | 8 | $message ?: 'Expected null. Got: %s', |
|
762 | 8 | static::valueToString($value) |
|
763 | )); |
||
764 | } |
||
765 | 3 | } |
|
766 | |||
767 | /** |
||
768 | * @psalm-pure |
||
769 | * @psalm-assert !null $value |
||
770 | * |
||
771 | * @param mixed $value |
||
772 | * @param string $message |
||
773 | * |
||
774 | * @throws InvalidArgumentException |
||
775 | */ |
||
776 | 11 | public static function notNull($value, $message = '') |
|
777 | { |
||
778 | 11 | if (null === $value) { |
|
779 | 3 | static::reportInvalidArgument( |
|
780 | 3 | $message ?: 'Expected a value other than null.' |
|
781 | ); |
||
782 | } |
||
783 | 8 | } |
|
784 | |||
785 | /** |
||
786 | * @psalm-pure |
||
787 | * @psalm-assert true $value |
||
788 | * |
||
789 | * @param mixed $value |
||
790 | * @param string $message |
||
791 | * |
||
792 | * @throws InvalidArgumentException |
||
793 | */ |
||
794 | 15 | public static function true($value, $message = '') |
|
795 | { |
||
796 | 15 | if (true !== $value) { |
|
797 | 11 | static::reportInvalidArgument(\sprintf( |
|
798 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
799 | 11 | static::valueToString($value) |
|
800 | )); |
||
801 | } |
||
802 | 4 | } |
|
803 | |||
804 | /** |
||
805 | * @psalm-pure |
||
806 | * @psalm-assert false $value |
||
807 | * |
||
808 | * @param mixed $value |
||
809 | * @param string $message |
||
810 | * |
||
811 | * @throws InvalidArgumentException |
||
812 | */ |
||
813 | 19 | public static function false($value, $message = '') |
|
814 | { |
||
815 | 19 | if (false !== $value) { |
|
816 | 15 | static::reportInvalidArgument(\sprintf( |
|
817 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
818 | 15 | static::valueToString($value) |
|
819 | )); |
||
820 | } |
||
821 | 4 | } |
|
822 | |||
823 | /** |
||
824 | * @psalm-assert !false $value |
||
825 | * |
||
826 | * @param mixed $value |
||
827 | * @param string $message |
||
828 | * |
||
829 | * @throws InvalidArgumentException |
||
830 | */ |
||
831 | 19 | public static function notFalse($value, $message = '') |
|
832 | { |
||
833 | 19 | if (false === $value) { |
|
834 | 4 | static::reportInvalidArgument( |
|
835 | 4 | $message ?: 'Expected a value other than false.' |
|
836 | ); |
||
837 | } |
||
838 | 15 | } |
|
839 | |||
840 | /** |
||
841 | * @param mixed $value |
||
842 | * @param string $message |
||
843 | * |
||
844 | * @throws InvalidArgumentException |
||
845 | */ |
||
846 | 51 | View Code Duplication | public static function ip($value, $message = '') |
847 | { |
||
848 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
|
849 | 19 | static::reportInvalidArgument(\sprintf( |
|
850 | 19 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
851 | 19 | static::valueToString($value) |
|
852 | )); |
||
853 | } |
||
854 | 32 | } |
|
855 | |||
856 | /** |
||
857 | * @param mixed $value |
||
858 | * @param string $message |
||
859 | * |
||
860 | * @throws InvalidArgumentException |
||
861 | */ |
||
862 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
863 | { |
||
864 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
865 | 35 | static::reportInvalidArgument(\sprintf( |
|
866 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
867 | 35 | static::valueToString($value) |
|
868 | )); |
||
869 | } |
||
870 | 16 | } |
|
871 | |||
872 | /** |
||
873 | * @param mixed $value |
||
874 | * @param string $message |
||
875 | * |
||
876 | * @throws InvalidArgumentException |
||
877 | */ |
||
878 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
879 | { |
||
880 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
881 | 31 | static::reportInvalidArgument(\sprintf( |
|
882 | 31 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
883 | 31 | static::valueToString($value) |
|
884 | )); |
||
885 | } |
||
886 | 20 | } |
|
887 | |||
888 | /** |
||
889 | * @param mixed $value |
||
890 | * @param string $message |
||
891 | * |
||
892 | * @throws InvalidArgumentException |
||
893 | */ |
||
894 | 20 | View Code Duplication | public static function email($value, $message = '') |
895 | { |
||
896 | 20 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
897 | 12 | static::reportInvalidArgument(\sprintf( |
|
898 | 12 | $message ?: 'Expected a value to be a valid e-mail address. Got %s', |
|
899 | 12 | static::valueToString($value) |
|
900 | )); |
||
901 | } |
||
902 | 8 | } |
|
903 | |||
904 | /** |
||
905 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
906 | * |
||
907 | * @param array $values |
||
908 | * @param string $message |
||
909 | * |
||
910 | * @throws InvalidArgumentException |
||
911 | */ |
||
912 | 12 | public static function uniqueValues(array $values, $message = '') |
|
913 | { |
||
914 | 12 | $allValues = \count($values); |
|
915 | 12 | $uniqueValues = \count(\array_unique($values)); |
|
916 | |||
917 | 12 | if ($allValues !== $uniqueValues) { |
|
918 | 8 | $difference = $allValues - $uniqueValues; |
|
919 | |||
920 | 8 | static::reportInvalidArgument(\sprintf( |
|
921 | 8 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
|
922 | 8 | $difference, |
|
923 | 8 | (1 === $difference ? 'is' : 'are') |
|
924 | )); |
||
925 | } |
||
926 | 4 | } |
|
927 | |||
928 | /** |
||
929 | * @param mixed $value |
||
930 | * @param mixed $expect |
||
931 | * @param string $message |
||
932 | * |
||
933 | * @throws InvalidArgumentException |
||
934 | */ |
||
935 | 33 | public static function eq($value, $expect, $message = '') |
|
936 | { |
||
937 | 33 | if ($expect != $value) { |
|
938 | 17 | static::reportInvalidArgument(\sprintf( |
|
939 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
940 | 17 | static::valueToString($value), |
|
941 | 17 | static::valueToString($expect) |
|
942 | )); |
||
943 | } |
||
944 | 16 | } |
|
945 | |||
946 | /** |
||
947 | * @param mixed $value |
||
948 | * @param mixed $expect |
||
949 | * @param string $message |
||
950 | * |
||
951 | * @throws InvalidArgumentException |
||
952 | */ |
||
953 | 28 | public static function notEq($value, $expect, $message = '') |
|
962 | |||
963 | /** |
||
964 | * @psalm-pure |
||
965 | * |
||
966 | * @param mixed $value |
||
967 | * @param mixed $expect |
||
968 | * @param string $message |
||
969 | * |
||
970 | * @throws InvalidArgumentException |
||
971 | */ |
||
972 | 16 | public static function same($value, $expect, $message = '') |
|
982 | |||
983 | /** |
||
984 | * @psalm-pure |
||
985 | * |
||
986 | * @param mixed $value |
||
987 | * @param mixed $expect |
||
988 | * @param string $message |
||
989 | * |
||
990 | * @throws InvalidArgumentException |
||
991 | */ |
||
992 | 16 | public static function notSame($value, $expect, $message = '') |
|
1001 | |||
1002 | /** |
||
1003 | * @psalm-pure |
||
1004 | * |
||
1005 | * @param mixed $value |
||
1006 | * @param mixed $limit |
||
1007 | * @param string $message |
||
1008 | * |
||
1009 | * @throws InvalidArgumentException |
||
1010 | */ |
||
1011 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
1021 | |||
1022 | /** |
||
1023 | * @psalm-pure |
||
1024 | * |
||
1025 | * @param mixed $value |
||
1026 | * @param mixed $limit |
||
1027 | * @param string $message |
||
1028 | * |
||
1029 | * @throws InvalidArgumentException |
||
1030 | */ |
||
1031 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
1041 | |||
1042 | /** |
||
1043 | * @psalm-pure |
||
1044 | * |
||
1045 | * @param mixed $value |
||
1046 | * @param mixed $limit |
||
1047 | * @param string $message |
||
1048 | * |
||
1049 | * @throws InvalidArgumentException |
||
1050 | */ |
||
1051 | 9 | public static function lessThan($value, $limit, $message = '') |
|
1061 | |||
1062 | /** |
||
1063 | * @psalm-pure |
||
1064 | * |
||
1065 | * @param mixed $value |
||
1066 | * @param mixed $limit |
||
1067 | * @param string $message |
||
1068 | * |
||
1069 | * @throws InvalidArgumentException |
||
1070 | */ |
||
1071 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
1081 | |||
1082 | /** |
||
1083 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
1084 | * |
||
1085 | * @psalm-pure |
||
1086 | * |
||
1087 | * @param mixed $value |
||
1088 | * @param mixed $min |
||
1089 | * @param mixed $max |
||
1090 | * @param string $message |
||
1091 | * |
||
1092 | * @throws InvalidArgumentException |
||
1093 | */ |
||
1094 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
1105 | |||
1106 | /** |
||
1107 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
1108 | * |
||
1109 | * @psalm-pure |
||
1110 | * |
||
1111 | * @param mixed $value |
||
1112 | * @param array $values |
||
1113 | * @param string $message |
||
1114 | * |
||
1115 | * @throws InvalidArgumentException |
||
1116 | */ |
||
1117 | 8 | public static function oneOf($value, array $values, $message = '') |
|
1127 | |||
1128 | /** |
||
1129 | * @psalm-pure |
||
1130 | * |
||
1131 | * @param string $value |
||
1132 | * @param string $subString |
||
1133 | * @param string $message |
||
1134 | * |
||
1135 | * @throws InvalidArgumentException |
||
1136 | */ |
||
1137 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
1147 | |||
1148 | /** |
||
1149 | * @psalm-pure |
||
1150 | * |
||
1151 | * @param string $value |
||
1152 | * @param string $subString |
||
1153 | * @param string $message |
||
1154 | * |
||
1155 | * @throws InvalidArgumentException |
||
1156 | */ |
||
1157 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1167 | |||
1168 | /** |
||
1169 | * @psalm-pure |
||
1170 | * |
||
1171 | * @param string $value |
||
1172 | * @param string $message |
||
1173 | * |
||
1174 | * @throws InvalidArgumentException |
||
1175 | */ |
||
1176 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
1185 | |||
1186 | /** |
||
1187 | * @psalm-pure |
||
1188 | * |
||
1189 | * @param string $value |
||
1190 | * @param string $prefix |
||
1191 | * @param string $message |
||
1192 | * |
||
1193 | * @throws InvalidArgumentException |
||
1194 | */ |
||
1195 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
1205 | |||
1206 | /** |
||
1207 | * @psalm-pure |
||
1208 | * |
||
1209 | * @param string $value |
||
1210 | * @param string $prefix |
||
1211 | * @param string $message |
||
1212 | * |
||
1213 | * @throws InvalidArgumentException |
||
1214 | */ |
||
1215 | 48 | View Code Duplication | public static function notStartsWith($value, $prefix, $message = '') |
1216 | { |
||
1217 | 48 | if (0 === \strpos($value, $prefix)) { |
|
1218 | 16 | static::reportInvalidArgument(\sprintf( |
|
1219 | 16 | $message ?: 'Expected a value not to start with %2$s. Got: %s', |
|
1220 | 16 | static::valueToString($value), |
|
1221 | 16 | static::valueToString($prefix) |
|
1222 | )); |
||
1223 | } |
||
1224 | 32 | } |
|
1225 | |||
1226 | /** |
||
1227 | * @psalm-pure |
||
1228 | * |
||
1229 | * @param mixed $value |
||
1230 | * @param string $message |
||
1231 | * |
||
1232 | * @throws InvalidArgumentException |
||
1233 | */ |
||
1234 | 35 | public static function startsWithLetter($value, $message = '') |
|
1235 | { |
||
1236 | 35 | static::string($value); |
|
1237 | |||
1238 | 24 | $valid = isset($value[0]); |
|
1239 | |||
1240 | 24 | if ($valid) { |
|
1241 | 20 | $locale = \setlocale(LC_CTYPE, 0); |
|
1242 | 20 | \setlocale(LC_CTYPE, 'C'); |
|
1254 | |||
1255 | /** |
||
1256 | * @psalm-pure |
||
1257 | * |
||
1258 | * @param string $value |
||
1259 | * @param string $suffix |
||
1260 | * @param string $message |
||
1261 | * |
||
1262 | * @throws InvalidArgumentException |
||
1263 | */ |
||
1264 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
1274 | |||
1275 | /** |
||
1276 | * @psalm-pure |
||
1277 | * |
||
1278 | * @param string $value |
||
1279 | * @param string $suffix |
||
1280 | * @param string $message |
||
1281 | * |
||
1282 | * @throws InvalidArgumentException |
||
1283 | */ |
||
1284 | 48 | View Code Duplication | public static function notEndsWith($value, $suffix, $message = '') |
1294 | |||
1295 | /** |
||
1296 | * @psalm-pure |
||
1297 | * |
||
1298 | * @param string $value |
||
1299 | * @param string $pattern |
||
1300 | * @param string $message |
||
1301 | * |
||
1302 | * @throws InvalidArgumentException |
||
1303 | */ |
||
1304 | 12 | public static function regex($value, $pattern, $message = '') |
|
1313 | |||
1314 | /** |
||
1315 | * @psalm-pure |
||
1316 | * |
||
1317 | * @param string $value |
||
1318 | * @param string $pattern |
||
1319 | * @param string $message |
||
1320 | * |
||
1321 | * @throws InvalidArgumentException |
||
1322 | */ |
||
1323 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
1334 | |||
1335 | /** |
||
1336 | * @psalm-pure |
||
1337 | * |
||
1338 | * @param mixed $value |
||
1339 | * @param string $message |
||
1340 | * |
||
1341 | * @throws InvalidArgumentException |
||
1342 | */ |
||
1343 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
1354 | |||
1355 | /** |
||
1356 | * @psalm-pure |
||
1357 | * |
||
1358 | * @param mixed $value |
||
1359 | * @param string $message |
||
1360 | * |
||
1361 | * @throws InvalidArgumentException |
||
1362 | */ |
||
1363 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
1379 | |||
1380 | /** |
||
1381 | * @psalm-pure |
||
1382 | * |
||
1383 | * @param string $value |
||
1384 | * @param string $message |
||
1385 | * |
||
1386 | * @throws InvalidArgumentException |
||
1387 | */ |
||
1388 | 12 | View Code Duplication | public static function digits($value, $message = '') |
1402 | |||
1403 | /** |
||
1404 | * @psalm-pure |
||
1405 | * |
||
1406 | * @param string $value |
||
1407 | * @param string $message |
||
1408 | * |
||
1409 | * @throws InvalidArgumentException |
||
1410 | */ |
||
1411 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
1425 | |||
1426 | /** |
||
1427 | * @psalm-pure |
||
1428 | * @psalm-assert lowercase-string $value |
||
1429 | * |
||
1430 | * @param string $value |
||
1431 | * @param string $message |
||
1432 | * |
||
1433 | * @throws InvalidArgumentException |
||
1434 | */ |
||
1435 | 16 | View Code Duplication | public static function lower($value, $message = '') |
1449 | |||
1450 | /** |
||
1451 | * @psalm-pure |
||
1452 | * @psalm-assert !lowercase-string $value |
||
1453 | * |
||
1454 | * @param string $value |
||
1455 | * @param string $message |
||
1456 | * |
||
1457 | * @throws InvalidArgumentException |
||
1458 | */ |
||
1459 | 16 | View Code Duplication | public static function upper($value, $message = '') |
1473 | |||
1474 | /** |
||
1475 | * @psalm-pure |
||
1476 | * |
||
1477 | * @param string $value |
||
1478 | * @param int $length |
||
1479 | * @param string $message |
||
1480 | * |
||
1481 | * @throws InvalidArgumentException |
||
1482 | */ |
||
1483 | 36 | public static function length($value, $length, $message = '') |
|
1493 | |||
1494 | /** |
||
1495 | * Inclusive min. |
||
1496 | * |
||
1497 | * @psalm-pure |
||
1498 | * |
||
1499 | * @param string $value |
||
1500 | * @param int|float $min |
||
1501 | * @param string $message |
||
1502 | * |
||
1503 | * @throws InvalidArgumentException |
||
1504 | */ |
||
1505 | 36 | public static function minLength($value, $min, $message = '') |
|
1515 | |||
1516 | /** |
||
1517 | * Inclusive max. |
||
1518 | * |
||
1519 | * @psalm-pure |
||
1520 | * |
||
1521 | * @param string $value |
||
1522 | * @param int|float $max |
||
1523 | * @param string $message |
||
1524 | * |
||
1525 | * @throws InvalidArgumentException |
||
1526 | */ |
||
1527 | 36 | public static function maxLength($value, $max, $message = '') |
|
1537 | |||
1538 | /** |
||
1539 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1540 | * |
||
1541 | * @psalm-pure |
||
1542 | * |
||
1543 | * @param string $value |
||
1544 | * @param int|float $min |
||
1545 | * @param int|float $max |
||
1546 | * @param string $message |
||
1547 | * |
||
1548 | * @throws InvalidArgumentException |
||
1549 | */ |
||
1550 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
1563 | |||
1564 | /** |
||
1565 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1566 | * |
||
1567 | * @param mixed $value |
||
1568 | * @param string $message |
||
1569 | * |
||
1570 | * @throws InvalidArgumentException |
||
1571 | */ |
||
1572 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
1583 | |||
1584 | /** |
||
1585 | * @param mixed $value |
||
1586 | * @param string $message |
||
1587 | * |
||
1588 | * @throws InvalidArgumentException |
||
1589 | */ |
||
1590 | 12 | View Code Duplication | public static function file($value, $message = '') |
1601 | |||
1602 | /** |
||
1603 | * @param mixed $value |
||
1604 | * @param string $message |
||
1605 | * |
||
1606 | * @throws InvalidArgumentException |
||
1607 | */ |
||
1608 | 12 | View Code Duplication | public static function directory($value, $message = '') |
1619 | |||
1620 | /** |
||
1621 | * @param string $value |
||
1622 | * @param string $message |
||
1623 | * |
||
1624 | * @throws InvalidArgumentException |
||
1625 | */ |
||
1626 | public static function readable($value, $message = '') |
||
1635 | |||
1636 | /** |
||
1637 | * @param string $value |
||
1638 | * @param string $message |
||
1639 | * |
||
1640 | * @throws InvalidArgumentException |
||
1641 | */ |
||
1642 | public static function writable($value, $message = '') |
||
1651 | |||
1652 | /** |
||
1653 | * @param mixed $value |
||
1654 | * @param string $message |
||
1655 | * |
||
1656 | * @throws InvalidArgumentException |
||
1657 | */ |
||
1658 | 8 | public static function classExists($value, $message = '') |
|
1667 | |||
1668 | /** |
||
1669 | * @psalm-pure |
||
1670 | * @psalm-template ExpectedType of object |
||
1671 | * @psalm-param class-string<ExpectedType> $class |
||
1672 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
1673 | * |
||
1674 | * @param mixed $value |
||
1675 | * @param string|object $class |
||
1676 | * @param string $message |
||
1677 | * |
||
1678 | * @throws InvalidArgumentException |
||
1679 | */ |
||
1680 | 8 | public static function subclassOf($value, $class, $message = '') |
|
1690 | |||
1691 | /** |
||
1692 | * @param mixed $value |
||
1693 | * @param string $message |
||
1694 | * |
||
1695 | * @throws InvalidArgumentException |
||
1696 | */ |
||
1697 | 8 | public static function interfaceExists($value, $message = '') |
|
1706 | |||
1707 | /** |
||
1708 | * @psalm-pure |
||
1709 | * @psalm-template ExpectedType of object |
||
1710 | * @psalm-param class-string<ExpectedType> $interface |
||
1711 | * @psalm-assert class-string<ExpectedType> $value |
||
1712 | * |
||
1713 | * @param mixed $value |
||
1714 | * @param mixed $interface |
||
1715 | * @param string $message |
||
1716 | * |
||
1717 | * @throws InvalidArgumentException |
||
1718 | */ |
||
1719 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
1729 | |||
1730 | /** |
||
1731 | * @psalm-pure |
||
1732 | * @psalm-param class-string|object $classOrObject |
||
1733 | * |
||
1734 | * @param string|object $classOrObject |
||
1735 | * @param mixed $property |
||
1736 | * @param string $message |
||
1737 | * |
||
1738 | * @throws InvalidArgumentException |
||
1739 | */ |
||
1740 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1749 | |||
1750 | /** |
||
1751 | * @psalm-pure |
||
1752 | * @psalm-param class-string|object $classOrObject |
||
1753 | * |
||
1754 | * @param string|object $classOrObject |
||
1755 | * @param mixed $property |
||
1756 | * @param string $message |
||
1757 | * |
||
1758 | * @throws InvalidArgumentException |
||
1759 | */ |
||
1760 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
1769 | |||
1770 | /** |
||
1771 | * @psalm-pure |
||
1772 | * @psalm-param class-string|object $classOrObject |
||
1773 | * |
||
1774 | * @param string|object $classOrObject |
||
1775 | * @param mixed $method |
||
1776 | * @param string $message |
||
1777 | * |
||
1778 | * @throws InvalidArgumentException |
||
1779 | */ |
||
1780 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1789 | |||
1790 | /** |
||
1791 | * @psalm-pure |
||
1792 | * @psalm-param class-string|object $classOrObject |
||
1793 | * |
||
1794 | * @param string|object $classOrObject |
||
1795 | * @param mixed $method |
||
1796 | * @param string $message |
||
1797 | * |
||
1798 | * @throws InvalidArgumentException |
||
1799 | */ |
||
1800 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
1809 | |||
1810 | /** |
||
1811 | * @psalm-pure |
||
1812 | * |
||
1813 | * @param array $array |
||
1814 | * @param string|int $key |
||
1815 | * @param string $message |
||
1816 | * |
||
1817 | * @throws InvalidArgumentException |
||
1818 | */ |
||
1819 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
1828 | |||
1829 | /** |
||
1830 | * @psalm-pure |
||
1831 | * |
||
1832 | * @param array $array |
||
1833 | * @param string|int $key |
||
1834 | * @param string $message |
||
1835 | * |
||
1836 | * @throws InvalidArgumentException |
||
1837 | */ |
||
1838 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1847 | |||
1848 | /** |
||
1849 | * Checks if a value is a valid array key (int or string). |
||
1850 | * |
||
1851 | * @psalm-pure |
||
1852 | * @psalm-assert array-key $value |
||
1853 | * |
||
1854 | * @param mixed $value |
||
1855 | * @param string $message |
||
1856 | * |
||
1857 | * @throws InvalidArgumentException |
||
1858 | */ |
||
1859 | 28 | public static function validArrayKey($value, $message = '') |
|
1868 | |||
1869 | /** |
||
1870 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1871 | * |
||
1872 | * @param Countable|array $array |
||
1873 | * @param int $number |
||
1874 | * @param string $message |
||
1875 | * |
||
1876 | * @throws InvalidArgumentException |
||
1877 | */ |
||
1878 | 8 | public static function count($array, $number, $message = '') |
|
1890 | |||
1891 | /** |
||
1892 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1893 | * |
||
1894 | * @param Countable|array $array |
||
1895 | * @param int|float $min |
||
1896 | * @param string $message |
||
1897 | * |
||
1898 | * @throws InvalidArgumentException |
||
1899 | */ |
||
1900 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1910 | |||
1911 | /** |
||
1912 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1913 | * |
||
1914 | * @param Countable|array $array |
||
1915 | * @param int|float $max |
||
1916 | * @param string $message |
||
1917 | * |
||
1918 | * @throws InvalidArgumentException |
||
1919 | */ |
||
1920 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1930 | |||
1931 | /** |
||
1932 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1933 | * |
||
1934 | * @param Countable|array $array |
||
1935 | * @param int|float $min |
||
1936 | * @param int|float $max |
||
1937 | * @param string $message |
||
1938 | * |
||
1939 | * @throws InvalidArgumentException |
||
1940 | */ |
||
1941 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
1954 | |||
1955 | /** |
||
1956 | * @psalm-pure |
||
1957 | * @psalm-assert list $array |
||
1958 | * |
||
1959 | * @param mixed $array |
||
1960 | * @param string $message |
||
1961 | * |
||
1962 | * @throws InvalidArgumentException |
||
1963 | */ |
||
1964 | 80 | View Code Duplication | public static function isList($array, $message = '') |
1972 | |||
1973 | /** |
||
1974 | * @psalm-pure |
||
1975 | * @psalm-assert list $array |
||
1976 | * @psalm-assert !empty $array |
||
1977 | * |
||
1978 | * @param mixed $array |
||
1979 | * @param string $message |
||
1980 | * |
||
1981 | * @throws InvalidArgumentException |
||
1982 | */ |
||
1983 | 40 | public static function isNonEmptyList($array, $message = '') |
|
1988 | |||
1989 | /** |
||
1990 | * @psalm-pure |
||
1991 | * @psalm-template T |
||
1992 | * @psalm-param mixed|array<T> $array |
||
1993 | * @psalm-assert array<string, T> $array |
||
1994 | * |
||
1995 | * @param mixed $array |
||
1996 | * @param string $message |
||
1997 | * |
||
1998 | * @throws InvalidArgumentException |
||
1999 | */ |
||
2000 | 32 | public static function isMap($array, $message = '') |
|
2011 | |||
2012 | /** |
||
2013 | * @psalm-pure |
||
2014 | * @psalm-template T |
||
2015 | * @psalm-param mixed|array<T> $array |
||
2016 | * @psalm-assert array<string, T> $array |
||
2017 | * @psalm-assert !empty $array |
||
2018 | * |
||
2019 | * @param mixed $array |
||
2020 | * @param string $message |
||
2021 | * |
||
2022 | * @throws InvalidArgumentException |
||
2023 | */ |
||
2024 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
2029 | |||
2030 | /** |
||
2031 | * @psalm-pure |
||
2032 | * |
||
2033 | * @param string $value |
||
2034 | * @param string $message |
||
2035 | * |
||
2036 | * @throws InvalidArgumentException |
||
2037 | */ |
||
2038 | 56 | public static function uuid($value, $message = '') |
|
2055 | |||
2056 | /** |
||
2057 | * @psalm-param class-string<Throwable> |
||
2058 | * |
||
2059 | * @param Closure $expression |
||
2060 | * @param string $class |
||
2061 | * @param string $message |
||
2062 | * |
||
2063 | * @throws InvalidArgumentException |
||
2064 | */ |
||
2065 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
2091 | |||
2092 | /** |
||
2093 | * @throws BadMethodCallException |
||
2094 | */ |
||
2095 | 1635 | public static function __callStatic($name, $arguments) |
|
2123 | |||
2124 | /** |
||
2125 | * @param mixed $value |
||
2126 | * |
||
2127 | * @return string |
||
2128 | */ |
||
2129 | 747 | protected static function valueToString($value) |
|
2169 | |||
2170 | /** |
||
2171 | * @param mixed $value |
||
2172 | * |
||
2173 | * @return string |
||
2174 | */ |
||
2175 | 251 | protected static function typeToString($value) |
|
2179 | |||
2180 | 168 | protected static function strlen($value) |
|
2192 | |||
2193 | /** |
||
2194 | * @param string $message |
||
2195 | * |
||
2196 | * @throws InvalidArgumentException |
||
2197 | * |
||
2198 | * @psalm-pure this method is not supposed to perform side-effects |
||
2199 | */ |
||
2200 | 1061 | protected static function reportInvalidArgument($message) |
|
2204 | |||
2205 | private function __construct() |
||
2208 | } |
||
2209 |