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 |
||
149 | class Assert |
||
|
|||
150 | { |
||
151 | 90 | public static function string($value, $message = '') |
|
160 | |||
161 | 12 | public static function stringNotEmpty($value, $message = '') |
|
166 | |||
167 | 17 | View Code Duplication | public static function integer($value, $message = '') |
176 | |||
177 | 16 | View Code Duplication | public static function integerish($value, $message = '') |
186 | |||
187 | 16 | View Code Duplication | public static function float($value, $message = '') |
196 | |||
197 | 20 | View Code Duplication | public static function numeric($value, $message = '') |
206 | |||
207 | 16 | View Code Duplication | public static function boolean($value, $message = '') |
216 | |||
217 | 23 | View Code Duplication | public static function scalar($value, $message = '') |
226 | |||
227 | 23 | View Code Duplication | public static function object($value, $message = '') |
236 | |||
237 | 16 | public static function resource($value, $type = null, $message = '') |
|
238 | { |
||
239 | 16 | if (!is_resource($value)) { |
|
240 | 4 | static::reportInvalidArgument(sprintf( |
|
241 | 4 | $message ?: 'Expected a resource. Got: %s', |
|
242 | 4 | static::typeToString($value) |
|
243 | )); |
||
244 | } |
||
245 | |||
246 | 12 | if ($type && $type !== get_resource_type($value)) { |
|
247 | 4 | static::reportInvalidArgument(sprintf( |
|
248 | 4 | $message ?: 'Expected a resource of type %2$s. Got: %s', |
|
249 | 4 | static::typeToString($value), |
|
250 | 4 | $type |
|
251 | )); |
||
252 | } |
||
253 | 8 | } |
|
254 | |||
255 | 20 | View Code Duplication | public static function isCallable($value, $message = '') |
264 | |||
265 | 20 | View Code Duplication | public static function isArray($value, $message = '') |
274 | |||
275 | 20 | public static function isTraversable($value, $message = '') |
|
292 | |||
293 | 516 | public static function isIterable($value, $message = '') |
|
302 | |||
303 | 16 | View Code Duplication | public static function isInstanceOf($value, $class, $message = '') |
304 | { |
||
305 | 16 | if (!($value instanceof $class)) { |
|
306 | 12 | static::reportInvalidArgument(sprintf( |
|
307 | 12 | $message ?: 'Expected an instance of %2$s. Got: %s', |
|
308 | 12 | static::typeToString($value), |
|
309 | 12 | $class |
|
310 | )); |
||
311 | } |
||
312 | 4 | } |
|
313 | |||
314 | 16 | View Code Duplication | public static function notInstanceOf($value, $class, $message = '') |
315 | { |
||
316 | 16 | if ($value instanceof $class) { |
|
317 | 4 | static::reportInvalidArgument(sprintf( |
|
318 | 4 | $message ?: 'Expected an instance other than %2$s. Got: %s', |
|
319 | 4 | static::typeToString($value), |
|
320 | 4 | $class |
|
321 | )); |
||
322 | } |
||
323 | 12 | } |
|
324 | |||
325 | 23 | public static function isEmpty($value, $message = '') |
|
334 | |||
335 | 31 | public static function notEmpty($value, $message = '') |
|
336 | { |
||
337 | 31 | if (empty($value)) { |
|
338 | 19 | static::reportInvalidArgument(sprintf( |
|
339 | 19 | $message ?: 'Expected a non-empty value. Got: %s', |
|
340 | 19 | static::valueToString($value) |
|
341 | )); |
||
342 | } |
||
343 | 12 | } |
|
344 | |||
345 | 11 | public static function null($value, $message = '') |
|
346 | { |
||
347 | 11 | if (null !== $value) { |
|
348 | 8 | static::reportInvalidArgument(sprintf( |
|
349 | 8 | $message ?: 'Expected null. Got: %s', |
|
350 | 8 | static::valueToString($value) |
|
351 | )); |
||
352 | } |
||
353 | 3 | } |
|
354 | |||
355 | 11 | public static function notNull($value, $message = '') |
|
363 | |||
364 | 15 | public static function true($value, $message = '') |
|
365 | { |
||
366 | 15 | if (true !== $value) { |
|
367 | 11 | static::reportInvalidArgument(sprintf( |
|
368 | 11 | $message ?: 'Expected a value to be true. Got: %s', |
|
369 | 11 | static::valueToString($value) |
|
370 | )); |
||
371 | } |
||
372 | 4 | } |
|
373 | |||
374 | 19 | public static function false($value, $message = '') |
|
383 | |||
384 | 32 | public static function eq($value, $value2, $message = '') |
|
394 | |||
395 | 16 | public static function notEq($value, $value2, $message = '') |
|
404 | |||
405 | 16 | public static function same($value, $value2, $message = '') |
|
415 | |||
416 | 16 | public static function notSame($value, $value2, $message = '') |
|
425 | |||
426 | 8 | public static function greaterThan($value, $limit, $message = '') |
|
427 | { |
||
428 | 8 | if ($value <= $limit) { |
|
429 | 4 | static::reportInvalidArgument(sprintf( |
|
430 | 4 | $message ?: 'Expected a value greater than %2$s. Got: %s', |
|
431 | 4 | static::valueToString($value), |
|
432 | 4 | static::valueToString($limit) |
|
433 | )); |
||
434 | } |
||
435 | 4 | } |
|
436 | |||
437 | 12 | public static function greaterThanEq($value, $limit, $message = '') |
|
438 | { |
||
439 | 12 | if ($value < $limit) { |
|
440 | 4 | static::reportInvalidArgument(sprintf( |
|
441 | 4 | $message ?: 'Expected a value greater than or equal to %2$s. Got: %s', |
|
442 | 4 | static::valueToString($value), |
|
443 | 4 | static::valueToString($limit) |
|
444 | )); |
||
445 | } |
||
446 | 8 | } |
|
447 | |||
448 | 8 | public static function lessThan($value, $limit, $message = '') |
|
449 | { |
||
450 | 8 | if ($value >= $limit) { |
|
451 | 4 | static::reportInvalidArgument(sprintf( |
|
452 | 4 | $message ?: 'Expected a value less than %2$s. Got: %s', |
|
453 | 4 | static::valueToString($value), |
|
454 | 4 | static::valueToString($limit) |
|
455 | )); |
||
456 | } |
||
457 | 4 | } |
|
458 | |||
459 | 12 | public static function lessThanEq($value, $limit, $message = '') |
|
460 | { |
||
461 | 12 | if ($value > $limit) { |
|
462 | 4 | static::reportInvalidArgument(sprintf( |
|
463 | 4 | $message ?: 'Expected a value less than or equal to %2$s. Got: %s', |
|
464 | 4 | static::valueToString($value), |
|
465 | 4 | static::valueToString($limit) |
|
466 | )); |
||
467 | } |
||
468 | 8 | } |
|
469 | |||
470 | 16 | View Code Duplication | public static function range($value, $min, $max, $message = '') |
471 | { |
||
472 | 16 | if ($value < $min || $value > $max) { |
|
473 | 8 | static::reportInvalidArgument(sprintf( |
|
474 | 8 | $message ?: 'Expected a value between %2$s and %3$s. Got: %s', |
|
475 | 8 | static::valueToString($value), |
|
476 | 8 | static::valueToString($min), |
|
477 | 8 | static::valueToString($max) |
|
478 | )); |
||
479 | } |
||
480 | 8 | } |
|
481 | |||
482 | 8 | public static function oneOf($value, array $values, $message = '') |
|
483 | { |
||
484 | 8 | if (!in_array($value, $values, true)) { |
|
485 | 4 | static::reportInvalidArgument(sprintf( |
|
486 | 4 | $message ?: 'Expected one of: %2$s. Got: %s', |
|
487 | 4 | static::valueToString($value), |
|
488 | 4 | implode(', ', array_map(array('static', 'valueToString'), $values)) |
|
489 | )); |
||
490 | } |
||
491 | 4 | } |
|
492 | |||
493 | 20 | View Code Duplication | public static function contains($value, $subString, $message = '') |
494 | { |
||
495 | 20 | if (false === strpos($value, $subString)) { |
|
496 | 8 | static::reportInvalidArgument(sprintf( |
|
497 | 8 | $message ?: 'Expected a value to contain %2$s. Got: %s', |
|
498 | 8 | static::valueToString($value), |
|
499 | 8 | static::valueToString($subString) |
|
500 | )); |
||
501 | } |
||
502 | 12 | } |
|
503 | |||
504 | 12 | View Code Duplication | public static function startsWith($value, $prefix, $message = '') |
505 | { |
||
506 | 12 | if (0 !== strpos($value, $prefix)) { |
|
507 | 8 | static::reportInvalidArgument(sprintf( |
|
508 | 8 | $message ?: 'Expected a value to start with %2$s. Got: %s', |
|
509 | 8 | static::valueToString($value), |
|
510 | 8 | static::valueToString($prefix) |
|
511 | )); |
||
512 | } |
||
513 | 4 | } |
|
514 | |||
515 | 12 | public static function startsWithLetter($value, $message = '') |
|
533 | |||
534 | 12 | View Code Duplication | public static function endsWith($value, $suffix, $message = '') |
535 | { |
||
536 | 12 | if ($suffix !== substr($value, -static::strlen($suffix))) { |
|
537 | 8 | static::reportInvalidArgument(sprintf( |
|
538 | 8 | $message ?: 'Expected a value to end with %2$s. Got: %s', |
|
539 | 8 | static::valueToString($value), |
|
540 | 8 | static::valueToString($suffix) |
|
541 | )); |
||
542 | } |
||
543 | 4 | } |
|
544 | |||
545 | 12 | public static function regex($value, $pattern, $message = '') |
|
546 | { |
||
547 | 12 | if (!preg_match($pattern, $value)) { |
|
548 | 8 | static::reportInvalidArgument(sprintf( |
|
549 | 8 | $message ?: 'The value %s does not match the expected pattern.', |
|
550 | 8 | static::valueToString($value) |
|
551 | )); |
||
552 | } |
||
553 | 4 | } |
|
554 | |||
555 | 12 | View Code Duplication | public static function alpha($value, $message = '') |
556 | { |
||
557 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
558 | 12 | setlocale(LC_CTYPE, 'C'); |
|
559 | 12 | $valid = !ctype_alpha($value); |
|
560 | 12 | setlocale(LC_CTYPE, $locale); |
|
561 | |||
562 | 12 | if ($valid) { |
|
563 | 8 | static::reportInvalidArgument(sprintf( |
|
564 | 8 | $message ?: 'Expected a value to contain only letters. Got: %s', |
|
565 | 8 | static::valueToString($value) |
|
566 | )); |
||
567 | } |
||
568 | 4 | } |
|
569 | |||
570 | 12 | View Code Duplication | public static function digits($value, $message = '') |
571 | { |
||
572 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
573 | 12 | setlocale(LC_CTYPE, 'C'); |
|
574 | 12 | $valid = !ctype_digit($value); |
|
575 | 12 | setlocale(LC_CTYPE, $locale); |
|
576 | |||
577 | 12 | if ($valid) { |
|
578 | 8 | static::reportInvalidArgument(sprintf( |
|
579 | 8 | $message ?: 'Expected a value to contain digits only. Got: %s', |
|
580 | 8 | static::valueToString($value) |
|
581 | )); |
||
582 | } |
||
583 | 4 | } |
|
584 | |||
585 | 12 | View Code Duplication | public static function alnum($value, $message = '') |
586 | { |
||
587 | 12 | $locale = setlocale(LC_CTYPE, 0); |
|
588 | 12 | setlocale(LC_CTYPE, 'C'); |
|
589 | 12 | $valid = !ctype_alnum($value); |
|
590 | 12 | setlocale(LC_CTYPE, $locale); |
|
591 | |||
592 | 12 | if ($valid) { |
|
593 | 8 | static::reportInvalidArgument(sprintf( |
|
594 | 8 | $message ?: 'Expected a value to contain letters and digits only. Got: %s', |
|
595 | 8 | static::valueToString($value) |
|
596 | )); |
||
597 | } |
||
598 | 4 | } |
|
599 | |||
600 | 16 | View Code Duplication | public static function lower($value, $message = '') |
601 | { |
||
602 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
603 | 16 | setlocale(LC_CTYPE, 'C'); |
|
604 | 16 | $valid = !ctype_lower($value); |
|
605 | 16 | setlocale(LC_CTYPE, $locale); |
|
606 | |||
607 | 16 | if ($valid) { |
|
608 | 12 | static::reportInvalidArgument(sprintf( |
|
609 | 12 | $message ?: 'Expected a value to contain lowercase characters only. Got: %s', |
|
610 | 12 | static::valueToString($value) |
|
611 | )); |
||
612 | } |
||
613 | 4 | } |
|
614 | |||
615 | 16 | View Code Duplication | public static function upper($value, $message = '') |
616 | { |
||
617 | 16 | $locale = setlocale(LC_CTYPE, 0); |
|
618 | 16 | setlocale(LC_CTYPE, 'C'); |
|
619 | 16 | $valid = !ctype_upper($value); |
|
620 | 16 | setlocale(LC_CTYPE, $locale); |
|
621 | |||
622 | 16 | if ($valid) { |
|
623 | 12 | static::reportInvalidArgument(sprintf( |
|
624 | 12 | $message ?: 'Expected a value to contain uppercase characters only. Got: %s', |
|
625 | 12 | static::valueToString($value) |
|
626 | )); |
||
627 | } |
||
628 | 4 | } |
|
629 | |||
630 | 24 | public static function length($value, $length, $message = '') |
|
631 | { |
||
632 | 24 | if ($length !== static::strlen($value)) { |
|
633 | 16 | static::reportInvalidArgument(sprintf( |
|
634 | 16 | $message ?: 'Expected a value to contain %2$s characters. Got: %s', |
|
635 | 16 | static::valueToString($value), |
|
636 | 16 | $length |
|
637 | )); |
||
638 | } |
||
639 | 8 | } |
|
640 | |||
641 | 24 | public static function minLength($value, $min, $message = '') |
|
642 | { |
||
643 | 24 | if (static::strlen($value) < $min) { |
|
644 | 8 | static::reportInvalidArgument(sprintf( |
|
645 | 8 | $message ?: 'Expected a value to contain at least %2$s characters. Got: %s', |
|
646 | 8 | static::valueToString($value), |
|
647 | 8 | $min |
|
648 | )); |
||
649 | } |
||
650 | 16 | } |
|
651 | |||
652 | 24 | public static function maxLength($value, $max, $message = '') |
|
653 | { |
||
654 | 24 | if (static::strlen($value) > $max) { |
|
655 | 8 | static::reportInvalidArgument(sprintf( |
|
656 | 8 | $message ?: 'Expected a value to contain at most %2$s characters. Got: %s', |
|
657 | 8 | static::valueToString($value), |
|
658 | 8 | $max |
|
659 | )); |
||
660 | } |
||
661 | 16 | } |
|
662 | |||
663 | 40 | View Code Duplication | public static function lengthBetween($value, $min, $max, $message = '') |
664 | { |
||
665 | 40 | $length = static::strlen($value); |
|
666 | |||
667 | 40 | if ($length < $min || $length > $max) { |
|
668 | 16 | static::reportInvalidArgument(sprintf( |
|
669 | 16 | $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s', |
|
670 | 16 | static::valueToString($value), |
|
671 | 16 | $min, |
|
672 | 16 | $max |
|
673 | )); |
||
674 | } |
||
675 | 24 | } |
|
676 | |||
677 | 36 | public static function fileExists($value, $message = '') |
|
678 | { |
||
679 | 36 | static::string($value); |
|
680 | |||
681 | 36 | if (!file_exists($value)) { |
|
682 | 12 | static::reportInvalidArgument(sprintf( |
|
683 | 12 | $message ?: 'The file %s does not exist.', |
|
684 | 12 | static::valueToString($value) |
|
685 | )); |
||
686 | } |
||
687 | 24 | } |
|
688 | |||
689 | 12 | View Code Duplication | public static function file($value, $message = '') |
690 | { |
||
691 | 12 | static::fileExists($value, $message); |
|
692 | |||
693 | 8 | if (!is_file($value)) { |
|
694 | 4 | static::reportInvalidArgument(sprintf( |
|
695 | 4 | $message ?: 'The path %s is not a file.', |
|
696 | 4 | static::valueToString($value) |
|
697 | )); |
||
698 | } |
||
699 | 4 | } |
|
700 | |||
701 | 12 | View Code Duplication | public static function directory($value, $message = '') |
702 | { |
||
703 | 12 | static::fileExists($value, $message); |
|
704 | |||
705 | 8 | if (!is_dir($value)) { |
|
706 | 4 | static::reportInvalidArgument(sprintf( |
|
707 | 4 | $message ?: 'The path %s is no directory.', |
|
708 | 4 | static::valueToString($value) |
|
709 | )); |
||
710 | } |
||
711 | 4 | } |
|
712 | |||
713 | public static function readable($value, $message = '') |
||
714 | { |
||
715 | if (!is_readable($value)) { |
||
716 | static::reportInvalidArgument(sprintf( |
||
717 | $message ?: 'The path %s is not readable.', |
||
718 | static::valueToString($value) |
||
719 | )); |
||
720 | } |
||
721 | } |
||
722 | |||
723 | public static function writable($value, $message = '') |
||
724 | { |
||
725 | if (!is_writable($value)) { |
||
726 | static::reportInvalidArgument(sprintf( |
||
727 | $message ?: 'The path %s is not writable.', |
||
728 | static::valueToString($value) |
||
729 | )); |
||
730 | } |
||
731 | } |
||
732 | |||
733 | 8 | public static function classExists($value, $message = '') |
|
742 | |||
743 | 8 | public static function subclassOf($value, $class, $message = '') |
|
744 | { |
||
745 | 8 | if (!is_subclass_of($value, $class)) { |
|
746 | 4 | static::reportInvalidArgument(sprintf( |
|
747 | 4 | $message ?: 'Expected a sub-class of %2$s. Got: %s', |
|
748 | 4 | static::valueToString($value), |
|
749 | 4 | static::valueToString($class) |
|
750 | )); |
||
751 | } |
||
752 | 4 | } |
|
753 | |||
754 | 8 | View Code Duplication | public static function implementsInterface($value, $interface, $message = '') |
755 | { |
||
756 | 8 | if (!in_array($interface, class_implements($value))) { |
|
757 | 4 | static::reportInvalidArgument(sprintf( |
|
758 | 4 | $message ?: 'Expected an implementation of %2$s. Got: %s', |
|
759 | 4 | static::valueToString($value), |
|
760 | 4 | static::valueToString($interface) |
|
761 | )); |
||
762 | } |
||
763 | 4 | } |
|
764 | |||
765 | 12 | View Code Duplication | public static function propertyExists($classOrObject, $property, $message = '') |
774 | |||
775 | 12 | View Code Duplication | public static function propertyNotExists($classOrObject, $property, $message = '') |
784 | |||
785 | 27 | View Code Duplication | public static function methodExists($classOrObject, $method, $message = '') |
794 | |||
795 | 27 | View Code Duplication | public static function methodNotExists($classOrObject, $method, $message = '') |
804 | |||
805 | 12 | View Code Duplication | public static function keyExists($array, $key, $message = '') |
806 | { |
||
807 | 12 | if (!array_key_exists($key, $array)) { |
|
808 | 4 | static::reportInvalidArgument(sprintf( |
|
809 | 4 | $message ?: 'Expected the key %s to exist.', |
|
810 | 4 | static::valueToString($key) |
|
811 | )); |
||
812 | } |
||
813 | 8 | } |
|
814 | |||
815 | 12 | View Code Duplication | public static function keyNotExists($array, $key, $message = '') |
816 | { |
||
817 | 12 | if (array_key_exists($key, $array)) { |
|
818 | 8 | static::reportInvalidArgument(sprintf( |
|
819 | 8 | $message ?: 'Expected the key %s to not exist.', |
|
820 | 8 | static::valueToString($key) |
|
821 | )); |
||
822 | } |
||
823 | 4 | } |
|
824 | |||
825 | 8 | public static function count($array, $number, $message = '') |
|
826 | { |
||
827 | 8 | static::eq( |
|
828 | 8 | count($array), |
|
829 | 8 | $number, |
|
830 | 8 | $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array)) |
|
831 | ); |
||
832 | 4 | } |
|
833 | |||
834 | 48 | public static function uuid($value, $message = '') |
|
835 | { |
||
836 | 48 | $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value); |
|
837 | |||
838 | // The nil UUID is special form of UUID that is specified to have all |
||
839 | // 128 bits set to zero. |
||
840 | 48 | if ('00000000-0000-0000-0000-000000000000' === $value) { |
|
841 | 4 | return; |
|
842 | } |
||
843 | |||
844 | 44 | if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { |
|
845 | 20 | static::reportInvalidArgument(sprintf( |
|
846 | 20 | $message ?: 'Value %s is not a valid UUID.', |
|
847 | 20 | static::valueToString($value) |
|
848 | )); |
||
849 | } |
||
850 | 24 | } |
|
851 | |||
852 | 24 | public static function throws(Closure $expression, $class = 'Exception', $message = '') |
|
853 | { |
||
854 | 24 | static::string($class); |
|
855 | |||
856 | 24 | $actual = 'none'; |
|
857 | try { |
||
858 | 24 | $expression(); |
|
859 | 24 | } catch (Exception $e) { |
|
860 | 20 | $actual = get_class($e); |
|
861 | 20 | if ($e instanceof $class) { |
|
862 | 20 | return; |
|
863 | } |
||
864 | 4 | } catch (Throwable $e) { |
|
865 | 4 | $actual = get_class($e); |
|
866 | 4 | if ($e instanceof $class) { |
|
867 | 4 | return; |
|
868 | } |
||
869 | } |
||
870 | |||
871 | 8 | static::reportInvalidArgument($message ?: sprintf( |
|
872 | 8 | 'Expected to throw "%s", got "%s"', |
|
873 | 8 | $class, |
|
874 | 8 | $actual |
|
875 | )); |
||
876 | } |
||
877 | |||
878 | 820 | public static function __callStatic($name, $arguments) |
|
879 | { |
||
880 | 820 | if ('nullOr' === substr($name, 0, 6)) { |
|
881 | 314 | if (null !== $arguments[0]) { |
|
882 | 243 | $method = lcfirst(substr($name, 6)); |
|
883 | 243 | call_user_func_array(array('static', $method), $arguments); |
|
884 | } |
||
885 | |||
886 | 193 | return; |
|
887 | } |
||
888 | |||
889 | 506 | if ('all' === substr($name, 0, 3)) { |
|
890 | 506 | static::isIterable($arguments[0]); |
|
891 | |||
892 | 506 | $method = lcfirst(substr($name, 3)); |
|
893 | 506 | $args = $arguments; |
|
894 | |||
895 | 506 | foreach ($arguments[0] as $entry) { |
|
896 | 506 | $args[0] = $entry; |
|
897 | |||
898 | 506 | call_user_func_array(array('static', $method), $args); |
|
899 | } |
||
900 | |||
901 | 250 | return; |
|
902 | } |
||
903 | |||
904 | throw new BadMethodCallException('No such method: '.$name); |
||
905 | } |
||
906 | |||
907 | 372 | protected static function valueToString($value) |
|
908 | { |
||
909 | 372 | if (null === $value) { |
|
910 | 11 | return 'null'; |
|
911 | } |
||
912 | |||
913 | 363 | if (true === $value) { |
|
914 | 15 | return 'true'; |
|
915 | } |
||
916 | |||
917 | 353 | if (false === $value) { |
|
918 | 13 | return 'false'; |
|
919 | } |
||
920 | |||
921 | 340 | if (is_array($value)) { |
|
922 | 1 | return 'array'; |
|
923 | } |
||
924 | |||
925 | 339 | if (is_object($value)) { |
|
926 | 1 | return get_class($value); |
|
927 | } |
||
928 | |||
929 | 338 | if (is_resource($value)) { |
|
930 | 1 | return 'resource'; |
|
931 | } |
||
932 | |||
933 | 338 | if (is_string($value)) { |
|
934 | 264 | return '"'.$value.'"'; |
|
935 | } |
||
936 | |||
937 | 82 | return (string) $value; |
|
938 | } |
||
939 | |||
940 | 137 | protected static function typeToString($value) |
|
944 | |||
945 | 124 | protected static function strlen($value) |
|
946 | { |
||
947 | 124 | if (!function_exists('mb_detect_encoding')) { |
|
948 | return strlen($value); |
||
949 | } |
||
950 | |||
951 | 124 | if (false === $encoding = mb_detect_encoding($value)) { |
|
952 | return strlen($value); |
||
953 | } |
||
954 | |||
955 | 124 | return mb_strwidth($value, $encoding); |
|
956 | } |
||
957 | |||
958 | 520 | protected static function reportInvalidArgument($message) |
|
959 | { |
||
960 | 520 | throw new InvalidArgumentException($message); |
|
961 | } |
||
962 | |||
963 | private function __construct() |
||
964 | { |
||
965 | } |
||
966 | } |
||
967 |