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 |
||
197 | class Assert |
||
|
|||
198 | { |
||
199 | /** |
||
200 | * @psalm-assert string $value |
||
201 | * |
||
202 | * @param mixed $value |
||
203 | * @param string $message |
||
204 | * |
||
205 | * @throws InvalidArgumentException |
||
206 | */ |
||
207 | 1050 | public static function string($value, $message = '') |
|
208 | { |
||
209 | 1050 | if (!\is_string($value)) { |
|
210 | 178 | static::reportInvalidArgument(\sprintf( |
|
211 | 178 | $message ?: 'Expected a string. Got: %s', |
|
212 | 178 | static::typeToString($value) |
|
213 | )); |
||
214 | } |
||
215 | 872 | } |
|
216 | |||
217 | /** |
||
218 | * @param mixed $value |
||
219 | * @param string $message |
||
220 | * |
||
221 | * @throws InvalidArgumentException |
||
222 | */ |
||
223 | 16 | public static function stringNotEmpty($value, $message = '') |
|
224 | { |
||
225 | 16 | static::string($value, $message); |
|
226 | 12 | static::notEq($value, '', $message); |
|
227 | 8 | } |
|
228 | |||
229 | /** |
||
230 | * @psalm-assert int $value |
||
231 | * |
||
232 | * @param mixed $value |
||
233 | * @param string $message |
||
234 | * |
||
235 | * @throws InvalidArgumentException |
||
236 | */ |
||
237 | 17 | public static function integer($value, $message = '') |
|
238 | { |
||
239 | 17 | if (!\is_int($value)) { |
|
240 | 13 | static::reportInvalidArgument(\sprintf( |
|
241 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
242 | 13 | static::typeToString($value) |
|
243 | )); |
||
244 | } |
||
245 | 4 | } |
|
246 | |||
247 | /** |
||
248 | * @psalm-assert numeric $value |
||
249 | * |
||
250 | * @param mixed $value |
||
251 | * @param string $message |
||
252 | * |
||
253 | * @throws InvalidArgumentException |
||
254 | */ |
||
255 | 16 | public static function integerish($value, $message = '') |
|
256 | { |
||
257 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
258 | 4 | static::reportInvalidArgument(\sprintf( |
|
259 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
260 | 4 | static::typeToString($value) |
|
261 | )); |
||
262 | } |
||
263 | 12 | } |
|
264 | |||
265 | /** |
||
266 | * @psalm-assert float $value |
||
267 | * |
||
268 | * @param mixed $value |
||
269 | * @param string $message |
||
270 | * |
||
271 | * @throws InvalidArgumentException |
||
272 | */ |
||
273 | 16 | public static function float($value, $message = '') |
|
274 | { |
||
275 | 16 | if (!\is_float($value)) { |
|
276 | 8 | static::reportInvalidArgument(\sprintf( |
|
277 | 8 | $message ?: 'Expected a float. Got: %s', |
|
278 | 8 | static::typeToString($value) |
|
279 | )); |
||
280 | } |
||
281 | 8 | } |
|
282 | |||
283 | /** |
||
284 | * @psalm-assert numeric $value |
||
285 | * |
||
286 | * @param mixed $value |
||
287 | * @param string $message |
||
288 | * |
||
289 | * @throws InvalidArgumentException |
||
290 | */ |
||
291 | 20 | public static function numeric($value, $message = '') |
|
292 | { |
||
293 | 20 | if (!\is_numeric($value)) { |
|
294 | 4 | static::reportInvalidArgument(\sprintf( |
|
295 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
296 | 4 | static::typeToString($value) |
|
297 | )); |
||
298 | } |
||
299 | 16 | } |
|
300 | |||
301 | /** |
||
302 | * @psalm-assert int $value |
||
303 | * |
||
304 | * @param mixed $value |
||
305 | * @param string $message |
||
306 | * |
||
307 | * @throws InvalidArgumentException |
||
308 | */ |
||
309 | 24 | public static function natural($value, $message = '') |
|
310 | { |
||
311 | 24 | if (!\is_int($value) || $value < 0) { |
|
312 | 16 | static::reportInvalidArgument(\sprintf( |
|
313 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
314 | 16 | static::valueToString($value) |
|
315 | )); |
||
316 | } |
||
317 | 8 | } |
|
318 | |||
319 | /** |
||
320 | * @psalm-assert bool $value |
||
321 | * |
||
322 | * @param mixed $value |
||
323 | * @param string $message |
||
324 | * |
||
325 | * @throws InvalidArgumentException |
||
326 | */ |
||
327 | 16 | public static function boolean($value, $message = '') |
|
328 | { |
||
329 | 16 | if (!\is_bool($value)) { |
|
330 | 8 | static::reportInvalidArgument(\sprintf( |
|
331 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
332 | 8 | static::typeToString($value) |
|
333 | )); |
||
334 | } |
||
335 | 8 | } |
|
336 | |||
337 | /** |
||
338 | * @psalm-assert scalar $value |
||
339 | * |
||
340 | * @param mixed $value |
||
341 | * @param string $message |
||
342 | * |
||
343 | * @throws InvalidArgumentException |
||
344 | */ |
||
345 | 23 | public static function scalar($value, $message = '') |
|
346 | { |
||
347 | 23 | if (!\is_scalar($value)) { |
|
348 | 11 | static::reportInvalidArgument(\sprintf( |
|
349 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
350 | 11 | static::typeToString($value) |
|
351 | )); |
||
352 | } |
||
353 | 12 | } |
|
354 | |||
355 | /** |
||
356 | * @psalm-assert object $value |
||
357 | * |
||
358 | * @param mixed $value |
||
359 | * @param string $message |
||
360 | * |
||
361 | * @throws InvalidArgumentException |
||
362 | */ |
||
363 | 23 | public static function object($value, $message = '') |
|
364 | { |
||
365 | 23 | if (!\is_object($value)) { |
|
366 | 15 | static::reportInvalidArgument(\sprintf( |
|
367 | 15 | $message ?: 'Expected an object. Got: %s', |
|
368 | 15 | static::typeToString($value) |
|
369 | )); |
||
370 | } |
||
371 | 8 | } |
|
372 | |||
373 | /** |
||
374 | * @psalm-assert resource $value |
||
375 | * |
||
376 | * @param mixed $value |
||
377 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
378 | * @param string $message |
||
379 | * |
||
380 | * @throws InvalidArgumentException |
||
381 | */ |
||
382 | 16 | public static function resource($value, $type = null, $message = '') |
|
383 | { |
||
384 | 16 | if (!\is_resource($value)) { |
|
385 | 4 | static::reportInvalidArgument(\sprintf( |
|
386 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
387 | 4 | static::typeToString($value) |
|
388 | )); |
||
389 | } |
||
390 | |||
391 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
392 | 4 | static::reportInvalidArgument(\sprintf( |
|
393 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
394 | 4 | static::typeToString($value), |
|
395 | 4 | $type |
|
396 | )); |
||
397 | } |
||
398 | 8 | } |
|
399 | |||
400 | /** |
||
401 | * @psalm-assert callable $value |
||
402 | * |
||
403 | * @param mixed $value |
||
404 | * @param string $message |
||
405 | * |
||
406 | * @throws InvalidArgumentException |
||
407 | */ |
||
408 | 20 | public static function isCallable($value, $message = '') |
|
409 | { |
||
410 | 20 | if (!\is_callable($value)) { |
|
411 | 8 | static::reportInvalidArgument(\sprintf( |
|
412 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
413 | 8 | static::typeToString($value) |
|
414 | )); |
||
415 | } |
||
416 | 12 | } |
|
417 | |||
418 | /** |
||
419 | * @psalm-assert array $value |
||
420 | * |
||
421 | * @param mixed $value |
||
422 | * @param string $message |
||
423 | * |
||
424 | * @throws InvalidArgumentException |
||
425 | */ |
||
426 | 20 | public static function isArray($value, $message = '') |
|
435 | |||
436 | /** |
||
437 | * @psalm-assert iterable $value |
||
438 | * |
||
439 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
440 | * |
||
441 | * @param mixed $value |
||
442 | * @param string $message |
||
443 | * |
||
444 | * @throws InvalidArgumentException |
||
445 | */ |
||
446 | 20 | public static function isTraversable($value, $message = '') |
|
463 | |||
464 | /** |
||
465 | * @param mixed $value |
||
466 | * @param string $message |
||
467 | * |
||
468 | * @throws InvalidArgumentException |
||
469 | */ |
||
470 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
471 | { |
||
472 | 20 | if (!\is_array($value) && !($value instanceof ArrayAccess)) { |
|
473 | 8 | static::reportInvalidArgument(\sprintf( |
|
474 | 8 | $message ?: 'Expected an array accessible. Got: %s', |
|
475 | 8 | static::typeToString($value) |
|
476 | )); |
||
477 | } |
||
478 | 12 | } |
|
479 | |||
480 | /** |
||
481 | * @psalm-assert countable $value |
||
482 | * |
||
483 | * @param mixed $value |
||
484 | * @param string $message |
||
485 | * |
||
486 | * @throws InvalidArgumentException |
||
487 | */ |
||
488 | 96 | public static function isCountable($value, $message = '') |
|
489 | { |
||
490 | if ( |
||
491 | 96 | !\is_array($value) |
|
492 | 96 | && !($value instanceof Countable) |
|
493 | 96 | && !($value instanceof ResourceBundle) |
|
494 | 96 | && !($value instanceof SimpleXMLElement) |
|
495 | ) { |
||
496 | 28 | static::reportInvalidArgument(\sprintf( |
|
497 | 28 | $message ?: 'Expected a countable. Got: %s', |
|
498 | 28 | static::typeToString($value) |
|
499 | )); |
||
500 | } |
||
501 | 68 | } |
|
502 | |||
503 | /** |
||
504 | * @psalm-assert iterable $value |
||
505 | * |
||
506 | * @param mixed $value |
||
507 | * @param string $message |
||
508 | * |
||
509 | * @throws InvalidArgumentException |
||
510 | */ |
||
511 | 964 | View Code Duplication | public static function isIterable($value, $message = '') |
512 | { |
||
513 | 964 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
514 | 8 | static::reportInvalidArgument(\sprintf( |
|
515 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
516 | 8 | static::typeToString($value) |
|
517 | )); |
||
518 | } |
||
519 | 960 | } |
|
520 | |||
521 | /** |
||
522 | * @psalm-template ExpectedType of object |
||
523 | * @psalm-param class-string<ExpectedType> $class |
||
524 | * @psalm-assert ExpectedType $value |
||
525 | * |
||
526 | * @param mixed $value |
||
527 | * @param string|object $class |
||
528 | * @param string $message |
||
529 | * |
||
530 | * @throws InvalidArgumentException |
||
531 | */ |
||
532 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
533 | { |
||
534 | 19 | if (!($value instanceof $class)) { |
|
535 | 15 | static::reportInvalidArgument(\sprintf( |
|
536 | 15 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
537 | 15 | static::typeToString($value), |
|
538 | 15 | $class |
|
539 | )); |
||
540 | } |
||
541 | 4 | } |
|
542 | |||
543 | /** |
||
544 | * @psalm-template ExpectedType of object |
||
545 | * @psalm-param class-string<ExpectedType> $class |
||
546 | * @psalm-assert !ExpectedType $value |
||
547 | * |
||
548 | * @param mixed $value |
||
549 | * @param string|object $class |
||
550 | * @param string $message |
||
551 | * |
||
552 | * @throws InvalidArgumentException |
||
553 | */ |
||
554 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
555 | { |
||
556 | 16 | if ($value instanceof $class) { |
|
557 | 4 | static::reportInvalidArgument(\sprintf( |
|
558 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
559 | 4 | static::typeToString($value), |
|
560 | 4 | $class |
|
561 | )); |
||
562 | } |
||
563 | 12 | } |
|
564 | |||
565 | /** |
||
566 | * @param mixed $value |
||
567 | * @param array<object|string> $classes |
||
568 | * @param string $message |
||
569 | * |
||
570 | * @throws InvalidArgumentException |
||
571 | */ |
||
572 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
573 | { |
||
574 | 20 | foreach ($classes as $class) { |
|
575 | 20 | if ($value instanceof $class) { |
|
576 | 8 | return; |
|
577 | } |
||
578 | } |
||
579 | |||
580 | /** @psalm-suppress InvalidArgument */ |
||
581 | 12 | static::reportInvalidArgument(\sprintf( |
|
582 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
583 | 12 | static::typeToString($value), |
|
584 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
585 | )); |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * @psalm-assert empty $value |
||
590 | * |
||
591 | * @param mixed $value |
||
592 | * @param string $message |
||
593 | * |
||
594 | * @throws InvalidArgumentException |
||
595 | */ |
||
596 | 23 | public static function isEmpty($value, $message = '') |
|
597 | { |
||
598 | 23 | if (!empty($value)) { |
|
599 | 8 | static::reportInvalidArgument(\sprintf( |
|
600 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
601 | 8 | static::valueToString($value) |
|
602 | )); |
||
603 | } |
||
604 | 15 | } |
|
605 | |||
606 | /** |
||
607 | * @psalm-assert !empty $value |
||
608 | * |
||
609 | * @param mixed $value |
||
610 | * @param string $message |
||
611 | * |
||
612 | * @throws InvalidArgumentException |
||
613 | */ |
||
614 | 23 | public static function notEmpty($value, $message = '') |
|
615 | { |
||
616 | 23 | if (empty($value)) { |
|
617 | 15 | static::reportInvalidArgument(\sprintf( |
|
618 | 15 | $message ?: 'Expected a non-empty value. Got: %s', |
|
619 | 15 | static::valueToString($value) |
|
620 | )); |
||
621 | } |
||
622 | 8 | } |
|
623 | |||
624 | /** |
||
625 | * @psalm-assert null $value |
||
626 | * |
||
627 | * @param mixed $value |
||
628 | * @param string $message |
||
629 | * |
||
630 | * @throws InvalidArgumentException |
||
631 | */ |
||
632 | 11 | public static function null($value, $message = '') |
|
633 | { |
||
634 | 11 | if (null !== $value) { |
|
635 | 8 | static::reportInvalidArgument(\sprintf( |
|
636 | 8 | $message ?: 'Expected null. Got: %s', |
|
637 | 8 | static::valueToString($value) |
|
638 | )); |
||
639 | } |
||
640 | 3 | } |
|
641 | |||
642 | /** |
||
643 | * @psalm-assert !null $value |
||
644 | * |
||
645 | * @param mixed $value |
||
646 | * @param string $message |
||
647 | * |
||
648 | * @throws InvalidArgumentException |
||
649 | */ |
||
650 | 11 | public static function notNull($value, $message = '') |
|
651 | { |
||
652 | 11 | if (null === $value) { |
|
653 | 3 | static::reportInvalidArgument( |
|
654 | 3 | $message ?: 'Expected a value other than null.' |
|
655 | ); |
||
656 | } |
||
657 | 8 | } |
|
658 | |||
659 | /** |
||
660 | * @psalm-assert true $value |
||
661 | * |
||
662 | * @param mixed $value |
||
663 | * @param string $message |
||
664 | * |
||
665 | * @throws InvalidArgumentException |
||
666 | */ |
||
667 | 15 | public static function true($value, $message = '') |
|
668 | { |
||
669 | 15 | if (true !== $value) { |
|
670 | 11 | static::reportInvalidArgument(\sprintf( |
|
671 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
672 | 11 | static::valueToString($value) |
|
673 | )); |
||
674 | } |
||
675 | 4 | } |
|
676 | |||
677 | /** |
||
678 | * @psalm-assert false $value |
||
679 | * |
||
680 | * @param mixed $value |
||
681 | * @param string $message |
||
682 | * |
||
683 | * @throws InvalidArgumentException |
||
684 | */ |
||
685 | 19 | public static function false($value, $message = '') |
|
686 | { |
||
687 | 19 | if (false !== $value) { |
|
688 | 15 | static::reportInvalidArgument(\sprintf( |
|
689 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
690 | 15 | static::valueToString($value) |
|
691 | )); |
||
692 | } |
||
693 | 4 | } |
|
694 | |||
695 | /** |
||
696 | * @param mixed $value |
||
697 | * @param string $message |
||
698 | * |
||
699 | * @throws InvalidArgumentException |
||
700 | */ |
||
701 | 51 | View Code Duplication | public static function ip($value, $message = '') |
702 | { |
||
703 | 51 | static::string($value, $message); |
|
704 | 32 | if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { |
|
705 | 4 | static::reportInvalidArgument(\sprintf( |
|
706 | 4 | $message ?: 'Expected a value to be an IP. Got: %s', |
|
707 | 4 | static::valueToString($value) |
|
708 | )); |
||
709 | } |
||
710 | 28 | } |
|
711 | |||
712 | /** |
||
713 | * @param mixed $value |
||
714 | * @param string $message |
||
715 | * |
||
716 | * @throws InvalidArgumentException |
||
717 | */ |
||
718 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
719 | { |
||
720 | 51 | static::string($value, $message); |
|
721 | 32 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
722 | 20 | static::reportInvalidArgument(\sprintf( |
|
723 | 20 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
724 | 20 | static::valueToString($value) |
|
725 | )); |
||
726 | } |
||
727 | 12 | } |
|
728 | |||
729 | /** |
||
730 | * @param mixed $value |
||
731 | * @param string $message |
||
732 | * |
||
733 | * @throws InvalidArgumentException |
||
734 | */ |
||
735 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
736 | { |
||
737 | 51 | static::string($value, $message); |
|
738 | 32 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { |
|
739 | 16 | static::reportInvalidArgument(\sprintf( |
|
740 | 16 | $message ?: 'Expected a value to be an IPv6. Got %s', |
|
741 | 16 | static::valueToString($value) |
|
742 | )); |
||
743 | } |
||
744 | 16 | } |
|
745 | |||
746 | /** |
||
747 | * @param mixed $value |
||
748 | * @param string $message |
||
749 | * |
||
750 | * @throws InvalidArgumentException |
||
751 | */ |
||
752 | 20 | View Code Duplication | public static function email($value, $message = '') |
762 | |||
763 | /** |
||
764 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
765 | * |
||
766 | * @param array $values |
||
767 | * @param string $message |
||
768 | * |
||
769 | * @throws InvalidArgumentException |
||
770 | */ |
||
771 | 12 | public static function uniqueValues(array $values, $message = '') |
|
786 | |||
787 | /** |
||
788 | * @param mixed $value |
||
789 | * @param mixed $expect |
||
790 | * @param string $message |
||
791 | * |
||
792 | * @throws InvalidArgumentException |
||
793 | */ |
||
794 | 33 | public static function eq($value, $expect, $message = '') |
|
804 | |||
805 | /** |
||
806 | * @param mixed $value |
||
807 | * @param mixed $expect |
||
808 | * @param string $message |
||
809 | * |
||
810 | * @throws InvalidArgumentException |
||
811 | */ |
||
812 | 28 | public static function notEq($value, $expect, $message = '') |
|
821 | |||
822 | /** |
||
823 | * @param mixed $value |
||
824 | * @param mixed $expect |
||
825 | * @param string $message |
||
826 | * |
||
827 | * @throws InvalidArgumentException |
||
828 | */ |
||
829 | 16 | public static function same($value, $expect, $message = '') |
|
830 | { |
||
831 | 16 | if ($expect !== $value) { |
|
832 | 12 | static::reportInvalidArgument(\sprintf( |
|
833 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
834 | 12 | static::valueToString($value), |
|
835 | 12 | static::valueToString($expect) |
|
836 | )); |
||
837 | } |
||
838 | 4 | } |
|
839 | |||
840 | /** |
||
841 | * @param mixed $value |
||
842 | * @param mixed $expect |
||
843 | * @param string $message |
||
844 | * |
||
845 | * @throws InvalidArgumentException |
||
846 | */ |
||
847 | 16 | public static function notSame($value, $expect, $message = '') |
|
848 | { |
||
849 | 16 | if ($expect === $value) { |
|
850 | 4 | static::reportInvalidArgument(\sprintf( |
|
851 | 4 | $message ?: 'Expected a value not identical to %s.', |
|
852 | 4 | static::valueToString($expect) |
|
853 | )); |
||
854 | } |
||
855 | 12 | } |
|
856 | |||
857 | /** |
||
858 | * @param mixed $value |
||
859 | * @param mixed $limit |
||
860 | * @param string $message |
||
861 | * |
||
862 | * @throws InvalidArgumentException |
||
863 | */ |
||
864 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
865 | { |
||
866 | 8 | if ($value <= $limit) { |
|
867 | 4 | static::reportInvalidArgument(\sprintf( |
|
868 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
869 | 4 | static::valueToString($value), |
|
870 | 4 | static::valueToString($limit) |
|
871 | )); |
||
872 | } |
||
873 | 4 | } |
|
874 | |||
875 | /** |
||
876 | * @param mixed $value |
||
877 | * @param mixed $limit |
||
878 | * @param string $message |
||
879 | * |
||
880 | * @throws InvalidArgumentException |
||
881 | */ |
||
882 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
883 | { |
||
884 | 12 | if ($value < $limit) { |
|
885 | 4 | static::reportInvalidArgument(\sprintf( |
|
886 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
887 | 4 | static::valueToString($value), |
|
888 | 4 | static::valueToString($limit) |
|
889 | )); |
||
890 | } |
||
891 | 8 | } |
|
892 | |||
893 | /** |
||
894 | * @param mixed $value |
||
895 | * @param mixed $limit |
||
896 | * @param string $message |
||
897 | * |
||
898 | * @throws InvalidArgumentException |
||
899 | */ |
||
900 | 8 | public static function lessThan($value, $limit, $message = '') |
|
901 | { |
||
902 | 8 | if ($value >= $limit) { |
|
903 | 4 | static::reportInvalidArgument(\sprintf( |
|
904 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
905 | 4 | static::valueToString($value), |
|
906 | 4 | static::valueToString($limit) |
|
907 | )); |
||
908 | } |
||
909 | 4 | } |
|
910 | |||
911 | /** |
||
912 | * @param mixed $value |
||
913 | * @param mixed $limit |
||
914 | * @param string $message |
||
915 | * |
||
916 | * @throws InvalidArgumentException |
||
917 | */ |
||
918 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
919 | { |
||
920 | 12 | if ($value > $limit) { |
|
921 | 4 | static::reportInvalidArgument(\sprintf( |
|
922 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
923 | 4 | static::valueToString($value), |
|
924 | 4 | static::valueToString($limit) |
|
925 | )); |
||
926 | } |
||
927 | 8 | } |
|
928 | |||
929 | /** |
||
930 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
931 | * |
||
932 | * @param mixed $value |
||
933 | * @param mixed $min |
||
934 | * @param mixed $max |
||
935 | * @param string $message |
||
936 | * |
||
937 | * @throws InvalidArgumentException |
||
938 | */ |
||
939 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
940 | { |
||
941 | 16 | if ($value < $min || $value > $max) { |
|
942 | 8 | static::reportInvalidArgument(\sprintf( |
|
943 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
944 | 8 | static::valueToString($value), |
|
945 | 8 | static::valueToString($min), |
|
946 | 8 | static::valueToString($max) |
|
947 | )); |
||
948 | } |
||
949 | 8 | } |
|
950 | |||
951 | /** |
||
952 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
953 | * |
||
954 | * @param mixed $value |
||
955 | * @param array $values |
||
956 | * @param string $message |
||
957 | * |
||
958 | * @throws InvalidArgumentException |
||
959 | */ |
||
960 | 8 | public static function oneOf($value, array $values, $message = '') |
|
961 | { |
||
962 | 8 | if (!\in_array($value, $values, true)) { |
|
963 | /** @psalm-suppress InvalidArgument */ |
||
964 | 4 | static::reportInvalidArgument(\sprintf( |
|
965 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
966 | 4 | static::valueToString($value), |
|
967 | 4 | \implode(', ', \array_map(array('static', 'valueToString'), $values)) |
|
968 | )); |
||
969 | } |
||
970 | 4 | } |
|
971 | |||
972 | /** |
||
973 | * @param string $value |
||
974 | * @param string $subString |
||
975 | * @param string $message |
||
976 | * |
||
977 | * @throws InvalidArgumentException |
||
978 | */ |
||
979 | 84 | View Code Duplication | public static function contains($value, $subString, $message = '') |
980 | { |
||
981 | 84 | static::string($value); |
|
982 | |||
983 | 80 | if (false === \strpos($value, $subString)) { |
|
984 | 32 | static::reportInvalidArgument(\sprintf( |
|
985 | 32 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
986 | 32 | static::valueToString($value), |
|
987 | 32 | static::valueToString($subString) |
|
988 | )); |
||
989 | } |
||
990 | 48 | } |
|
991 | |||
992 | /** |
||
993 | * @param string $value |
||
994 | * @param string $subString |
||
995 | * @param string $message |
||
996 | * |
||
997 | * @throws InvalidArgumentException |
||
998 | */ |
||
999 | 84 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1000 | { |
||
1001 | 84 | static::string($value); |
|
1002 | |||
1003 | 80 | if (false !== \strpos($value, $subString)) { |
|
1004 | 48 | static::reportInvalidArgument(\sprintf( |
|
1005 | 48 | $message ?: '%2$s was not expected to be contained in a value. Got: %s', |
|
1006 | 48 | static::valueToString($value), |
|
1007 | 48 | static::valueToString($subString) |
|
1008 | )); |
||
1009 | } |
||
1010 | 32 | } |
|
1011 | |||
1012 | /** |
||
1013 | * @param string $value |
||
1014 | * @param string $message |
||
1015 | * |
||
1016 | * @throws InvalidArgumentException |
||
1017 | */ |
||
1018 | 40 | View Code Duplication | public static function notWhitespaceOnly($value, $message = '') |
1019 | { |
||
1020 | 40 | static::string($value); |
|
1021 | |||
1022 | 40 | if (\preg_match('/^\s*$/', $value)) { |
|
1023 | 24 | static::reportInvalidArgument(\sprintf( |
|
1024 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
1025 | 24 | static::valueToString($value) |
|
1026 | )); |
||
1027 | } |
||
1028 | 16 | } |
|
1029 | |||
1030 | /** |
||
1031 | * @param string $value |
||
1032 | * @param string $prefix |
||
1033 | * @param string $message |
||
1034 | * |
||
1035 | * @throws InvalidArgumentException |
||
1036 | */ |
||
1037 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
1038 | { |
||
1039 | 48 | static::string($value); |
|
1040 | |||
1041 | 48 | if (0 !== \strpos($value, $prefix)) { |
|
1042 | 32 | static::reportInvalidArgument(\sprintf( |
|
1043 | 32 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
1044 | 32 | static::valueToString($value), |
|
1045 | 32 | static::valueToString($prefix) |
|
1046 | )); |
||
1047 | } |
||
1048 | 16 | } |
|
1049 | |||
1050 | /** |
||
1051 | * @param mixed $value |
||
1052 | * @param string $message |
||
1053 | * |
||
1054 | * @throws InvalidArgumentException |
||
1055 | */ |
||
1056 | 35 | public static function startsWithLetter($value, $message = '') |
|
1057 | { |
||
1058 | 35 | static::string($value); |
|
1059 | |||
1060 | 24 | $valid = isset($value[0]); |
|
1061 | |||
1062 | 24 | if ($valid) { |
|
1063 | 20 | $locale = \setlocale(LC_CTYPE, 0); |
|
1064 | 20 | \setlocale(LC_CTYPE, 'C'); |
|
1065 | 20 | $valid = \ctype_alpha($value[0]); |
|
1066 | 20 | \setlocale(LC_CTYPE, $locale); |
|
1067 | } |
||
1068 | |||
1069 | 24 | if (!$valid) { |
|
1070 | 12 | static::reportInvalidArgument(\sprintf( |
|
1071 | 12 | $message ?: 'Expected a value to start with a letter. Got: %s', |
|
1072 | 12 | static::valueToString($value) |
|
1073 | )); |
||
1074 | } |
||
1075 | 12 | } |
|
1076 | |||
1077 | /** |
||
1078 | * @param string $value |
||
1079 | * @param string $suffix |
||
1080 | * @param string $message |
||
1081 | * |
||
1082 | * @throws InvalidArgumentException |
||
1083 | */ |
||
1084 | 48 | public static function endsWith($value, $suffix, $message = '') |
|
1085 | { |
||
1086 | 48 | static::string($value, $message); |
|
1087 | |||
1088 | 48 | if ($suffix !== \substr($value, -\strlen($suffix))) { |
|
1089 | 32 | static::reportInvalidArgument(\sprintf( |
|
1090 | 32 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
1091 | 32 | static::valueToString($value), |
|
1092 | 32 | static::valueToString($suffix) |
|
1093 | )); |
||
1094 | } |
||
1095 | 16 | } |
|
1096 | |||
1097 | /** |
||
1098 | * @param string $value |
||
1099 | * @param string $pattern |
||
1100 | * @param string $message |
||
1101 | * |
||
1102 | * @throws InvalidArgumentException |
||
1103 | */ |
||
1104 | 12 | public static function regex($value, $pattern, $message = '') |
|
1105 | { |
||
1106 | 12 | static::string($value, $message); |
|
1107 | 12 | static::string($pattern, $message); |
|
1108 | |||
1109 | 12 | if (!\preg_match($pattern, $value)) { |
|
1110 | 8 | static::reportInvalidArgument(\sprintf( |
|
1111 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
1112 | 8 | static::valueToString($value) |
|
1113 | )); |
||
1114 | } |
||
1115 | 4 | } |
|
1116 | |||
1117 | /** |
||
1118 | * @param string $value |
||
1119 | * @param string $pattern |
||
1120 | * @param string $message |
||
1121 | * |
||
1122 | * @throws InvalidArgumentException |
||
1123 | */ |
||
1124 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
1125 | { |
||
1126 | 12 | static::string($value, $message); |
|
1127 | 12 | static::string($pattern, $message); |
|
1128 | |||
1129 | 12 | if (\preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) { |
|
1130 | /** |
||
1131 | * @psalm-suppress MixedArgument |
||
1132 | * @psalm-suppress MixedArrayAccess |
||
1133 | */ |
||
1134 | 4 | static::reportInvalidArgument(\sprintf( |
|
1135 | 4 | $message ?: 'The value %s matches the pattern %s (at offset %d).', |
|
1136 | 4 | static::valueToString($value), |
|
1137 | 4 | static::valueToString($pattern), |
|
1138 | 4 | $matches[0][1] |
|
1139 | )); |
||
1140 | } |
||
1141 | 8 | } |
|
1142 | |||
1143 | /** |
||
1144 | * @param mixed $value |
||
1145 | * @param string $message |
||
1146 | * |
||
1147 | * @throws InvalidArgumentException |
||
1148 | */ |
||
1149 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
1160 | |||
1161 | /** |
||
1162 | * @param mixed $value |
||
1163 | * @param string $message |
||
1164 | * |
||
1165 | * @throws InvalidArgumentException |
||
1166 | */ |
||
1167 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
1168 | { |
||
1169 | 20 | static::string($value); |
|
1170 | |||
1171 | 12 | $locale = \setlocale(LC_CTYPE, 0); |
|
1172 | 12 | \setlocale(LC_CTYPE, 'C'); |
|
1173 | 12 | $valid = !\ctype_alpha($value); |
|
1174 | 12 | \setlocale(LC_CTYPE, $locale); |
|
1175 | |||
1176 | 12 | if ($valid) { |
|
1177 | 8 | static::reportInvalidArgument(\sprintf( |
|
1178 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
1179 | 8 | static::valueToString($value) |
|
1180 | )); |
||
1181 | } |
||
1182 | 4 | } |
|
1183 | |||
1184 | /** |
||
1185 | * @param mixed $value |
||
1186 | * @param string $message |
||
1187 | * |
||
1188 | * @throws InvalidArgumentException |
||
1189 | */ |
||
1190 | 24 | View Code Duplication | public static function digits($value, $message = '') |
1206 | |||
1207 | /** |
||
1208 | * @param mixed $value |
||
1209 | * @param string $message |
||
1210 | * |
||
1211 | * @throws InvalidArgumentException |
||
1212 | */ |
||
1213 | 32 | View Code Duplication | public static function alnum($value, $message = '') |
1229 | |||
1230 | /** |
||
1231 | * @param mixed $value |
||
1232 | * @param string $message |
||
1233 | * |
||
1234 | * @throws InvalidArgumentException |
||
1235 | */ |
||
1236 | 32 | View Code Duplication | public static function lower($value, $message = '') |
1252 | |||
1253 | /** |
||
1254 | * @param mixed $value |
||
1255 | * @param string $message |
||
1256 | * |
||
1257 | * @throws InvalidArgumentException |
||
1258 | */ |
||
1259 | 32 | View Code Duplication | public static function upper($value, $message = '') |
1275 | |||
1276 | /** |
||
1277 | * @param string $value |
||
1278 | * @param int|float $length |
||
1279 | * @param string $message |
||
1280 | * |
||
1281 | * @throws InvalidArgumentException |
||
1282 | */ |
||
1283 | 36 | View Code Duplication | public static function length($value, $length, $message = '') |
1295 | |||
1296 | /** |
||
1297 | * Inclusive min. |
||
1298 | * |
||
1299 | * @param string $value |
||
1300 | * @param int|float $min |
||
1301 | * @param string $message |
||
1302 | * |
||
1303 | * @throws InvalidArgumentException |
||
1304 | */ |
||
1305 | 36 | View Code Duplication | public static function minLength($value, $min, $message = '') |
1316 | |||
1317 | /** |
||
1318 | * Inclusive max. |
||
1319 | * |
||
1320 | * @param string $value |
||
1321 | * @param int|float $max |
||
1322 | * @param string $message |
||
1323 | * |
||
1324 | * @throws InvalidArgumentException |
||
1325 | */ |
||
1326 | 36 | View Code Duplication | public static function maxLength($value, $max, $message = '') |
1338 | |||
1339 | /** |
||
1340 | * Inclusive, so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1341 | * |
||
1342 | * @param string $value |
||
1343 | * @param int|float $min |
||
1344 | * @param int|float $max |
||
1345 | * @param string $message |
||
1346 | * |
||
1347 | * @throws InvalidArgumentException |
||
1348 | */ |
||
1349 | 60 | public static function lengthBetween($value, $min, $max, $message = '') |
|
1363 | |||
1364 | /** |
||
1365 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1366 | * |
||
1367 | * @param string $value |
||
1368 | * @param string $message |
||
1369 | * |
||
1370 | * @throws InvalidArgumentException |
||
1371 | */ |
||
1372 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
1383 | |||
1384 | /** |
||
1385 | * @param string $value |
||
1386 | * @param string $message |
||
1387 | * |
||
1388 | * @throws InvalidArgumentException |
||
1389 | */ |
||
1390 | 12 | View Code Duplication | public static function file($value, $message = '') |
1401 | |||
1402 | /** |
||
1403 | * @param string $value |
||
1404 | * @param string $message |
||
1405 | * |
||
1406 | * @throws InvalidArgumentException |
||
1407 | */ |
||
1408 | 12 | View Code Duplication | public static function directory($value, $message = '') |
1419 | |||
1420 | /** |
||
1421 | * @param string $value |
||
1422 | * @param string $message |
||
1423 | * |
||
1424 | * @throws InvalidArgumentException |
||
1425 | */ |
||
1426 | View Code Duplication | public static function readable($value, $message = '') |
|
1437 | |||
1438 | /** |
||
1439 | * @param string $value |
||
1440 | * @param string $message |
||
1441 | * |
||
1442 | * @throws InvalidArgumentException |
||
1443 | */ |
||
1444 | View Code Duplication | public static function writable($value, $message = '') |
|
1455 | |||
1456 | /** |
||
1457 | * @psalm-assert class-string $value |
||
1458 | * |
||
1459 | * @param string $value |
||
1460 | * @param string $message |
||
1461 | * |
||
1462 | * @throws InvalidArgumentException |
||
1463 | */ |
||
1464 | 12 | View Code Duplication | public static function classExists($value, $message = '') |
1475 | |||
1476 | /** |
||
1477 | * @param object|string $value |
||
1478 | * @param string $class |
||
1479 | * @param string $message |
||
1480 | * @psalm-param class-string $class |
||
1481 | * |
||
1482 | * @throws InvalidArgumentException |
||
1483 | */ |
||
1484 | 8 | public static function subclassOf($value, $class, $message = '') |
|
1494 | |||
1495 | /** |
||
1496 | * @psalm-assert class-string $value |
||
1497 | * |
||
1498 | * @param string $value |
||
1499 | * @param string $message |
||
1500 | * |
||
1501 | * @throws InvalidArgumentException |
||
1502 | */ |
||
1503 | 8 | View Code Duplication | public static function interfaceExists($value, $message = '') |
1514 | |||
1515 | /** |
||
1516 | * @param object|string $value |
||
1517 | * @param mixed $interface |
||
1518 | * @param string $message |
||
1519 | * |
||
1520 | * @throws InvalidArgumentException |
||
1521 | */ |
||
1522 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
1532 | |||
1533 | /** |
||
1534 | * @psalm-param class-string|object $classOrObject |
||
1535 | * @param string|object $classOrObject |
||
1536 | * @param string $property |
||
1537 | * @param string $message |
||
1538 | * |
||
1539 | * @throws InvalidArgumentException |
||
1540 | */ |
||
1541 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1550 | |||
1551 | /** |
||
1552 | * @psalm-param class-string|object $classOrObject |
||
1553 | * @param string|object $classOrObject |
||
1554 | * @param string $property |
||
1555 | * @param string $message |
||
1556 | * |
||
1557 | * @throws InvalidArgumentException |
||
1558 | */ |
||
1559 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
1568 | |||
1569 | /** |
||
1570 | * @psalm-param class-string|object $classOrObject |
||
1571 | * @param string|object $classOrObject |
||
1572 | * @param string $method |
||
1573 | * @param string $message |
||
1574 | * |
||
1575 | * @throws InvalidArgumentException |
||
1576 | */ |
||
1577 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1586 | |||
1587 | /** |
||
1588 | * @psalm-param class-string|object $classOrObject |
||
1589 | * @param string|object $classOrObject |
||
1590 | * @param string $method |
||
1591 | * @param string $message |
||
1592 | * |
||
1593 | * @throws InvalidArgumentException |
||
1594 | */ |
||
1595 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
1604 | |||
1605 | /** |
||
1606 | * @param array $array |
||
1607 | * @param string|int $key |
||
1608 | * @param string $message |
||
1609 | * |
||
1610 | * @throws InvalidArgumentException |
||
1611 | */ |
||
1612 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
1621 | |||
1622 | /** |
||
1623 | * @param array $array |
||
1624 | * @param string|int $key |
||
1625 | * @param string $message |
||
1626 | * |
||
1627 | * @throws InvalidArgumentException |
||
1628 | */ |
||
1629 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1638 | |||
1639 | /** |
||
1640 | * Checks if a value is a valid array key (int or string). |
||
1641 | * |
||
1642 | * @psalm-assert array-key $value |
||
1643 | * |
||
1644 | * @param mixed $value |
||
1645 | * @param string $message |
||
1646 | * |
||
1647 | * @throws InvalidArgumentException |
||
1648 | */ |
||
1649 | 28 | public static function validArrayKey($value, $message = '') |
|
1658 | |||
1659 | /** |
||
1660 | * @param array|Countable $array |
||
1661 | * @param int $number |
||
1662 | * @param string $message |
||
1663 | * |
||
1664 | * @throws InvalidArgumentException |
||
1665 | */ |
||
1666 | 12 | public static function count($array, $number, $message = '') |
|
1676 | |||
1677 | /** |
||
1678 | * @param mixed $array |
||
1679 | * @param int|float $min |
||
1680 | * @param string $message |
||
1681 | * |
||
1682 | * @throws InvalidArgumentException |
||
1683 | */ |
||
1684 | 16 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1696 | |||
1697 | /** |
||
1698 | * @param mixed $array |
||
1699 | * @param int|float $max |
||
1700 | * @param string $message |
||
1701 | * |
||
1702 | * @throws InvalidArgumentException |
||
1703 | */ |
||
1704 | 16 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1716 | |||
1717 | /** |
||
1718 | * @param mixed $array |
||
1719 | * @param int|float $min |
||
1720 | * @param int|float $max |
||
1721 | * @param string $message |
||
1722 | * |
||
1723 | * @throws InvalidArgumentException |
||
1724 | */ |
||
1725 | 24 | public static function countBetween($array, $min, $max, $message = '') |
|
1740 | |||
1741 | /** |
||
1742 | * @psalm-assert list $array |
||
1743 | * |
||
1744 | * @param mixed $array |
||
1745 | * @param string $message |
||
1746 | * |
||
1747 | * @throws InvalidArgumentException |
||
1748 | */ |
||
1749 | 24 | public static function isList($array, $message = '') |
|
1757 | |||
1758 | /** |
||
1759 | * @param mixed $array |
||
1760 | * @param string $message |
||
1761 | * |
||
1762 | * @throws InvalidArgumentException |
||
1763 | */ |
||
1764 | 16 | public static function isMap($array, $message = '') |
|
1778 | |||
1779 | /** |
||
1780 | * @param string $value |
||
1781 | * @param string $message |
||
1782 | * |
||
1783 | * @throws InvalidArgumentException |
||
1784 | */ |
||
1785 | 64 | public static function uuid($value, $message = '') |
|
1804 | |||
1805 | /** |
||
1806 | * @param Closure $expression |
||
1807 | * @param string|object $class |
||
1808 | * @param string $message |
||
1809 | * |
||
1810 | * @throws InvalidArgumentException |
||
1811 | */ |
||
1812 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
1838 | |||
1839 | /** |
||
1840 | * @param string $name |
||
1841 | * @param array $arguments |
||
1842 | * @psalm-param list<mixed> $arguments |
||
1843 | * |
||
1844 | * |
||
1845 | * @throws BadMethodCallException |
||
1846 | */ |
||
1847 | 1513 | public static function __callStatic($name, $arguments) |
|
1877 | |||
1878 | /** |
||
1879 | * @param mixed $value |
||
1880 | * |
||
1881 | * @return string |
||
1882 | */ |
||
1883 | 645 | protected static function valueToString($value) |
|
1919 | |||
1920 | /** |
||
1921 | * @param mixed $value |
||
1922 | * |
||
1923 | * @return string |
||
1924 | */ |
||
1925 | 372 | protected static function typeToString($value) |
|
1929 | |||
1930 | /** |
||
1931 | * @param string $value |
||
1932 | * |
||
1933 | * @return int |
||
1934 | */ |
||
1935 | 168 | protected static function strlen($value) |
|
1947 | |||
1948 | /** |
||
1949 | * @param string $message |
||
1950 | * |
||
1951 | * @throws InvalidArgumentException |
||
1952 | * |
||
1953 | * @psalm-return never-return |
||
1954 | */ |
||
1955 | 1072 | protected static function reportInvalidArgument($message) |
|
1959 | |||
1960 | private function __construct() |
||
1963 | } |
||
1964 |