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 |
||
201 | class Assert |
||
|
|||
202 | { |
||
203 | /** |
||
204 | * @psalm-assert string $value |
||
205 | * |
||
206 | * @param mixed $value |
||
207 | * @param string $message |
||
208 | * |
||
209 | * @throws InvalidArgumentException |
||
210 | */ |
||
211 | 350 | public static function string($value, $message = '') |
|
212 | { |
||
213 | 350 | if (!\is_string($value)) { |
|
214 | 98 | static::reportInvalidArgument(\sprintf( |
|
215 | 98 | $message ?: 'Expected a string. Got: %s', |
|
216 | 98 | static::typeToString($value) |
|
217 | )); |
||
218 | } |
||
219 | 252 | } |
|
220 | |||
221 | /** |
||
222 | * @param mixed $value |
||
223 | * @param string $message |
||
224 | * |
||
225 | * @throws InvalidArgumentException |
||
226 | */ |
||
227 | 16 | public static function stringNotEmpty($value, $message = '') |
|
228 | { |
||
229 | 16 | static::string($value, $message); |
|
230 | 12 | static::notEq($value, '', $message); |
|
231 | 8 | } |
|
232 | |||
233 | /** |
||
234 | * @psalm-assert int $value |
||
235 | * |
||
236 | * @param mixed $value |
||
237 | * @param string $message |
||
238 | * |
||
239 | * @throws InvalidArgumentException |
||
240 | */ |
||
241 | 17 | public static function integer($value, $message = '') |
|
242 | { |
||
243 | 17 | if (!\is_int($value)) { |
|
244 | 13 | static::reportInvalidArgument(\sprintf( |
|
245 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
246 | 13 | static::typeToString($value) |
|
247 | )); |
||
248 | } |
||
249 | 4 | } |
|
250 | |||
251 | /** |
||
252 | * @psalm-assert numeric $value |
||
253 | * |
||
254 | * @param mixed $value |
||
255 | * @param string $message |
||
256 | * |
||
257 | * @throws InvalidArgumentException |
||
258 | */ |
||
259 | 16 | public static function integerish($value, $message = '') |
|
260 | { |
||
261 | 16 | if (!\is_numeric($value) || $value != (int) $value) { |
|
262 | 4 | static::reportInvalidArgument(\sprintf( |
|
263 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
264 | 4 | static::typeToString($value) |
|
265 | )); |
||
266 | } |
||
267 | 12 | } |
|
268 | |||
269 | /** |
||
270 | * @psalm-assert float $value |
||
271 | * |
||
272 | * @param mixed $value |
||
273 | * @param string $message |
||
274 | * |
||
275 | * @throws InvalidArgumentException |
||
276 | */ |
||
277 | 16 | public static function float($value, $message = '') |
|
278 | { |
||
279 | 16 | if (!\is_float($value)) { |
|
280 | 8 | static::reportInvalidArgument(\sprintf( |
|
281 | 8 | $message ?: 'Expected a float. Got: %s', |
|
282 | 8 | static::typeToString($value) |
|
283 | )); |
||
284 | } |
||
285 | 8 | } |
|
286 | |||
287 | /** |
||
288 | * @psalm-assert numeric $value |
||
289 | * |
||
290 | * @param mixed $value |
||
291 | * @param string $message |
||
292 | * |
||
293 | * @throws InvalidArgumentException |
||
294 | */ |
||
295 | 20 | public static function numeric($value, $message = '') |
|
296 | { |
||
297 | 20 | if (!\is_numeric($value)) { |
|
298 | 4 | static::reportInvalidArgument(\sprintf( |
|
299 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
300 | 4 | static::typeToString($value) |
|
301 | )); |
||
302 | } |
||
303 | 16 | } |
|
304 | |||
305 | /** |
||
306 | * @psalm-assert int $value |
||
307 | * |
||
308 | * @param mixed $value |
||
309 | * @param string $message |
||
310 | * |
||
311 | * @throws InvalidArgumentException |
||
312 | */ |
||
313 | 24 | public static function natural($value, $message = '') |
|
314 | { |
||
315 | 24 | if (!\is_int($value) || $value < 0) { |
|
316 | 16 | static::reportInvalidArgument(\sprintf( |
|
317 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
318 | 16 | static::valueToString($value) |
|
319 | )); |
||
320 | } |
||
321 | 8 | } |
|
322 | |||
323 | /** |
||
324 | * @psalm-assert bool $value |
||
325 | * |
||
326 | * @param mixed $value |
||
327 | * @param string $message |
||
328 | * |
||
329 | * @throws InvalidArgumentException |
||
330 | */ |
||
331 | 16 | public static function boolean($value, $message = '') |
|
332 | { |
||
333 | 16 | if (!\is_bool($value)) { |
|
334 | 8 | static::reportInvalidArgument(\sprintf( |
|
335 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
336 | 8 | static::typeToString($value) |
|
337 | )); |
||
338 | } |
||
339 | 8 | } |
|
340 | |||
341 | /** |
||
342 | * @psalm-assert scalar $value |
||
343 | * |
||
344 | * @param mixed $value |
||
345 | * @param string $message |
||
346 | * |
||
347 | * @throws InvalidArgumentException |
||
348 | */ |
||
349 | 23 | public static function scalar($value, $message = '') |
|
350 | { |
||
351 | 23 | if (!\is_scalar($value)) { |
|
352 | 11 | static::reportInvalidArgument(\sprintf( |
|
353 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
354 | 11 | static::typeToString($value) |
|
355 | )); |
||
356 | } |
||
357 | 12 | } |
|
358 | |||
359 | /** |
||
360 | * @psalm-assert object $value |
||
361 | * |
||
362 | * @param mixed $value |
||
363 | * @param string $message |
||
364 | * |
||
365 | * @throws InvalidArgumentException |
||
366 | */ |
||
367 | 23 | public static function object($value, $message = '') |
|
368 | { |
||
369 | 23 | if (!\is_object($value)) { |
|
370 | 15 | static::reportInvalidArgument(\sprintf( |
|
371 | 15 | $message ?: 'Expected an object. Got: %s', |
|
372 | 15 | static::typeToString($value) |
|
373 | )); |
||
374 | } |
||
375 | 8 | } |
|
376 | |||
377 | /** |
||
378 | * @psalm-assert resource $value |
||
379 | * |
||
380 | * @param mixed $value |
||
381 | * @param string|null $type type of resource this should be. @see https://www.php.net/manual/en/function.get-resource-type.php |
||
382 | * @param string $message |
||
383 | * |
||
384 | * @throws InvalidArgumentException |
||
385 | */ |
||
386 | 16 | public static function resource($value, $type = null, $message = '') |
|
387 | { |
||
388 | 16 | if (!\is_resource($value)) { |
|
389 | 4 | static::reportInvalidArgument(\sprintf( |
|
390 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
391 | 4 | static::typeToString($value) |
|
392 | )); |
||
393 | } |
||
394 | |||
395 | 12 | if ($type && $type !== \get_resource_type($value)) { |
|
396 | 4 | static::reportInvalidArgument(\sprintf( |
|
397 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
398 | 4 | static::typeToString($value), |
|
399 | 4 | $type |
|
400 | )); |
||
401 | } |
||
402 | 8 | } |
|
403 | |||
404 | /** |
||
405 | * @psalm-assert callable $value |
||
406 | * |
||
407 | * @param mixed $value |
||
408 | * @param string $message |
||
409 | * |
||
410 | * @throws InvalidArgumentException |
||
411 | */ |
||
412 | 20 | public static function isCallable($value, $message = '') |
|
413 | { |
||
414 | 20 | if (!\is_callable($value)) { |
|
415 | 8 | static::reportInvalidArgument(\sprintf( |
|
416 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
417 | 8 | static::typeToString($value) |
|
418 | )); |
||
419 | } |
||
420 | 12 | } |
|
421 | |||
422 | /** |
||
423 | * @psalm-assert array $value |
||
424 | * |
||
425 | * @param mixed $value |
||
426 | * @param string $message |
||
427 | * |
||
428 | * @throws InvalidArgumentException |
||
429 | */ |
||
430 | 20 | public static function isArray($value, $message = '') |
|
431 | { |
||
432 | 20 | if (!\is_array($value)) { |
|
433 | 12 | static::reportInvalidArgument(\sprintf( |
|
434 | 12 | $message ?: 'Expected an array. Got: %s', |
|
435 | 12 | static::typeToString($value) |
|
436 | )); |
||
437 | } |
||
438 | 8 | } |
|
439 | |||
440 | /** |
||
441 | * @psalm-assert iterable $value |
||
442 | * |
||
443 | * @deprecated use "isIterable" or "isInstanceOf" instead |
||
444 | * |
||
445 | * @param mixed $value |
||
446 | * @param string $message |
||
447 | * |
||
448 | * @throws InvalidArgumentException |
||
449 | */ |
||
450 | 20 | public static function isTraversable($value, $message = '') |
|
451 | { |
||
452 | 20 | @\trigger_error( |
|
453 | 20 | \sprintf( |
|
454 | 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.', |
|
455 | 20 | __METHOD__ |
|
456 | ), |
||
457 | 20 | \E_USER_DEPRECATED |
|
458 | ); |
||
459 | |||
460 | 20 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
461 | 8 | static::reportInvalidArgument(\sprintf( |
|
462 | 8 | $message ?: 'Expected a traversable. Got: %s', |
|
463 | 8 | static::typeToString($value) |
|
464 | )); |
||
465 | } |
||
466 | 12 | } |
|
467 | |||
468 | /** |
||
469 | * @param mixed $value |
||
470 | * @param string $message |
||
471 | * |
||
472 | * @throws InvalidArgumentException |
||
473 | */ |
||
474 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
483 | |||
484 | /** |
||
485 | * @psalm-assert countable $value |
||
486 | * |
||
487 | * @param mixed $value |
||
488 | * @param string $message |
||
489 | * |
||
490 | * @throws InvalidArgumentException |
||
491 | */ |
||
492 | 28 | public static function isCountable($value, $message = '') |
|
493 | { |
||
494 | if ( |
||
495 | 28 | !\is_array($value) |
|
496 | 28 | && !($value instanceof Countable) |
|
497 | 28 | && !($value instanceof ResourceBundle) |
|
498 | 28 | && !($value instanceof SimpleXMLElement) |
|
499 | ) { |
||
500 | 12 | static::reportInvalidArgument(\sprintf( |
|
501 | 12 | $message ?: 'Expected a countable. Got: %s', |
|
502 | 12 | static::typeToString($value) |
|
503 | )); |
||
504 | } |
||
505 | 16 | } |
|
506 | |||
507 | /** |
||
508 | * @psalm-assert iterable $value |
||
509 | * |
||
510 | * @param mixed $value |
||
511 | * @param string $message |
||
512 | * |
||
513 | * @throws InvalidArgumentException |
||
514 | */ |
||
515 | 950 | View Code Duplication | public static function isIterable($value, $message = '') |
516 | { |
||
517 | 950 | if (!\is_array($value) && !($value instanceof Traversable)) { |
|
518 | 8 | static::reportInvalidArgument(\sprintf( |
|
519 | 8 | $message ?: 'Expected an iterable. Got: %s', |
|
520 | 8 | static::typeToString($value) |
|
521 | )); |
||
522 | } |
||
523 | 946 | } |
|
524 | |||
525 | /** |
||
526 | * @psalm-template ExpectedType of object |
||
527 | * @psalm-param class-string<ExpectedType> $class |
||
528 | * @psalm-assert ExpectedType $value |
||
529 | * |
||
530 | * @param mixed $value |
||
531 | * @param string|object $class |
||
532 | * @param string $message |
||
533 | * |
||
534 | * @throws InvalidArgumentException |
||
535 | */ |
||
536 | 19 | public static function isInstanceOf($value, $class, $message = '') |
|
537 | { |
||
538 | 19 | if (!($value instanceof $class)) { |
|
539 | 15 | static::reportInvalidArgument(\sprintf( |
|
540 | 15 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
541 | 15 | static::typeToString($value), |
|
542 | 15 | $class |
|
543 | )); |
||
544 | } |
||
545 | 4 | } |
|
546 | |||
547 | /** |
||
548 | * @psalm-template ExpectedType of object |
||
549 | * @psalm-param class-string<ExpectedType> $class |
||
550 | * @psalm-assert !ExpectedType $value |
||
551 | * |
||
552 | * @param mixed $value |
||
553 | * @param string|object $class |
||
554 | * @param string $message |
||
555 | * |
||
556 | * @throws InvalidArgumentException |
||
557 | */ |
||
558 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
559 | { |
||
560 | 16 | if ($value instanceof $class) { |
|
561 | 4 | static::reportInvalidArgument(\sprintf( |
|
562 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
563 | 4 | static::typeToString($value), |
|
564 | 4 | $class |
|
565 | )); |
||
566 | } |
||
567 | 12 | } |
|
568 | |||
569 | /** |
||
570 | * @param mixed $value |
||
571 | * @param array<object|string> $classes |
||
572 | * @param string $message |
||
573 | * |
||
574 | * @throws InvalidArgumentException |
||
575 | */ |
||
576 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
577 | { |
||
578 | 20 | foreach ($classes as $class) { |
|
579 | 20 | if ($value instanceof $class) { |
|
580 | 8 | return; |
|
581 | } |
||
582 | } |
||
583 | |||
584 | 12 | static::reportInvalidArgument(\sprintf( |
|
585 | 12 | $message ?: 'Expected an instance of any of %2$s. Got: %s', |
|
586 | 12 | static::typeToString($value), |
|
587 | 12 | \implode(', ', \array_map(array('static', 'valueToString'), $classes)) |
|
588 | )); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * @psalm-assert empty $value |
||
593 | * |
||
594 | * @param mixed $value |
||
595 | * @param string $message |
||
596 | * |
||
597 | * @throws InvalidArgumentException |
||
598 | */ |
||
599 | 23 | public static function isEmpty($value, $message = '') |
|
600 | { |
||
601 | 23 | if (!empty($value)) { |
|
602 | 8 | static::reportInvalidArgument(\sprintf( |
|
603 | 8 | $message ?: 'Expected an empty value. Got: %s', |
|
604 | 8 | static::valueToString($value) |
|
605 | )); |
||
606 | } |
||
607 | 15 | } |
|
608 | |||
609 | /** |
||
610 | * @psalm-assert !empty $value |
||
611 | * |
||
612 | * @param mixed $value |
||
613 | * @param string $message |
||
614 | * |
||
615 | * @throws InvalidArgumentException |
||
616 | */ |
||
617 | 55 | public static function notEmpty($value, $message = '') |
|
618 | { |
||
619 | 55 | if (empty($value)) { |
|
620 | 23 | static::reportInvalidArgument(\sprintf( |
|
621 | 23 | $message ?: 'Expected a non-empty value. Got: %s', |
|
622 | 23 | static::valueToString($value) |
|
623 | )); |
||
624 | } |
||
625 | 32 | } |
|
626 | |||
627 | /** |
||
628 | * @psalm-assert null $value |
||
629 | * |
||
630 | * @param mixed $value |
||
631 | * @param string $message |
||
632 | * |
||
633 | * @throws InvalidArgumentException |
||
634 | */ |
||
635 | 11 | public static function null($value, $message = '') |
|
636 | { |
||
637 | 11 | if (null !== $value) { |
|
638 | 8 | static::reportInvalidArgument(\sprintf( |
|
639 | 8 | $message ?: 'Expected null. Got: %s', |
|
640 | 8 | static::valueToString($value) |
|
641 | )); |
||
642 | } |
||
643 | 3 | } |
|
644 | |||
645 | /** |
||
646 | * @psalm-assert !null $value |
||
647 | * |
||
648 | * @param mixed $value |
||
649 | * @param string $message |
||
650 | * |
||
651 | * @throws InvalidArgumentException |
||
652 | */ |
||
653 | 11 | public static function notNull($value, $message = '') |
|
654 | { |
||
655 | 11 | if (null === $value) { |
|
656 | 3 | static::reportInvalidArgument( |
|
657 | 3 | $message ?: 'Expected a value other than null.' |
|
658 | ); |
||
659 | } |
||
660 | 8 | } |
|
661 | |||
662 | /** |
||
663 | * @psalm-assert true $value |
||
664 | * |
||
665 | * @param mixed $value |
||
666 | * @param string $message |
||
667 | * |
||
668 | * @throws InvalidArgumentException |
||
669 | */ |
||
670 | 15 | public static function true($value, $message = '') |
|
671 | { |
||
672 | 15 | if (true !== $value) { |
|
673 | 11 | static::reportInvalidArgument(\sprintf( |
|
674 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
675 | 11 | static::valueToString($value) |
|
676 | )); |
||
677 | } |
||
678 | 4 | } |
|
679 | |||
680 | /** |
||
681 | * @psalm-assert false $value |
||
682 | * |
||
683 | * @param mixed $value |
||
684 | * @param string $message |
||
685 | * |
||
686 | * @throws InvalidArgumentException |
||
687 | */ |
||
688 | 19 | public static function false($value, $message = '') |
|
689 | { |
||
690 | 19 | if (false !== $value) { |
|
691 | 15 | static::reportInvalidArgument(\sprintf( |
|
692 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
693 | 15 | static::valueToString($value) |
|
694 | )); |
||
695 | } |
||
696 | 4 | } |
|
697 | |||
698 | /** |
||
699 | * @param mixed $value |
||
700 | * @param string $message |
||
701 | * |
||
702 | * @throws InvalidArgumentException |
||
703 | */ |
||
704 | 51 | View Code Duplication | public static function ip($value, $message = '') |
714 | |||
715 | /** |
||
716 | * @param mixed $value |
||
717 | * @param string $message |
||
718 | * |
||
719 | * @throws InvalidArgumentException |
||
720 | */ |
||
721 | 51 | View Code Duplication | public static function ipv4($value, $message = '') |
722 | { |
||
723 | 51 | static::string($value, $message); |
|
724 | 32 | if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
|
725 | 20 | static::reportInvalidArgument(\sprintf( |
|
726 | 20 | $message ?: 'Expected a value to be an IPv4. Got: %s', |
|
727 | 20 | static::valueToString($value) |
|
728 | )); |
||
729 | } |
||
730 | 12 | } |
|
731 | |||
732 | /** |
||
733 | * @param mixed $value |
||
734 | * @param string $message |
||
735 | * |
||
736 | * @throws InvalidArgumentException |
||
737 | */ |
||
738 | 51 | View Code Duplication | public static function ipv6($value, $message = '') |
739 | { |
||
740 | 51 | static::string($value, $message); |
|
748 | |||
749 | /** |
||
750 | * @param mixed $value |
||
751 | * @param string $message |
||
752 | * |
||
753 | * @throws InvalidArgumentException |
||
754 | */ |
||
755 | 20 | View Code Duplication | public static function email($value, $message = '') |
765 | |||
766 | /** |
||
767 | * Does non strict comparisons on the items, so ['3', 3] will not pass the assertion. |
||
768 | * |
||
769 | * @param array $values |
||
770 | * @param string $message |
||
771 | * |
||
772 | * @throws InvalidArgumentException |
||
773 | */ |
||
774 | 12 | public static function uniqueValues(array $values, $message = '') |
|
789 | |||
790 | /** |
||
791 | * @param mixed $value |
||
792 | * @param mixed $expect |
||
793 | * @param string $message |
||
794 | * |
||
795 | * @throws InvalidArgumentException |
||
796 | */ |
||
797 | 33 | public static function eq($value, $expect, $message = '') |
|
807 | |||
808 | /** |
||
809 | * @param mixed $value |
||
810 | * @param mixed $expect |
||
811 | * @param string $message |
||
812 | * |
||
813 | * @throws InvalidArgumentException |
||
814 | */ |
||
815 | 28 | public static function notEq($value, $expect, $message = '') |
|
824 | |||
825 | /** |
||
826 | * @param mixed $value |
||
827 | * @param mixed $expect |
||
828 | * @param string $message |
||
829 | * |
||
830 | * @throws InvalidArgumentException |
||
831 | */ |
||
832 | 16 | public static function same($value, $expect, $message = '') |
|
842 | |||
843 | /** |
||
844 | * @param mixed $value |
||
845 | * @param mixed $expect |
||
846 | * @param string $message |
||
847 | * |
||
848 | * @throws InvalidArgumentException |
||
849 | */ |
||
850 | 16 | public static function notSame($value, $expect, $message = '') |
|
859 | |||
860 | /** |
||
861 | * @param mixed $value |
||
862 | * @param mixed $limit |
||
863 | * @param string $message |
||
864 | * |
||
865 | * @throws InvalidArgumentException |
||
866 | */ |
||
867 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
877 | |||
878 | /** |
||
879 | * @param mixed $value |
||
880 | * @param mixed $limit |
||
881 | * @param string $message |
||
882 | * |
||
883 | * @throws InvalidArgumentException |
||
884 | */ |
||
885 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
895 | |||
896 | /** |
||
897 | * @param mixed $value |
||
898 | * @param mixed $limit |
||
899 | * @param string $message |
||
900 | * |
||
901 | * @throws InvalidArgumentException |
||
902 | */ |
||
903 | 8 | public static function lessThan($value, $limit, $message = '') |
|
913 | |||
914 | /** |
||
915 | * @param mixed $value |
||
916 | * @param mixed $limit |
||
917 | * @param string $message |
||
918 | * |
||
919 | * @throws InvalidArgumentException |
||
920 | */ |
||
921 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
931 | |||
932 | /** |
||
933 | * Inclusive range, so Assert::(3, 3, 5) passes. |
||
934 | * |
||
935 | * @param mixed $value |
||
936 | * @param mixed $min |
||
937 | * @param mixed $max |
||
938 | * @param string $message |
||
939 | * |
||
940 | * @throws InvalidArgumentException |
||
941 | */ |
||
942 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
953 | |||
954 | /** |
||
955 | * Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion. |
||
956 | * |
||
957 | * @param mixed $value |
||
958 | * @param array $values |
||
959 | * @param string $message |
||
960 | * |
||
961 | * @throws InvalidArgumentException |
||
962 | */ |
||
963 | 8 | public static function oneOf($value, array $values, $message = '') |
|
973 | |||
974 | /** |
||
975 | * @param mixed $value |
||
976 | * @param string $subString |
||
977 | * @param string $message |
||
978 | * |
||
979 | * @throws InvalidArgumentException |
||
980 | */ |
||
981 | 80 | View Code Duplication | public static function contains($value, $subString, $message = '') |
991 | |||
992 | /** |
||
993 | * @param mixed $value |
||
994 | * @param string $subString |
||
995 | * @param string $message |
||
996 | * |
||
997 | * @throws InvalidArgumentException |
||
998 | */ |
||
999 | 80 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
1009 | |||
1010 | /** |
||
1011 | * @param mixed $value |
||
1012 | * @param string $message |
||
1013 | * |
||
1014 | * @throws InvalidArgumentException |
||
1015 | */ |
||
1016 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
1025 | |||
1026 | /** |
||
1027 | * @param mixed $value |
||
1028 | * @param string $prefix |
||
1029 | * @param string $message |
||
1030 | * |
||
1031 | * @throws InvalidArgumentException |
||
1032 | */ |
||
1033 | 48 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
1043 | |||
1044 | /** |
||
1045 | * @param mixed $value |
||
1046 | * @param string $message |
||
1047 | * |
||
1048 | * @throws InvalidArgumentException |
||
1049 | */ |
||
1050 | 35 | public static function startsWithLetter($value, $message = '') |
|
1070 | |||
1071 | /** |
||
1072 | * @param mixed $value |
||
1073 | * @param string $suffix |
||
1074 | * @param string $message |
||
1075 | * |
||
1076 | * @throws InvalidArgumentException |
||
1077 | */ |
||
1078 | 48 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
1088 | |||
1089 | /** |
||
1090 | * @param mixed $value |
||
1091 | * @param mixed $pattern |
||
1092 | * @param string $message |
||
1093 | * |
||
1094 | * @throws InvalidArgumentException |
||
1095 | */ |
||
1096 | 12 | public static function regex($value, $pattern, $message = '') |
|
1105 | |||
1106 | /** |
||
1107 | * @param mixed $value |
||
1108 | * @param mixed $pattern |
||
1109 | * @param string $message |
||
1110 | * |
||
1111 | * @throws InvalidArgumentException |
||
1112 | */ |
||
1113 | 12 | public static function notRegex($value, $pattern, $message = '') |
|
1124 | |||
1125 | /** |
||
1126 | * @param mixed $value |
||
1127 | * @param string $message |
||
1128 | * |
||
1129 | * @throws InvalidArgumentException |
||
1130 | */ |
||
1131 | 28 | View Code Duplication | public static function unicodeLetters($value, $message = '') |
1142 | |||
1143 | /** |
||
1144 | * @param mixed $value |
||
1145 | * @param string $message |
||
1146 | * |
||
1147 | * @throws InvalidArgumentException |
||
1148 | */ |
||
1149 | 20 | View Code Duplication | public static function alpha($value, $message = '') |
1165 | |||
1166 | /** |
||
1167 | * @param mixed $value |
||
1168 | * @param string $message |
||
1169 | * |
||
1170 | * @throws InvalidArgumentException |
||
1171 | */ |
||
1172 | 12 | View Code Duplication | public static function digits($value, $message = '') |
1186 | |||
1187 | /** |
||
1188 | * @param mixed $value |
||
1189 | * @param string $message |
||
1190 | * |
||
1191 | * @throws InvalidArgumentException |
||
1192 | */ |
||
1193 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
1207 | |||
1208 | /** |
||
1209 | * @param mixed $value |
||
1210 | * @param string $message |
||
1211 | * |
||
1212 | * @throws InvalidArgumentException |
||
1213 | */ |
||
1214 | 16 | View Code Duplication | public static function lower($value, $message = '') |
1228 | |||
1229 | /** |
||
1230 | * @param mixed $value |
||
1231 | * @param string $message |
||
1232 | * |
||
1233 | * @throws InvalidArgumentException |
||
1234 | */ |
||
1235 | 16 | View Code Duplication | public static function upper($value, $message = '') |
1249 | |||
1250 | /** |
||
1251 | * @param mixed $value |
||
1252 | * @param mixed $length |
||
1253 | * @param string $message |
||
1254 | * |
||
1255 | * @throws InvalidArgumentException |
||
1256 | */ |
||
1257 | 36 | public static function length($value, $length, $message = '') |
|
1267 | |||
1268 | /** |
||
1269 | * Inclusive min. |
||
1270 | * |
||
1271 | * @param mixed $value |
||
1272 | * @param mixed $min |
||
1273 | * @param string $message |
||
1274 | * |
||
1275 | * @throws InvalidArgumentException |
||
1276 | */ |
||
1277 | 36 | public static function minLength($value, $min, $message = '') |
|
1287 | |||
1288 | /** |
||
1289 | * Inclusive max. |
||
1290 | * |
||
1291 | * @param mixed $value |
||
1292 | * @param mixed $max |
||
1293 | * @param string $message |
||
1294 | * |
||
1295 | * @throws InvalidArgumentException |
||
1296 | */ |
||
1297 | 36 | public static function maxLength($value, $max, $message = '') |
|
1307 | |||
1308 | /** |
||
1309 | * Inclusive , so Assert::lengthBetween('asd', 3, 5); passes the assertion. |
||
1310 | * |
||
1311 | * @param mixed $value |
||
1312 | * @param mixed $min |
||
1313 | * @param mixed $max |
||
1314 | * @param string $message |
||
1315 | * |
||
1316 | * @throws InvalidArgumentException |
||
1317 | */ |
||
1318 | 60 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
1331 | |||
1332 | /** |
||
1333 | * Will also pass if $value is a directory, use Assert::file() instead if you need to be sure it is a file. |
||
1334 | * |
||
1335 | * @param mixed $value |
||
1336 | * @param string $message |
||
1337 | * |
||
1338 | * @throws InvalidArgumentException |
||
1339 | */ |
||
1340 | 36 | View Code Duplication | public static function fileExists($value, $message = '') |
1351 | |||
1352 | /** |
||
1353 | * @param mixed $value |
||
1354 | * @param string $message |
||
1355 | * |
||
1356 | * @throws InvalidArgumentException |
||
1357 | */ |
||
1358 | 12 | View Code Duplication | public static function file($value, $message = '') |
1369 | |||
1370 | /** |
||
1371 | * @param mixed $value |
||
1372 | * @param string $message |
||
1373 | * |
||
1374 | * @throws InvalidArgumentException |
||
1375 | */ |
||
1376 | 12 | View Code Duplication | public static function directory($value, $message = '') |
1387 | |||
1388 | /** |
||
1389 | * @param mixed $value |
||
1390 | * @param string $message |
||
1391 | * |
||
1392 | * @throws InvalidArgumentException |
||
1393 | */ |
||
1394 | public static function readable($value, $message = '') |
||
1403 | |||
1404 | /** |
||
1405 | * @param mixed $value |
||
1406 | * @param string $message |
||
1407 | * |
||
1408 | * @throws InvalidArgumentException |
||
1409 | */ |
||
1410 | public static function writable($value, $message = '') |
||
1419 | |||
1420 | /** |
||
1421 | * @psalm-assert class-string $value |
||
1422 | * |
||
1423 | * @param mixed $value |
||
1424 | * @param string $message |
||
1425 | * |
||
1426 | * @throws InvalidArgumentException |
||
1427 | */ |
||
1428 | 8 | public static function classExists($value, $message = '') |
|
1437 | |||
1438 | /** |
||
1439 | * @param mixed $value |
||
1440 | * @param string|object $class |
||
1441 | * @param string $message |
||
1442 | * |
||
1443 | * @throws InvalidArgumentException |
||
1444 | */ |
||
1445 | 8 | public static function subclassOf($value, $class, $message = '') |
|
1455 | |||
1456 | /** |
||
1457 | * @psalm-assert class-string $value |
||
1458 | * |
||
1459 | * @param mixed $value |
||
1460 | * @param string $message |
||
1461 | * |
||
1462 | * @throws InvalidArgumentException |
||
1463 | */ |
||
1464 | 8 | public static function interfaceExists($value, $message = '') |
|
1473 | |||
1474 | /** |
||
1475 | * @param mixed $value |
||
1476 | * @param mixed $interface |
||
1477 | * @param string $message |
||
1478 | * |
||
1479 | * @throws InvalidArgumentException |
||
1480 | */ |
||
1481 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
1491 | |||
1492 | /** |
||
1493 | * @param string|object $classOrObject |
||
1494 | * @param mixed $property |
||
1495 | * @param string $message |
||
1496 | * |
||
1497 | * @throws InvalidArgumentException |
||
1498 | */ |
||
1499 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
1508 | |||
1509 | /** |
||
1510 | * @param string|object $classOrObject |
||
1511 | * @param mixed $property |
||
1512 | * @param string $message |
||
1513 | * |
||
1514 | * @throws InvalidArgumentException |
||
1515 | */ |
||
1516 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
1525 | |||
1526 | /** |
||
1527 | * @param string|object $classOrObject |
||
1528 | * @param mixed $method |
||
1529 | * @param string $message |
||
1530 | * |
||
1531 | * @throws InvalidArgumentException |
||
1532 | */ |
||
1533 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
1542 | |||
1543 | /** |
||
1544 | * @param string|object $classOrObject |
||
1545 | * @param mixed $method |
||
1546 | * @param string $message |
||
1547 | * |
||
1548 | * @throws InvalidArgumentException |
||
1549 | */ |
||
1550 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
1559 | |||
1560 | /** |
||
1561 | * @param array $array |
||
1562 | * @param string|int $key |
||
1563 | * @param string $message |
||
1564 | * |
||
1565 | * @throws InvalidArgumentException |
||
1566 | */ |
||
1567 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
1576 | |||
1577 | /** |
||
1578 | * @param array $array |
||
1579 | * @param string|int $key |
||
1580 | * @param string $message |
||
1581 | * |
||
1582 | * @throws InvalidArgumentException |
||
1583 | */ |
||
1584 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
1593 | |||
1594 | /** |
||
1595 | * Checks if a value is a valid array key (int or string). |
||
1596 | * |
||
1597 | * @psalm-assert array-key $value |
||
1598 | * |
||
1599 | * @param mixed $value |
||
1600 | * @param string $message |
||
1601 | * |
||
1602 | * @throws InvalidArgumentException |
||
1603 | */ |
||
1604 | 28 | public static function validArrayKey($value, $message = '') |
|
1613 | |||
1614 | /** |
||
1615 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1616 | * |
||
1617 | * @param mixed $array |
||
1618 | * @param mixed $number |
||
1619 | * @param string $message |
||
1620 | * |
||
1621 | * @throws InvalidArgumentException |
||
1622 | */ |
||
1623 | 8 | public static function count($array, $number, $message = '') |
|
1631 | |||
1632 | /** |
||
1633 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1634 | * |
||
1635 | * @param mixed $array |
||
1636 | * @param mixed $min |
||
1637 | * @param string $message |
||
1638 | * |
||
1639 | * @throws InvalidArgumentException |
||
1640 | */ |
||
1641 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
1651 | |||
1652 | /** |
||
1653 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1654 | * |
||
1655 | * @param mixed $array |
||
1656 | * @param mixed $max |
||
1657 | * @param string $message |
||
1658 | * |
||
1659 | * @throws InvalidArgumentException |
||
1660 | */ |
||
1661 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
1671 | |||
1672 | /** |
||
1673 | * Does not check if $array is countable, this can generate a warning on php versions after 7.2. |
||
1674 | * |
||
1675 | * @param mixed $array |
||
1676 | * @param mixed $min |
||
1677 | * @param mixed $max |
||
1678 | * @param string $message |
||
1679 | * |
||
1680 | * @throws InvalidArgumentException |
||
1681 | */ |
||
1682 | 20 | public static function countBetween($array, $min, $max, $message = '') |
|
1695 | |||
1696 | /** |
||
1697 | * @psalm-assert list $array |
||
1698 | * |
||
1699 | * @param mixed $array |
||
1700 | * @param string $message |
||
1701 | * |
||
1702 | * @throws InvalidArgumentException |
||
1703 | */ |
||
1704 | 80 | View Code Duplication | public static function isList($array, $message = '') |
1712 | |||
1713 | /** |
||
1714 | * @psalm-assert non-empty-list $array |
||
1715 | * |
||
1716 | * @param mixed $array |
||
1717 | * @param string $message |
||
1718 | * |
||
1719 | * @throws InvalidArgumentException |
||
1720 | */ |
||
1721 | 40 | public static function isNonEmptyList($array, $message = '') |
|
1726 | |||
1727 | /** |
||
1728 | * @param mixed $array |
||
1729 | * @param string $message |
||
1730 | * |
||
1731 | * @throws InvalidArgumentException |
||
1732 | */ |
||
1733 | 32 | public static function isMap($array, $message = '') |
|
1744 | |||
1745 | /** |
||
1746 | * @param mixed $array |
||
1747 | * @param string $message |
||
1748 | * |
||
1749 | * @throws InvalidArgumentException |
||
1750 | */ |
||
1751 | 16 | public static function isNonEmptyMap($array, $message = '') |
|
1756 | |||
1757 | /** |
||
1758 | * @param mixed $value |
||
1759 | * @param string $message |
||
1760 | * |
||
1761 | * @throws InvalidArgumentException |
||
1762 | */ |
||
1763 | 56 | public static function uuid($value, $message = '') |
|
1780 | |||
1781 | /** |
||
1782 | * @param Closure $expression |
||
1783 | * @param string|object $class |
||
1784 | * @param string $message |
||
1785 | * |
||
1786 | * @throws InvalidArgumentException |
||
1787 | */ |
||
1788 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
1814 | |||
1815 | /** |
||
1816 | * @throws BadMethodCallException |
||
1817 | */ |
||
1818 | 1494 | public static function __callStatic($name, $arguments) |
|
1846 | |||
1847 | /** |
||
1848 | * @param mixed $value |
||
1849 | * |
||
1850 | * @return string |
||
1851 | */ |
||
1852 | 653 | protected static function valueToString($value) |
|
1888 | |||
1889 | /** |
||
1890 | * @param mixed $value |
||
1891 | * |
||
1892 | * @return string |
||
1893 | */ |
||
1894 | 276 | protected static function typeToString($value) |
|
1898 | |||
1899 | 168 | protected static function strlen($value) |
|
1911 | |||
1912 | /** |
||
1913 | * @param string $message |
||
1914 | * |
||
1915 | * @throws InvalidArgumentException |
||
1916 | */ |
||
1917 | 1000 | protected static function reportInvalidArgument($message) |
|
1921 | |||
1922 | private function __construct() |
||
1925 | } |
||
1926 |