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 |
||
213 | class Assert |
||
|
|||
214 | { |
||
215 | /** |
||
216 | * @psalm-pure |
||
217 | * @psalm-assert string $value |
||
218 | * |
||
219 | * @param mixed $value |
||
220 | * @param string $message |
||
221 | * |
||
222 | * @throws InvalidArgumentException |
||
223 | */ |
||
224 | 241 | public static function string($value, $message = '') |
|
225 | { |
||
226 | 241 | if (!\is_string($value)) { |
|
227 | 45 | static::reportInvalidArgument(\sprintf( |
|
228 | 45 | $message ?: 'Expected a string. Got: %s', |
|
229 | 45 | static::typeToString($value) |
|
230 | )); |
||
231 | } |
||
232 | 196 | } |
|
233 | |||
234 | /** |
||
235 | * @psalm-pure |
||
236 | * @psalm-assert string $value |
||
237 | * @psalm-assert !empty $value |
||
238 | * |
||
239 | * @param mixed $value |
||
240 | * @param string $message |
||
241 | * |
||
242 | * @throws InvalidArgumentException |
||
243 | */ |
||
244 | 16 | public static function stringNotEmpty($value, $message = '') |
|
245 | { |
||
246 | 16 | static::string($value, $message); |
|
247 | 12 | static::notEq($value, '', $message); |
|
248 | 8 | } |
|
249 | |||
250 | /** |
||
251 | * @psalm-pure |
||
252 | * @psalm-assert int $value |
||
253 | * |
||
254 | * @param mixed $value |
||
255 | * @param string $message |
||
256 | * |
||
257 | * @throws InvalidArgumentException |
||
258 | */ |
||
259 | 17 | public static function integer($value, $message = '') |
|
260 | { |
||
261 | 17 | if (!\is_int($value)) { |
|
262 | 13 | static::reportInvalidArgument(\sprintf( |
|
263 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
264 | 13 | static::typeToString($value) |
|
265 | )); |
||
266 | } |
||
267 | 4 | } |
|
268 | |||
269 | /** |
||
270 | * @psalm-pure |
||
271 | * @psalm-assert numeric $value |
||
272 | * |
||
273 | * @param mixed $value |
||
274 | * @param string $message |
||
275 | * |
||
276 | * @throws InvalidArgumentException |
||
277 | */ |
||
278 | 16 | public static function integerish($value, $message = '') |
|
279 | { |
||
280 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
281 | 4 | static::reportInvalidArgument(\sprintf( |
|
282 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
283 | 4 | static::typeToString($value) |
|
284 | )); |
||
285 | } |
||
286 | 12 | } |
|
287 | |||
288 | /** |
||
289 | * @psalm-pure |
||
290 | * @psalm-assert float $value |
||
291 | * |
||
292 | * @param mixed $value |
||
293 | * @param string $message |
||
294 | * |
||
295 | * @throws InvalidArgumentException |
||
296 | */ |
||
297 | 16 | public static function float($value, $message = '') |
|
298 | { |
||
299 | 16 | if (!\is_float($value)) { |
|
300 | 8 | static::reportInvalidArgument(\sprintf( |
|
301 | 8 | $message ?: 'Expected a float. Got: %s', |
|
302 | 8 | static::typeToString($value) |
|
303 | )); |
||
304 | } |
||
305 | 8 | } |
|
306 | |||
307 | /** |
||
308 | * @psalm-pure |
||
309 | * @psalm-assert numeric $value |
||
310 | * |
||
311 | * @param mixed $value |
||
312 | * @param string $message |
||
313 | * |
||
314 | * @throws InvalidArgumentException |
||
315 | */ |
||
316 | 20 | public static function numeric($value, $message = '') |
|
317 | { |
||
318 | 20 | if (!\is_numeric($value)) { |
|
319 | 4 | static::reportInvalidArgument(\sprintf( |
|
320 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
321 | 4 | static::typeToString($value) |
|
322 | )); |
||
323 | } |
||
324 | 16 | } |
|
325 | |||
326 | /** |
||
327 | * @psalm-pure |
||
328 | * @psalm-assert int $value |
||
329 | * |
||
330 | * @param mixed $value |
||
331 | * @param string $message |
||
332 | * |
||
333 | * @throws InvalidArgumentException |
||
334 | */ |
||
335 | 24 | public static function natural($value, $message = '') |
|
336 | { |
||
337 | 24 | if (!\is_int($value) || $value < 0) { |
|
338 | 16 | static::reportInvalidArgument(\sprintf( |
|
339 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
340 | 16 | static::valueToString($value) |
|
341 | )); |
||
342 | } |
||
343 | 8 | } |
|
344 | |||
345 | /** |
||
346 | * @psalm-pure |
||
347 | * @psalm-assert bool $value |
||
348 | * |
||
349 | * @param mixed $value |
||
350 | * @param string $message |
||
351 | * |
||
352 | * @throws InvalidArgumentException |
||
353 | */ |
||
354 | 16 | public static function boolean($value, $message = '') |
|
355 | { |
||
356 | 16 | if (!\is_bool($value)) { |
|
357 | 8 | static::reportInvalidArgument(\sprintf( |
|
358 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
359 | 8 | static::typeToString($value) |
|
360 | )); |
||
361 | } |
||
362 | 8 | } |
|
363 | |||
364 | /** |
||
365 | * @psalm-pure |
||
366 | * @psalm-assert scalar $value |
||
367 | * |
||
368 | * @param mixed $value |
||
369 | * @param string $message |
||
370 | * |
||
371 | * @throws InvalidArgumentException |
||
372 | */ |
||
373 | 23 | public static function scalar($value, $message = '') |
|
374 | { |
||
375 | 23 | if (!\is_scalar($value)) { |
|
376 | 11 | static::reportInvalidArgument(\sprintf( |
|
377 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
378 | 11 | static::typeToString($value) |
|
379 | )); |
||
380 | } |
||
381 | 12 | } |
|
382 | |||
383 | /** |
||
384 | * @psalm-pure |
||
385 | * @psalm-assert object $value |
||
386 | * |
||
387 | * @param mixed $value |
||
388 | * @param string $message |
||
389 | * |
||
390 | * @throws InvalidArgumentException |
||
391 | */ |
||
392 | 23 | public static function object($value, $message = '') |
|
393 | { |
||
394 | 23 | if (!\is_object($value)) { |
|
395 | 15 | static::reportInvalidArgument(\sprintf( |
|
396 | 15 | $message ?: 'Expected an object. Got: %s', |
|
397 | 15 | static::typeToString($value) |
|
398 | )); |
||
399 | } |
||
400 | 8 | } |
|
401 | |||
402 | /** |
||
403 | * @psalm-pure |
||
404 | * @psalm-assert resource $value |
||
405 | * |
||
406 | * @param mixed $value |
||
407 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
408 | * @param string $message |
||
409 | * |
||
410 | * @throws InvalidArgumentException |
||
411 | */ |
||
412 | 16 | public static function resource($value, $type = null, $message = '') |
|
413 | { |
||
414 | 16 | if (!\is_resource($value)) { |
|
415 | 4 | static::reportInvalidArgument(\sprintf( |
|
416 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
417 | 4 | static::typeToString($value) |
|
418 | )); |
||
419 | } |
||
420 | |||
421 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
422 | 4 | static::reportInvalidArgument(\sprintf( |
|
423 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
424 | 4 | static::typeToString($value), |
|
425 | 4 | $type |
|
426 | )); |
||
427 | } |
||
428 | 8 | } |
|
429 | |||
430 | /** |
||
431 | * @psalm-pure |
||
432 | * @psalm-assert callable $value |
||
433 | * |
||
434 | * @param mixed $value |
||
435 | * @param string $message |
||
436 | * |
||
437 | * @throws InvalidArgumentException |
||
438 | */ |
||
439 | 20 | public static function isCallable($value, $message = '') |
|
440 | { |
||
441 | 20 | if (!\is_callable($value)) { |
|
442 | 8 | static::reportInvalidArgument(\sprintf( |
|
443 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
444 | 8 | static::typeToString($value) |
|
445 | )); |
||
446 | } |
||
447 | 12 | } |
|
448 | |||
449 | /** |
||
450 | * @psalm-pure |
||
451 | * @psalm-assert array $value |
||
452 | * |
||
453 | * @param mixed $value |
||
454 | * @param string $message |
||
455 | * |
||
456 | * @throws InvalidArgumentException |
||
457 | */ |
||
458 | 20 | public static function isArray($value, $message = '') |
|
459 | { |
||
460 | 20 | if (!\is_array($value)) { |
|
461 | 12 | static::reportInvalidArgument(\sprintf( |
|
462 | 12 | $message ?: 'Expected an array. Got: %s', |
|
463 | 12 | static::typeToString($value) |
|
464 | )); |
||
465 | } |
||
466 | 8 | } |
|
467 | |||
468 | /** |
||
469 | * @psalm-pure |
||
470 | * @psalm-assert iterable $value |
||
471 | * |
||
472 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
473 | * |
||
474 | * @param mixed $value |
||
475 | * @param string $message |
||
476 | * |
||
477 | * @throws InvalidArgumentException |
||
478 | */ |
||
479 | 20 | public static function isTraversable($value, $message = '') |
|
480 | { |
||
481 | 20 | @\trigger_error( |
|
482 | 20 | \sprintf( |
|
483 | 20 | 'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.', |
|
484 | 20 | __METHOD__ |
|
485 | ), |
||
486 | 20 | \E_USER_DEPRECATED |
|
487 | ); |
||
488 | |||
489 | 20 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
490 | 8 | static::reportInvalidArgument(\sprintf( |
|
491 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
492 | 8 | static::typeToString($value) |
|
493 | )); |
||
494 | } |
||
495 | 12 | } |
|
496 | |||
497 | /** |
||
498 | * @psalm-pure |
||
499 | * @psalm-assert array|ArrayAccess $value |
||
500 | * |
||
501 | * @param mixed $value |
||
502 | * @param string $message |
||
503 | * |
||
504 | * @throws InvalidArgumentException |
||
505 | */ |
||
506 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
507 | { |
||
508 | 20 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
|
509 | 8 | static::reportInvalidArgument(\sprintf( |
|
510 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
511 | 8 | static::typeToString($value) |
|
512 | )); |
||
513 | } |
||
514 | 12 | } |
|
515 | |||
516 | /** |
||
517 | * @psalm-pure |
||
518 | * @psalm-assert countable $value |
||
519 | * |
||
520 | * @param mixed $value |
||
521 | * @param string $message |
||
522 | * |
||
523 | * @throws InvalidArgumentException |
||
524 | */ |
||
525 | 28 | public static function isCountable($value, $message = '') |
|
526 | { |
||
527 | if ( |
||
528 | 28 | !\is_array($value) |
|
529 | 28 | && !($value instanceof Countable) |
|
530 | 28 | && !($value instanceof ResourceBundle) |
|
531 | 28 | && !($value instanceof SimpleXMLElement) |
|
532 | ) { |
||
533 | 12 | static::reportInvalidArgument(\sprintf( |
|
534 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
535 | 12 | static::typeToString($value) |
|
536 | )); |
||
537 | } |
||
538 | 16 | } |
|
539 | |||
540 | /** |
||
541 | * @psalm-pure |
||
542 | * @psalm-assert iterable $value |
||
543 | * |
||
544 | * @param mixed $value |
||
545 | * @param string $message |
||
546 | * |
||
547 | * @throws InvalidArgumentException |
||
548 | */ |
||
549 | 1040 | View Code Duplication | public static function isIterable($value, $message = '') |
550 | { |
||
551 | 1040 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
552 | 8 | static::reportInvalidArgument(\sprintf( |
|
553 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
554 | 8 | static::typeToString($value) |
|
555 | )); |
||
556 | } |
||
557 | 1036 | } |
|
558 | |||
559 | /** |
||
560 | * @psalm-pure |
||
561 | * @psalm-template ExpectedType of object |
||
562 | * @psalm-param class-string<ExpectedType> $class |
||
563 | * @psalm-assert ExpectedType $value |
||
564 | * |
||
565 | * @param mixed $value |
||
566 | * @param string|object $class |
||
567 | * @param string $message |
||
568 | * |
||
569 | * @throws InvalidArgumentException |
||
570 | */ |
||
571 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
572 | { |
||
573 | 19 | if (!($value instanceof $class)) { |
|
574 | 15 | static::reportInvalidArgument(\sprintf( |
|
575 | 15 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
576 | 15 | static::typeToString($value), |
|
577 | 15 | $class |
|
578 | )); |
||
579 | } |
||
580 | 4 | } |
|
581 | |||
582 | /** |
||
583 | * @psalm-pure |
||
584 | * @psalm-template ExpectedType of object |
||
585 | * @psalm-param class-string<ExpectedType> $class |
||
586 | * @psalm-assert !ExpectedType $value |
||
587 | * |
||
588 | * @param mixed $value |
||
589 | * @param string|object $class |
||
590 | * @param string $message |
||
591 | * |
||
592 | * @throws InvalidArgumentException |
||
593 | */ |
||
594 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
595 | { |
||
596 | 16 | if ($value instanceof $class) { |
|
597 | 4 | static::reportInvalidArgument(\sprintf( |
|
598 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
599 | 4 | static::typeToString($value), |
|
600 | 4 | $class |
|
601 | )); |
||
602 | } |
||
603 | 12 | } |
|
604 | |||
605 | /** |
||
606 | * @psalm-pure |
||
607 | * @psalm-param array<class-string> $classes |
||
608 | * |
||
609 | * @param mixed $value |
||
610 | * @param array<object|string> $classes |
||
611 | * @param string $message |
||
612 | * |
||
613 | * @throws InvalidArgumentException |
||
614 | */ |
||
615 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
616 | { |
||
617 | 20 | foreach ($classes as $class) { |
|
618 | 20 | if ($value instanceof $class) { |
|
619 | 8 | return; |
|
620 | } |
||
621 | } |
||
622 | |||
623 | 12 | static::reportInvalidArgument(\sprintf( |
|
624 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
625 | 12 | static::typeToString($value), |
|
626 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
627 | )); |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * @psalm-pure |
||
632 | * @psalm-template ExpectedType of object |
||
633 | * @psalm-param class-string<ExpectedType> $class |
||
634 | * @psalm-assert ExpectedType|class-string<ExpectedType> $value |
||
635 | * |
||
636 | * @param object|string $value |
||
637 | * @param string $class |
||
638 | * @param string $message |
||
639 | * |
||
640 | * @throws InvalidArgumentException |
||
641 | */ |
||
642 | 20 | View Code Duplication | public static function isAOf($value, $class, $message = '') |
643 | { |
||
644 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
645 | |||
646 | 16 | if (!\is_a($value, $class, \is_string($value))) { |
|
647 | 12 | static::reportInvalidArgument(sprintf( |
|
648 | 12 | $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s', |
|
649 | 12 | static::typeToString($value), |
|
650 | $class |
||
651 | )); |
||
652 | } |
||
653 | 4 | } |
|
654 | |||
655 | /** |
||
656 | * @psalm-pure |
||
657 | * @psalm-template UnexpectedType of object |
||
658 | * @psalm-param class-string<UnexpectedType> $class |
||
659 | * @psalm-assert !UnexpectedType $value |
||
660 | * @psalm-assert !class-string<UnexpectedType> $value |
||
661 | * |
||
662 | * @param object|string $value |
||
663 | * @param string $class |
||
664 | * @param string $message |
||
665 | * |
||
666 | * @throws InvalidArgumentException |
||
667 | */ |
||
668 | 20 | View Code Duplication | public static function isNotA($value, $class, $message = '') |
669 | { |
||
670 | 20 | static::string($class, 'Expected class as a string. Got: %s'); |
|
671 | |||
672 | 16 | if (\is_a($value, $class, \is_string($value))) { |
|
673 | 4 | static::reportInvalidArgument(sprintf( |
|
674 | 4 | $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
675 | 4 | static::typeToString($value), |
|
676 | $class |
||
677 | )); |
||
678 | } |
||
679 | 12 | } |
|
680 | |||
681 | /** |
||
682 | * @psalm-pure |
||
683 | * @psalm-param array<class-string> $classes |
||
684 | * |
||
685 | * @param object|string $value |
||
686 | * @param string[] $classes |
||
687 | * @param string $message |
||
688 | * |
||
689 | * @throws InvalidArgumentException |
||
690 | */ |
||
691 | 24 | public static function isAnyOf($value, array $classes, $message = '') |
|
692 | { |
||
693 | 24 | foreach ($classes as $class) { |
|
694 | 24 | static::string($class, 'Expected class as a string. Got: %s'); |
|
695 | |||
696 | 20 | if (\is_a($value, $class, \is_string($value))) { |
|
697 | 8 | return; |
|
698 | } |
||
699 | } |
||
700 | |||
701 | 12 | static::reportInvalidArgument(sprintf( |
|
702 | 12 | $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s', |
|
703 | 12 | static::typeToString($value), |
|
704 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
705 | )); |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * @psalm-pure |
||
710 | * @psalm-assert empty $value |
||
711 | * |
||
712 | * @param mixed $value |
||
713 | * @param string $message |
||
714 | * |
||
715 | * @throws InvalidArgumentException |
||
716 | */ |
||
717 | 23 | public static function isEmpty($value, $message = '') |
|
718 | { |
||
719 | 23 | if (!empty($value)) { |
|
720 | 8 | static::reportInvalidArgument(\sprintf( |
|
721 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
722 | 8 | static::valueToString($value) |
|
723 | )); |
||
724 | } |
||
725 | 15 | } |
|
726 | |||
727 | /** |
||
728 | * @psalm-pure |
||
729 | * @psalm-assert !empty $value |
||
730 | * |
||
731 | * @param mixed $value |
||
732 | * @param string $message |
||
733 | * |
||
734 | * @throws InvalidArgumentException |
||
735 | */ |
||
736 | 55 | public static function notEmpty($value, $message = '') |
|
737 | { |
||
738 | 55 | if (empty($value)) { |
|
739 | 23 | static::reportInvalidArgument(\sprintf( |
|
740 | 23 | $message ?: 'Expected a non-empty value. Got: %s', |
|
741 | 23 | static::valueToString($value) |
|
742 | )); |
||
743 | } |
||
744 | 32 | } |
|
745 | |||
746 | /** |
||
747 | * @psalm-pure |
||
748 | * @psalm-assert null $value |
||
749 | * |
||
750 | * @param mixed $value |
||
751 | * @param string $message |
||
752 | * |
||
753 | * @throws InvalidArgumentException |
||
754 | */ |
||
755 | 11 | public static function null($value, $message = '') |
|
756 | { |
||
757 | 11 | if (null !== $value) { |
|
758 | 8 | static::reportInvalidArgument(\sprintf( |
|
759 | 8 | $message ?: 'Expected null. Got: %s', |
|
760 | 8 | static::valueToString($value) |
|
761 | )); |
||
762 | } |
||
763 | 3 | } |
|
764 | |||
765 | /** |
||
766 | * @psalm-pure |
||
767 | * @psalm-assert !null $value |
||
768 | * |
||
769 | * @param mixed $value |
||
770 | * @param string $message |
||
771 | * |
||
772 | * @throws InvalidArgumentException |
||
773 | */ |
||
774 | 11 | public static function notNull($value, $message = '') |
|
775 | { |
||
776 | 11 | if (null === $value) { |
|
777 | 3 | static::reportInvalidArgument( |
|
778 | 3 | $message ?: 'Expected a value other than null.' |
|
779 | ); |
||
780 | } |
||
781 | 8 | } |
|
782 | |||
783 | /** |
||
784 | * @psalm-pure |
||
785 | * @psalm-assert true $value |
||
786 | * |
||
787 | * @param mixed $value |
||
788 | * @param string $message |
||
789 | * |
||
790 | * @throws InvalidArgumentException |
||
791 | */ |
||
792 | 15 | public static function true($value, $message = '') |
|
793 | { |
||
794 | 15 | if (true !== $value) { |
|
795 | 11 | static::reportInvalidArgument(\sprintf( |
|
796 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
797 | 11 | static::valueToString($value) |
|
798 | )); |
||
799 | } |
||
800 | 4 | } |
|
801 | |||
802 | /** |
||
803 | * @psalm-pure |
||
804 | * @psalm-assert false $value |
||
805 | * |
||
806 | * @param mixed $value |
||
807 | * @param string $message |
||
808 | * |
||
809 | * @throws InvalidArgumentException |
||
810 | */ |
||
811 | 19 | public static function false($value, $message = '') |
|
812 | { |
||
813 | 19 | if (false !== $value) { |
|
814 | 15 | static::reportInvalidArgument(\sprintf( |
|
815 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
816 | 15 | static::valueToString($value) |
|
817 | )); |
||
818 | } |
||
819 | 4 | } |
|
820 | |||
821 | /** |
||
822 | * @psalm-assert !false $value |
||
823 | * |
||
824 | * @param mixed $value |
||
825 | * @param string $message |
||
826 | * |
||
827 | * @throws InvalidArgumentException |
||
828 | */ |
||
829 | 19 | public static function notFalse($value, $message = '') |
|
830 | { |
||
831 | 19 | if (false === $value) { |
|
832 | 4 | static::reportInvalidArgument( |
|
833 | 4 | $message ?: 'Expected a value other than false.' |
|
834 | ); |
||
835 | } |
||
836 | 15 | } |
|
837 | |||
838 | /** |
||
839 | * @param mixed $value |
||
840 | * @param string $message |
||
841 | * |
||
842 | * @throws InvalidArgumentException |
||
843 | */ |
||
844 | 51 | View Code Duplication | public static function ip($value, $message = '') |
845 | { |
||
846 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
|
847 | 19 | static::reportInvalidArgument(\sprintf( |
|
848 | 19 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
849 | 19 | static::valueToString($value) |
|
850 | )); |
||
851 | } |
||
852 | 32 | } |
|
853 | |||
854 | /** |
||
855 | * @param mixed $value |
||
856 | * @param string $message |
||
857 | * |
||
858 | * @throws InvalidArgumentException |
||
859 | */ |
||
860 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
861 | { |
||
862 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
863 | 35 | static::reportInvalidArgument(\sprintf( |
|
864 | 35 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
865 | 35 | static::valueToString($value) |
|
866 | )); |
||
867 | } |
||
868 | 16 | } |
|
869 | |||
870 | /** |
||
871 | * @param mixed $value |
||
872 | * @param string $message |
||
873 | * |
||
874 | * @throws InvalidArgumentException |
||
875 | */ |
||
876 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
877 | { |
||
878 | 51 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
879 | 31 | static::reportInvalidArgument(\sprintf( |
|
880 | 31 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
881 | 31 | static::valueToString($value) |
|
882 | )); |
||
883 | } |
||
884 | 20 | } |
|
885 | |||
886 | /** |
||
887 | * @param mixed $value |
||
888 | * @param string $message |
||
889 | * |
||
890 | * @throws InvalidArgumentException |
||
891 | */ |
||
892 | 20 | View Code Duplication | public static function email($value, $message = '') |
893 | { |
||
894 | 20 | if (false === \filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
895 | 12 | static::reportInvalidArgument(\sprintf( |
|
896 | 12 | $message ?: 'Expected a value to be a valid e-mail address. Got %s', |
|
897 | 12 | static::valueToString($value) |
|
898 | )); |
||
899 | } |
||
900 | 8 | } |
|
901 | |||
902 | /** |
||
903 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
904 | * |
||
905 | * @param array $values |
||
906 | * @param string $message |
||
907 | * |
||
908 | * @throws InvalidArgumentException |
||
909 | */ |
||
910 | 12 | public static function uniqueValues(array $values, $message = '') |
|
911 | { |
||
912 | 12 | $allValues = \count($values); |
|
913 | 12 | $uniqueValues = \count(\array_unique($values)); |
|
914 | |||
915 | 12 | if ($allValues !== $uniqueValues) { |
|
916 | 8 | $difference = $allValues - $uniqueValues; |
|
917 | |||
918 | 8 | static::reportInvalidArgument(\sprintf( |
|
919 | 8 | $message ?: 'Expected an array of unique values, but %s of them %s duplicated', |
|
920 | 8 | $difference, |
|
921 | 8 | (1 === $difference ? 'is' : 'are') |
|
922 | )); |
||
923 | } |
||
924 | 4 | } |
|
925 | |||
926 | /** |
||
927 | * @param mixed $value |
||
928 | * @param mixed $expect |
||
929 | * @param string $message |
||
930 | * |
||
931 | * @throws InvalidArgumentException |
||
932 | */ |
||
933 | 33 | public static function eq($value, $expect, $message = '') |
|
943 | |||
944 | /** |
||
945 | * @param mixed $value |
||
946 | * @param mixed $expect |
||
947 | * @param string $message |
||
948 | * |
||
949 | * @throws InvalidArgumentException |
||
950 | */ |
||
951 | 28 | public static function notEq($value, $expect, $message = '') |
|
952 | { |
||
953 | 28 | if ($expect == $value) { |
|
954 | 16 | static::reportInvalidArgument(\sprintf( |
|
955 | 16 | $message ?: 'Expected a different value than %s.', |
|
956 | 16 | static::valueToString($expect) |
|
957 | )); |
||
958 | } |
||
959 | 12 | } |
|
960 | |||
961 | /** |
||
962 | * @psalm-pure |
||
963 | * |
||
964 | * @param mixed $value |
||
965 | * @param mixed $expect |
||
966 | * @param string $message |
||
967 | * |
||
968 | * @throws InvalidArgumentException |
||
969 | */ |
||
970 | 16 | public static function same($value, $expect, $message = '') |
|
971 | { |
||
972 | 16 | if ($expect !== $value) { |
|
973 | 12 | static::reportInvalidArgument(\sprintf( |
|
974 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
975 | 12 | static::valueToString($value), |
|
976 | 12 | static::valueToString($expect) |
|
977 | )); |
||
978 | } |
||
979 | 4 | } |
|
980 | |||
981 | /** |
||
982 | * @psalm-pure |
||
983 | * |
||
984 | * @param mixed $value |
||
985 | * @param mixed $expect |
||
986 | * @param string $message |
||
987 | * |
||
988 | * @throws InvalidArgumentException |
||
989 | */ |
||
990 | 16 | public static function notSame($value, $expect, $message = '') |
|
999 | |||
1000 | /** |
||
1001 | * @psalm-pure |
||
1002 | * |
||
1003 | * @param mixed $value |
||
1004 | * @param mixed $limit |
||
1005 | * @param string $message |
||
1006 | * |
||
1007 | * @throws InvalidArgumentException |
||
1008 | */ |
||
1009 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
1019 | |||
1020 | /** |
||
1021 | * @psalm-pure |
||
1022 | * |
||
1023 | * @param mixed $value |
||
1024 | * @param mixed $limit |
||
1025 | * @param string $message |
||
1026 | * |
||
1027 | * @throws InvalidArgumentException |
||
1028 | */ |
||
1029 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
1039 | |||
1040 | /** |
||
1041 | * @psalm-pure |
||
1042 | * |
||
1043 | * @param mixed $value |
||
1044 | * @param mixed $limit |
||
1045 | * @param string $message |
||
1046 | * |
||
1047 | * @throws InvalidArgumentException |
||
1048 | */ |
||
1049 | 8 | public static function lessThan($value, $limit, $message = '') |
|
1059 | |||
1060 | /** |
||
1061 | * @psalm-pure |
||
1062 | * |
||
1063 | * @param mixed $value |
||
1064 | * @param mixed $limit |
||
1065 | * @param string $message |
||
1066 | * |
||
1067 | * @throws InvalidArgumentException |
||
1068 | */ |
||
1069 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
1079 | |||
1080 | /** |
||
1081 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
1082 | * |
||
1083 | * @psalm-pure |
||
1084 | * |
||
1085 | * @param mixed $value |
||
1086 | * @param mixed $min |
||
1087 | * @param mixed $max |
||
1088 | * @param string $message |
||
1089 | * |
||
1090 | * @throws InvalidArgumentException |
||
1091 | */ |
||
1092 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
1103 | |||
1104 | /** |
||
1105 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
1106 | * |
||
1107 | * @psalm-pure |
||
1108 | * |
||
1109 | * @param mixed $value |
||
1110 | * @param array $values |
||
1111 | * @param string $message |
||
1112 | * |
||
1113 | * @throws InvalidArgumentException |
||
1114 | */ |
||
1115 | 8 | public static function oneOf($value, array $values, $message = '') |
|
1125 | |||
1126 | /** |
||
1127 | * @psalm-pure |
||
1128 | * |
||
1129 | * @param string $value |
||
1130 | * @param string $subString |
||
1131 | * @param string $message |
||
1132 | * |
||
1133 | * @throws InvalidArgumentException |
||
1134 | */ |
||
1135 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
1145 | |||
1146 | /** |
||
1147 | * @psalm-pure |
||
1148 | * |
||
1149 | * @param string $value |
||
1150 | * @param string $subString |
||
1151 | * @param string $message |
||
1152 | * |
||
1153 | * @throws InvalidArgumentException |
||
1154 | */ |
||
1155 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1165 | |||
1166 | /** |
||
1167 | * @psalm-pure |
||
1168 | * |
||
1169 | * @param string $value |
||
1170 | * @param string $message |
||
1171 | * |
||
1172 | * @throws InvalidArgumentException |
||
1173 | */ |
||
1174 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
1183 | |||
1184 | /** |
||
1185 | * @psalm-pure |
||
1186 | * |
||
1187 | * @param string $value |
||
1188 | * @param string $prefix |
||
1189 | * @param string $message |
||
1190 | * |
||
1191 | * @throws InvalidArgumentException |
||
1192 | */ |
||
1193 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
1203 | |||
1204 | /** |
||
1205 | * @psalm-pure |
||
1206 | * |
||
1207 | * @param string $value |
||
1208 | * @param string $prefix |
||
1209 | * @param string $message |
||
1210 | * |
||
1211 | * @throws InvalidArgumentException |
||
1212 | */ |
||
1213 | 48 | View Code Duplication | public static function notStartsWith($value, $prefix, $message = '') |
1223 | |||
1224 | /** |
||
1225 | * @psalm-pure |
||
1226 | * |
||
1227 | * @param mixed $value |
||
1228 | * @param string $message |
||
1229 | * |
||
1230 | * @throws InvalidArgumentException |
||
1231 | */ |
||
1232 | 35 | public static function startsWithLetter($value, $message = '') |
|
1252 | |||
1253 | /** |
||
1254 | * @psalm-pure |
||
1255 | * |
||
1256 | * @param string $value |
||
1257 | * @param string $suffix |
||
1258 | * @param string $message |
||
1259 | * |
||
1260 | * @throws InvalidArgumentException |
||
1261 | */ |
||
1262 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
1272 | |||
1273 | /** |
||
1274 | * @psalm-pure |
||
1275 | * |
||
1276 | * @param string $value |
||
1277 | * @param string $suffix |
||
1278 | * @param string $message |
||
1279 | * |
||
1280 | * @throws InvalidArgumentException |
||
1281 | */ |
||
1282 | 48 | View Code Duplication | public static function notEndsWith($value, $suffix, $message = '') |
1292 | |||
1293 | /** |
||
1294 | * @psalm-pure |
||
1295 | * |
||
1296 | * @param string $value |
||
1297 | * @param string $pattern |
||
1298 | * @param string $message |
||
1299 | * |
||
1300 | * @throws InvalidArgumentException |
||
1301 | */ |
||
1302 | 12 | public static function regex($value, $pattern, $message = '') |
|
1311 | |||
1312 | /** |
||
1313 | * @psalm-pure |
||
1314 | * |
||
1315 | * @param string $value |
||
1316 | * @param string $pattern |
||
1317 | * @param string $message |
||
1318 | * |
||
1319 | * @throws InvalidArgumentException |
||
1320 | */ |
||
1321 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
1332 | |||
1333 | /** |
||
1334 | * @psalm-pure |
||
1335 | * |
||
1336 | * @param mixed $value |
||
1337 | * @param string $message |
||
1338 | * |
||
1339 | * @throws InvalidArgumentException |
||
1340 | */ |
||
1341 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
1352 | |||
1353 | /** |
||
1354 | * @psalm-pure |
||
1355 | * |
||
1356 | * @param mixed $value |
||
1357 | * @param string $message |
||
1358 | * |
||
1359 | * @throws InvalidArgumentException |
||
1360 | */ |
||
1361 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
1377 | |||
1378 | /** |
||
1379 | * @psalm-pure |
||
1380 | * |
||
1381 | * @param string $value |
||
1382 | * @param string $message |
||
1383 | * |
||
1384 | * @throws InvalidArgumentException |
||
1385 | */ |
||
1386 | 12 | View Code Duplication | public static function digits($value, $message = '') |
1400 | |||
1401 | /** |
||
1402 | * @psalm-pure |
||
1403 | * |
||
1404 | * @param string $value |
||
1405 | * @param string $message |
||
1406 | * |
||
1407 | * @throws InvalidArgumentException |
||
1408 | */ |
||
1409 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
1423 | |||
1424 | /** |
||
1425 | * @psalm-pure |
||
1426 | * @psalm-assert lowercase-string $value |
||
1427 | * |
||
1428 | * @param string $value |
||
1429 | * @param string $message |
||
1430 | * |
||
1431 | * @throws InvalidArgumentException |
||
1432 | */ |
||
1433 | 16 | View Code Duplication | public static function lower($value, $message = '') |
1447 | |||
1448 | /** |
||
1449 | * @psalm-pure |
||
1450 | * @psalm-assert !lowercase-string $value |
||
1451 | * |
||
1452 | * @param string $value |
||
1453 | * @param string $message |
||
1454 | * |
||
1455 | * @throws InvalidArgumentException |
||
1456 | */ |
||
1457 | 16 | View Code Duplication | public static function upper($value, $message = '') |
1471 | |||
1472 | /** |
||
1473 | * @psalm-pure |
||
1474 | * |
||
1475 | * @param string $value |
||
1476 | * @param int $length |
||
1477 | * @param string $message |
||
1478 | * |
||
1479 | * @throws InvalidArgumentException |
||
1480 | */ |
||
1481 | 36 | public static function length($value, $length, $message = '') |
|
1491 | |||
1492 | /** |
||
1493 | * Inclusive min. |
||
1494 | * |
||
1495 | * @psalm-pure |
||
1496 | * |
||
1497 | * @param string $value |
||
1498 | * @param int|float $min |
||
1499 | * @param string $message |
||
1500 | * |
||
1501 | * @throws InvalidArgumentException |
||
1502 | */ |
||
1503 | 36 | public static function minLength($value, $min, $message = '') |
|
1513 | |||
1514 | /** |
||
1515 | * Inclusive max. |
||
1516 | * |
||
1517 | * @psalm-pure |
||
1518 | * |
||
1519 | * @param string $value |
||
1520 | * @param int|float $max |
||
1521 | * @param string $message |
||
1522 | * |
||
1523 | * @throws InvalidArgumentException |
||
1524 | */ |
||
1525 | 36 | public static function maxLength($value, $max, $message = '') |
|
1535 | |||
1536 | /** |
||
1537 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1538 | * |
||
1539 | * @psalm-pure |
||
1540 | * |
||
1541 | * @param string $value |
||
1542 | * @param int|float $min |
||
1543 | * @param int|float $max |
||
1544 | * @param string $message |
||
1545 | * |
||
1546 | * @throws InvalidArgumentException |
||
1547 | */ |
||
1548 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
1561 | |||
1562 | /** |
||
1563 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1564 | * |
||
1565 | * @param mixed $value |
||
1566 | * @param string $message |
||
1567 | * |
||
1568 | * @throws InvalidArgumentException |
||
1569 | */ |
||
1570 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
1581 | |||
1582 | /** |
||
1583 | * @param mixed $value |
||
1584 | * @param string $message |
||
1585 | * |
||
1586 | * @throws InvalidArgumentException |
||
1587 | */ |
||
1588 | 12 | View Code Duplication | public static function file($value, $message = '') |
1599 | |||
1600 | /** |
||
1601 | * @param mixed $value |
||
1602 | * @param string $message |
||
1603 | * |
||
1604 | * @throws InvalidArgumentException |
||
1605 | */ |
||
1606 | 12 | View Code Duplication | public static function directory($value, $message = '') |
1617 | |||
1618 | /** |
||
1619 | * @param string $value |
||
1620 | * @param string $message |
||
1621 | * |
||
1622 | * @throws InvalidArgumentException |
||
1623 | */ |
||
1624 | public static function readable($value, $message = '') |
||
1633 | |||
1634 | /** |
||
1635 | * @param string $value |
||
1636 | * @param string $message |
||
1637 | * |
||
1638 | * @throws InvalidArgumentException |
||
1639 | */ |
||
1640 | public static function writable($value, $message = '') |
||
1649 | |||
1650 | /** |
||
1651 | * @param mixed $value |
||
1652 | * @param string $message |
||
1653 | * |
||
1654 | * @throws InvalidArgumentException |
||
1655 | */ |
||
1656 | 8 | public static function classExists($value, $message = '') |
|
1665 | |||
1666 | /** |
||
1667 | * @psalm-pure |
||
1668 | * @psalm-template ExpectedType of object |
||
1669 | * @psalm-param class-string<ExpectedType> $class |
||
1670 | * @psalm-assert class-string<ExpectedType>|ExpectedType $value |
||
1671 | * |
||
1672 | * @param mixed $value |
||
1673 | * @param string|object $class |
||
1674 | * @param string $message |
||
1675 | * |
||
1676 | * @throws InvalidArgumentException |
||
1677 | */ |
||
1678 | 8 | public static function subclassOf($value, $class, $message = '') |
|
1688 | |||
1689 | /** |
||
1690 | * @param mixed $value |
||
1691 | * @param string $message |
||
1692 | * |
||
1693 | * @throws InvalidArgumentException |
||
1694 | */ |
||
1695 | 8 | public static function interfaceExists($value, $message = '') |
|
1704 | |||
1705 | /** |
||
1706 | * @psalm-pure |
||
1707 | * @psalm-template ExpectedType of object |
||
1708 | * @psalm-param class-string<ExpectedType> $interface |
||
1709 | * @psalm-assert class-string<ExpectedType> $value |
||
1710 | * |
||
1711 | * @param mixed $value |
||
1712 | * @param mixed $interface |
||
1713 | * @param string $message |
||
1714 | * |
||
1715 | * @throws InvalidArgumentException |
||
1716 | */ |
||
1717 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
1727 | |||
1728 | /** |
||
1729 | * @psalm-pure |
||
1730 | * @psalm-param class-string|object $classOrObject |
||
1731 | * |
||
1732 | * @param string|object $classOrObject |
||
1733 | * @param mixed $property |
||
1734 | * @param string $message |
||
1735 | * |
||
1736 | * @throws InvalidArgumentException |
||
1737 | */ |
||
1738 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1747 | |||
1748 | /** |
||
1749 | * @psalm-pure |
||
1750 | * @psalm-param class-string|object $classOrObject |
||
1751 | * |
||
1752 | * @param string|object $classOrObject |
||
1753 | * @param mixed $property |
||
1754 | * @param string $message |
||
1755 | * |
||
1756 | * @throws InvalidArgumentException |
||
1757 | */ |
||
1758 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
1767 | |||
1768 | /** |
||
1769 | * @psalm-pure |
||
1770 | * @psalm-param class-string|object $classOrObject |
||
1771 | * |
||
1772 | * @param string|object $classOrObject |
||
1773 | * @param mixed $method |
||
1774 | * @param string $message |
||
1775 | * |
||
1776 | * @throws InvalidArgumentException |
||
1777 | */ |
||
1778 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1787 | |||
1788 | /** |
||
1789 | * @psalm-pure |
||
1790 | * @psalm-param class-string|object $classOrObject |
||
1791 | * |
||
1792 | * @param string|object $classOrObject |
||
1793 | * @param mixed $method |
||
1794 | * @param string $message |
||
1795 | * |
||
1796 | * @throws InvalidArgumentException |
||
1797 | */ |
||
1798 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
1807 | |||
1808 | /** |
||
1809 | * @psalm-pure |
||
1810 | * |
||
1811 | * @param array $array |
||
1812 | * @param string|int $key |
||
1813 | * @param string $message |
||
1814 | * |
||
1815 | * @throws InvalidArgumentException |
||
1816 | */ |
||
1817 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
1826 | |||
1827 | /** |
||
1828 | * @psalm-pure |
||
1829 | * |
||
1830 | * @param array $array |
||
1831 | * @param string|int $key |
||
1832 | * @param string $message |
||
1833 | * |
||
1834 | * @throws InvalidArgumentException |
||
1835 | */ |
||
1836 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1845 | |||
1846 | /** |
||
1847 | * Checks if a value is a valid array key (int or string). |
||
1848 | * |
||
1849 | * @psalm-pure |
||
1850 | * @psalm-assert array-key $value |
||
1851 | * |
||
1852 | * @param mixed $value |
||
1853 | * @param string $message |
||
1854 | * |
||
1855 | * @throws InvalidArgumentException |
||
1856 | */ |
||
1857 | 28 | public static function validArrayKey($value, $message = '') |
|
1866 | |||
1867 | /** |
||
1868 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1869 | * |
||
1870 | * @param Countable|array $array |
||
1871 | * @param int $number |
||
1872 | * @param string $message |
||
1873 | * |
||
1874 | * @throws InvalidArgumentException |
||
1875 | */ |
||
1876 | 8 | public static function count($array, $number, $message = '') |
|
1884 | |||
1885 | /** |
||
1886 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1887 | * |
||
1888 | * @param Countable|array $array |
||
1889 | * @param int|float $min |
||
1890 | * @param string $message |
||
1891 | * |
||
1892 | * @throws InvalidArgumentException |
||
1893 | */ |
||
1894 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1904 | |||
1905 | /** |
||
1906 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1907 | * |
||
1908 | * @param Countable|array $array |
||
1909 | * @param int|float $max |
||
1910 | * @param string $message |
||
1911 | * |
||
1912 | * @throws InvalidArgumentException |
||
1913 | */ |
||
1914 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1924 | |||
1925 | /** |
||
1926 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1927 | * |
||
1928 | * @param Countable|array $array |
||
1929 | * @param int|float $min |
||
1930 | * @param int|float $max |
||
1931 | * @param string $message |
||
1932 | * |
||
1933 | * @throws InvalidArgumentException |
||
1934 | */ |
||
1935 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
1948 | |||
1949 | /** |
||
1950 | * @psalm-pure |
||
1951 | * @psalm-assert list $array |
||
1952 | * |
||
1953 | * @param mixed $array |
||
1954 | * @param string $message |
||
1955 | * |
||
1956 | * @throws InvalidArgumentException |
||
1957 | */ |
||
1958 | 80 | View Code Duplication | public static function isList($array, $message = '') |
1966 | |||
1967 | /** |
||
1968 | * @psalm-pure |
||
1969 | * @psalm-assert list $array |
||
1970 | * @psalm-assert !empty $array |
||
1971 | * |
||
1972 | * @param mixed $array |
||
1973 | * @param string $message |
||
1974 | * |
||
1975 | * @throws InvalidArgumentException |
||
1976 | */ |
||
1977 | 40 | public static function isNonEmptyList($array, $message = '') |
|
1982 | |||
1983 | /** |
||
1984 | * @psalm-pure |
||
1985 | * @psalm-template T |
||
1986 | * @psalm-param mixed|array<T> $array |
||
1987 | * @psalm-assert array<string, T> $array |
||
1988 | * |
||
1989 | * @param mixed $array |
||
1990 | * @param string $message |
||
1991 | * |
||
1992 | * @throws InvalidArgumentException |
||
1993 | */ |
||
1994 | 32 | public static function isMap($array, $message = '') |
|
2005 | |||
2006 | /** |
||
2007 | * @psalm-pure |
||
2008 | * @psalm-template T |
||
2009 | * @psalm-param mixed|array<T> $array |
||
2010 | * @psalm-assert array<string, T> $array |
||
2011 | * @psalm-assert !empty $array |
||
2012 | * |
||
2013 | * @param mixed $array |
||
2014 | * @param string $message |
||
2015 | * |
||
2016 | * @throws InvalidArgumentException |
||
2017 | */ |
||
2018 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
2023 | |||
2024 | /** |
||
2025 | * @psalm-pure |
||
2026 | * |
||
2027 | * @param string $value |
||
2028 | * @param string $message |
||
2029 | * |
||
2030 | * @throws InvalidArgumentException |
||
2031 | */ |
||
2032 | 56 | public static function uuid($value, $message = '') |
|
2049 | |||
2050 | /** |
||
2051 | * @psalm-param class-string<Throwable> |
||
2052 | * |
||
2053 | * @param Closure $expression |
||
2054 | * @param string $class |
||
2055 | * @param string $message |
||
2056 | * |
||
2057 | * @throws InvalidArgumentException |
||
2058 | */ |
||
2059 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
2085 | |||
2086 | /** |
||
2087 | * @throws BadMethodCallException |
||
2088 | */ |
||
2089 | 1635 | public static function __callStatic($name, $arguments) |
|
2117 | |||
2118 | /** |
||
2119 | * @param mixed $value |
||
2120 | * |
||
2121 | * @return string |
||
2122 | */ |
||
2123 | 746 | protected static function valueToString($value) |
|
2159 | |||
2160 | /** |
||
2161 | * @param mixed $value |
||
2162 | * |
||
2163 | * @return string |
||
2164 | */ |
||
2165 | 251 | protected static function typeToString($value) |
|
2169 | |||
2170 | 168 | protected static function strlen($value) |
|
2182 | |||
2183 | /** |
||
2184 | * @param string $message |
||
2185 | * |
||
2186 | * @throws InvalidArgumentException |
||
2187 | * |
||
2188 | * @psalm-pure this method is not supposed to perform side-effects |
||
2189 | */ |
||
2190 | 1060 | protected static function reportInvalidArgument($message) |
|
2194 | |||
2195 | private function __construct() |
||
2198 | } |
||
2199 |