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 |
||
| 167 | class Assert |
||
|
|
|||
| 168 | { |
||
| 169 | 94 | public static function string($value, $message = '') |
|
| 178 | |||
| 179 | 16 | public static function stringNotEmpty($value, $message = '') |
|
| 184 | |||
| 185 | 17 | public static function integer($value, $message = '') |
|
| 186 | { |
||
| 187 | 17 | if (!is_int($value)) { |
|
| 188 | 13 | static::reportInvalidArgument(sprintf( |
|
| 189 | 13 | $message ?: 'Expected an integer. Got: %s', |
|
| 190 | 13 | static::typeToString($value) |
|
| 191 | )); |
||
| 192 | } |
||
| 193 | 4 | } |
|
| 194 | |||
| 195 | 16 | public static function integerish($value, $message = '') |
|
| 196 | { |
||
| 197 | 16 | if (!is_numeric($value) || $value != (int) $value) { |
|
| 198 | 4 | static::reportInvalidArgument(sprintf( |
|
| 199 | 4 | $message ?: 'Expected an integerish value. Got: %s', |
|
| 200 | 4 | static::typeToString($value) |
|
| 201 | )); |
||
| 202 | } |
||
| 203 | 12 | } |
|
| 204 | |||
| 205 | 16 | public static function float($value, $message = '') |
|
| 206 | { |
||
| 207 | 16 | if (!is_float($value)) { |
|
| 208 | 8 | static::reportInvalidArgument(sprintf( |
|
| 209 | 8 | $message ?: 'Expected a float. Got: %s', |
|
| 210 | 8 | static::typeToString($value) |
|
| 211 | )); |
||
| 212 | } |
||
| 213 | 8 | } |
|
| 214 | |||
| 215 | 20 | public static function numeric($value, $message = '') |
|
| 216 | { |
||
| 217 | 20 | if (!is_numeric($value)) { |
|
| 218 | 4 | static::reportInvalidArgument(sprintf( |
|
| 219 | 4 | $message ?: 'Expected a numeric. Got: %s', |
|
| 220 | 4 | static::typeToString($value) |
|
| 221 | )); |
||
| 222 | } |
||
| 223 | 16 | } |
|
| 224 | |||
| 225 | 24 | public static function natural($value, $message = '') |
|
| 226 | { |
||
| 227 | 24 | if (!is_int($value) || $value < 0) { |
|
| 228 | 16 | static::reportInvalidArgument(sprintf( |
|
| 229 | 16 | $message ?: 'Expected a non-negative integer. Got %s', |
|
| 230 | 16 | static::valueToString($value) |
|
| 231 | )); |
||
| 232 | } |
||
| 233 | 8 | } |
|
| 234 | |||
| 235 | 16 | public static function boolean($value, $message = '') |
|
| 236 | { |
||
| 237 | 16 | if (!is_bool($value)) { |
|
| 238 | 8 | static::reportInvalidArgument(sprintf( |
|
| 239 | 8 | $message ?: 'Expected a boolean. Got: %s', |
|
| 240 | 8 | static::typeToString($value) |
|
| 241 | )); |
||
| 242 | } |
||
| 243 | 8 | } |
|
| 244 | |||
| 245 | 23 | public static function scalar($value, $message = '') |
|
| 246 | { |
||
| 247 | 23 | if (!is_scalar($value)) { |
|
| 248 | 11 | static::reportInvalidArgument(sprintf( |
|
| 249 | 11 | $message ?: 'Expected a scalar. Got: %s', |
|
| 250 | 11 | static::typeToString($value) |
|
| 251 | )); |
||
| 252 | } |
||
| 253 | 12 | } |
|
| 254 | |||
| 255 | 23 | public static function object($value, $message = '') |
|
| 256 | { |
||
| 257 | 23 | if (!is_object($value)) { |
|
| 258 | 15 | static::reportInvalidArgument(sprintf( |
|
| 259 | 15 | $message ?: 'Expected an object. Got: %s', |
|
| 260 | 15 | static::typeToString($value) |
|
| 261 | )); |
||
| 262 | } |
||
| 263 | 8 | } |
|
| 264 | |||
| 265 | 16 | public static function resource($value, $type = null, $message = '') |
|
| 282 | |||
| 283 | 20 | public static function isCallable($value, $message = '') |
|
| 284 | { |
||
| 285 | 20 | if (!is_callable($value)) { |
|
| 286 | 8 | static::reportInvalidArgument(sprintf( |
|
| 287 | 8 | $message ?: 'Expected a callable. Got: %s', |
|
| 288 | 8 | static::typeToString($value) |
|
| 289 | )); |
||
| 290 | } |
||
| 291 | 12 | } |
|
| 292 | |||
| 293 | 20 | public static function isArray($value, $message = '') |
|
| 294 | { |
||
| 295 | 20 | if (!is_array($value)) { |
|
| 296 | 12 | static::reportInvalidArgument(sprintf( |
|
| 297 | 12 | $message ?: 'Expected an array. Got: %s', |
|
| 298 | 12 | static::typeToString($value) |
|
| 299 | )); |
||
| 300 | } |
||
| 301 | 8 | } |
|
| 302 | |||
| 303 | 20 | public static function isTraversable($value, $message = '') |
|
| 320 | |||
| 321 | 20 | View Code Duplication | public static function isArrayAccessible($value, $message = '') |
| 330 | |||
| 331 | 24 | View Code Duplication | public static function isCountable($value, $message = '') |
| 340 | |||
| 341 | 610 | View Code Duplication | public static function isIterable($value, $message = '') |
| 350 | |||
| 351 | 16 | public static function isInstanceOf($value, $class, $message = '') |
|
| 352 | { |
||
| 353 | 16 | if (!($value instanceof $class)) { |
|
| 354 | 12 | static::reportInvalidArgument(sprintf( |
|
| 355 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
| 356 | 12 | static::typeToString($value), |
|
| 357 | 12 | $class |
|
| 358 | )); |
||
| 359 | } |
||
| 360 | 4 | } |
|
| 361 | |||
| 362 | 16 | public static function notInstanceOf($value, $class, $message = '') |
|
| 363 | { |
||
| 364 | 16 | if ($value instanceof $class) { |
|
| 365 | 4 | static::reportInvalidArgument(sprintf( |
|
| 366 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
| 367 | 4 | static::typeToString($value), |
|
| 368 | 4 | $class |
|
| 369 | )); |
||
| 370 | } |
||
| 371 | 12 | } |
|
| 372 | |||
| 373 | 20 | public static function isInstanceOfAny($value, array $classes, $message = '') |
|
| 387 | |||
| 388 | 23 | public static function isEmpty($value, $message = '') |
|
| 397 | |||
| 398 | 23 | public static function notEmpty($value, $message = '') |
|
| 407 | |||
| 408 | 11 | public static function null($value, $message = '') |
|
| 409 | { |
||
| 410 | 11 | if (null !== $value) { |
|
| 411 | 8 | static::reportInvalidArgument(sprintf( |
|
| 412 | 8 | $message ?: 'Expected null. Got: %s', |
|
| 413 | 8 | static::valueToString($value) |
|
| 414 | )); |
||
| 415 | } |
||
| 416 | 3 | } |
|
| 417 | |||
| 418 | 11 | public static function notNull($value, $message = '') |
|
| 426 | |||
| 427 | 15 | public static function true($value, $message = '') |
|
| 428 | { |
||
| 429 | 15 | if (true !== $value) { |
|
| 430 | 11 | static::reportInvalidArgument(sprintf( |
|
| 431 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
| 432 | 11 | static::valueToString($value) |
|
| 433 | )); |
||
| 434 | } |
||
| 435 | 4 | } |
|
| 436 | |||
| 437 | 19 | public static function false($value, $message = '') |
|
| 438 | { |
||
| 439 | 19 | if (false !== $value) { |
|
| 440 | 15 | static::reportInvalidArgument(sprintf( |
|
| 441 | 15 | $message ?: 'Expected a value to be false. Got: %s', |
|
| 442 | 15 | static::valueToString($value) |
|
| 443 | )); |
||
| 444 | } |
||
| 445 | 4 | } |
|
| 446 | |||
| 447 | 33 | public static function eq($value, $value2, $message = '') |
|
| 448 | { |
||
| 449 | 33 | if ($value2 != $value) { |
|
| 450 | 17 | static::reportInvalidArgument(sprintf( |
|
| 451 | 17 | $message ?: 'Expected a value equal to %2$s. Got: %s', |
|
| 452 | 17 | static::valueToString($value), |
|
| 453 | 17 | static::valueToString($value2) |
|
| 454 | )); |
||
| 455 | } |
||
| 456 | 16 | } |
|
| 457 | |||
| 458 | 28 | public static function notEq($value, $value2, $message = '') |
|
| 467 | |||
| 468 | 16 | public static function same($value, $value2, $message = '') |
|
| 469 | { |
||
| 470 | 16 | if ($value2 !== $value) { |
|
| 471 | 12 | static::reportInvalidArgument(sprintf( |
|
| 472 | 12 | $message ?: 'Expected a value identical to %2$s. Got: %s', |
|
| 473 | 12 | static::valueToString($value), |
|
| 474 | 12 | static::valueToString($value2) |
|
| 475 | )); |
||
| 476 | } |
||
| 477 | 4 | } |
|
| 478 | |||
| 479 | 16 | public static function notSame($value, $value2, $message = '') |
|
| 488 | |||
| 489 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
| 490 | { |
||
| 491 | 8 | if ($value <= $limit) { |
|
| 492 | 4 | static::reportInvalidArgument(sprintf( |
|
| 493 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
| 494 | 4 | static::valueToString($value), |
|
| 495 | 4 | static::valueToString($limit) |
|
| 496 | )); |
||
| 497 | } |
||
| 498 | 4 | } |
|
| 499 | |||
| 500 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
| 501 | { |
||
| 502 | 12 | if ($value < $limit) { |
|
| 503 | 4 | static::reportInvalidArgument(sprintf( |
|
| 504 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
| 505 | 4 | static::valueToString($value), |
|
| 506 | 4 | static::valueToString($limit) |
|
| 507 | )); |
||
| 508 | } |
||
| 509 | 8 | } |
|
| 510 | |||
| 511 | 8 | public static function lessThan($value, $limit, $message = '') |
|
| 512 | { |
||
| 513 | 8 | if ($value >= $limit) { |
|
| 514 | 4 | static::reportInvalidArgument(sprintf( |
|
| 515 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
| 516 | 4 | static::valueToString($value), |
|
| 517 | 4 | static::valueToString($limit) |
|
| 518 | )); |
||
| 519 | } |
||
| 520 | 4 | } |
|
| 521 | |||
| 522 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
| 523 | { |
||
| 524 | 12 | if ($value > $limit) { |
|
| 525 | 4 | static::reportInvalidArgument(sprintf( |
|
| 526 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
| 527 | 4 | static::valueToString($value), |
|
| 528 | 4 | static::valueToString($limit) |
|
| 529 | )); |
||
| 530 | } |
||
| 531 | 8 | } |
|
| 532 | |||
| 533 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
| 544 | |||
| 545 | 8 | public static function oneOf($value, array $values, $message = '') |
|
| 555 | |||
| 556 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
| 566 | |||
| 567 | 20 | View Code Duplication | public static function notContains($value, $subString, $message = '') |
| 577 | |||
| 578 | 40 | public static function notWhitespaceOnly($value, $message = '') |
|
| 579 | { |
||
| 580 | 40 | if (preg_match('/^\s*$/', $value)) { |
|
| 581 | 24 | static::reportInvalidArgument(sprintf( |
|
| 582 | 24 | $message ?: 'Expected a non-whitespace string. Got: %s', |
|
| 583 | 24 | static::valueToString($value) |
|
| 584 | )); |
||
| 585 | } |
||
| 586 | 16 | } |
|
| 587 | |||
| 588 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
| 598 | |||
| 599 | 12 | public static function startsWithLetter($value, $message = '') |
|
| 617 | |||
| 618 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
| 628 | |||
| 629 | 12 | public static function regex($value, $pattern, $message = '') |
|
| 630 | { |
||
| 631 | 12 | if (!preg_match($pattern, $value)) { |
|
| 632 | 8 | static::reportInvalidArgument(sprintf( |
|
| 633 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
| 634 | 8 | static::valueToString($value) |
|
| 635 | )); |
||
| 636 | } |
||
| 637 | 4 | } |
|
| 638 | |||
| 639 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
| 653 | |||
| 654 | 12 | View Code Duplication | public static function digits($value, $message = '') |
| 668 | |||
| 669 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
| 683 | |||
| 684 | 16 | View Code Duplication | public static function lower($value, $message = '') |
| 698 | |||
| 699 | 16 | View Code Duplication | public static function upper($value, $message = '') |
| 713 | |||
| 714 | 24 | public static function length($value, $length, $message = '') |
|
| 724 | |||
| 725 | 24 | public static function minLength($value, $min, $message = '') |
|
| 726 | { |
||
| 727 | 24 | if (static::strlen($value) < $min) { |
|
| 728 | 8 | static::reportInvalidArgument(sprintf( |
|
| 729 | 8 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
| 730 | 8 | static::valueToString($value), |
|
| 731 | 8 | $min |
|
| 732 | )); |
||
| 733 | } |
||
| 734 | 16 | } |
|
| 735 | |||
| 736 | 24 | public static function maxLength($value, $max, $message = '') |
|
| 737 | { |
||
| 738 | 24 | if (static::strlen($value) > $max) { |
|
| 739 | 8 | static::reportInvalidArgument(sprintf( |
|
| 740 | 8 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
| 741 | 8 | static::valueToString($value), |
|
| 742 | 8 | $max |
|
| 743 | )); |
||
| 744 | } |
||
| 745 | 16 | } |
|
| 746 | |||
| 747 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
| 760 | |||
| 761 | 36 | public static function fileExists($value, $message = '') |
|
| 772 | |||
| 773 | 12 | View Code Duplication | public static function file($value, $message = '') |
| 784 | |||
| 785 | 12 | View Code Duplication | public static function directory($value, $message = '') |
| 796 | |||
| 797 | public static function readable($value, $message = '') |
||
| 798 | { |
||
| 799 | if (!is_readable($value)) { |
||
| 800 | static::reportInvalidArgument(sprintf( |
||
| 801 | $message ?: 'The path %s is not readable.', |
||
| 802 | static::valueToString($value) |
||
| 803 | )); |
||
| 804 | } |
||
| 805 | } |
||
| 806 | |||
| 807 | public static function writable($value, $message = '') |
||
| 808 | { |
||
| 809 | if (!is_writable($value)) { |
||
| 810 | static::reportInvalidArgument(sprintf( |
||
| 811 | $message ?: 'The path %s is not writable.', |
||
| 812 | static::valueToString($value) |
||
| 813 | )); |
||
| 814 | } |
||
| 815 | } |
||
| 816 | |||
| 817 | 8 | public static function classExists($value, $message = '') |
|
| 818 | { |
||
| 819 | 8 | if (!class_exists($value)) { |
|
| 820 | 4 | static::reportInvalidArgument(sprintf( |
|
| 821 | 4 | $message ?: 'Expected an existing class name. Got: %s', |
|
| 822 | 4 | static::valueToString($value) |
|
| 823 | )); |
||
| 824 | } |
||
| 825 | 4 | } |
|
| 826 | |||
| 827 | 8 | public static function subclassOf($value, $class, $message = '') |
|
| 837 | |||
| 838 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
| 848 | |||
| 849 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
| 858 | |||
| 859 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
| 868 | |||
| 869 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
| 878 | |||
| 879 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
| 888 | |||
| 889 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
| 898 | |||
| 899 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
| 908 | |||
| 909 | 8 | public static function count($array, $number, $message = '') |
|
| 917 | |||
| 918 | 12 | View Code Duplication | public static function minCount($array, $min, $message = '') |
| 928 | |||
| 929 | 12 | View Code Duplication | public static function maxCount($array, $max, $message = '') |
| 939 | |||
| 940 | 12 | public static function countBetween($array, $min, $max, $message = '') |
|
| 953 | |||
| 954 | 48 | public static function uuid($value, $message = '') |
|
| 971 | |||
| 972 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
| 998 | |||
| 999 | 970 | public static function __callStatic($name, $arguments) |
|
| 1027 | |||
| 1028 | 437 | protected static function valueToString($value) |
|
| 1029 | { |
||
| 1030 | 437 | if (null === $value) { |
|
| 1031 | 11 | return 'null'; |
|
| 1032 | } |
||
| 1033 | |||
| 1034 | 428 | if (true === $value) { |
|
| 1035 | 15 | return 'true'; |
|
| 1036 | } |
||
| 1037 | |||
| 1038 | 418 | if (false === $value) { |
|
| 1039 | 13 | return 'false'; |
|
| 1040 | } |
||
| 1041 | |||
| 1042 | 405 | if (is_array($value)) { |
|
| 1043 | 1 | return 'array'; |
|
| 1044 | } |
||
| 1045 | |||
| 1046 | 404 | if (is_object($value)) { |
|
| 1047 | 2 | if (method_exists($value, '__toString')) { |
|
| 1048 | 1 | return get_class($value).': '.self::valueToString($value->__toString()); |
|
| 1049 | } |
||
| 1050 | |||
| 1051 | 1 | return get_class($value); |
|
| 1052 | } |
||
| 1053 | |||
| 1054 | 403 | if (is_resource($value)) { |
|
| 1055 | 1 | return 'resource'; |
|
| 1064 | |||
| 1065 | 169 | protected static function typeToString($value) |
|
| 1069 | |||
| 1070 | 124 | protected static function strlen($value) |
|
| 1082 | |||
| 1083 | 621 | protected static function reportInvalidArgument($message) |
|
| 1087 | |||
| 1088 | private function __construct() |
||
| 1091 | } |
||
| 1092 |