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 String 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 String, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class String |
||
30 | { |
||
31 | /** @type string FILTER_VALIDATE_BOOLEAN a symbolic constant for boolean validation */ |
||
32 | const FILTER_VALIDATE_BOOLEAN = "scabbiaFilterValidateBoolean"; |
||
33 | /** @type string FILTER_SANITIZE_BOOLEAN a symbolic constant for boolean sanitization */ |
||
34 | const FILTER_SANITIZE_BOOLEAN = "scabbiaFilterSanitizeBoolean"; |
||
35 | /** @type string FILTER_SANITIZE_XSS a symbolic constant for xss sanitization */ |
||
36 | const FILTER_SANITIZE_XSS = "scabbiaFilterSanitizeXss"; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Constructor to prevent new instances of String class |
||
41 | * |
||
42 | * @return String |
||
|
|||
43 | */ |
||
44 | final private function __construct() |
||
47 | |||
48 | /** |
||
49 | * Clone method to prevent duplication of String class |
||
50 | * |
||
51 | * @return String |
||
52 | */ |
||
53 | final private function __clone() |
||
56 | |||
57 | /** |
||
58 | * Unserialization method to prevent restoration of String class |
||
59 | * |
||
60 | * @return String |
||
61 | */ |
||
62 | final private function __wakeup() |
||
65 | |||
66 | /** |
||
67 | * Default variables for Html utility set |
||
68 | * |
||
69 | * @type array $defaults array of default variables |
||
70 | */ |
||
71 | public static $defaults = [ |
||
72 | "tab" => "\t", |
||
73 | "eol" => PHP_EOL, |
||
74 | |||
75 | "squote_replacement" => [["\\", "'"], ["\\\\", "\\'"]], |
||
76 | "dquote_replacement" => [["\\", "\""], ["\\\\", "\\\""]], |
||
77 | |||
78 | "baseconversion_url_chars" => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:[]@!$'()*+,;", |
||
79 | "baseconversion_base62_chars" => "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||
80 | ]; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Sets the default variables |
||
85 | * |
||
86 | * @param array $uDefaults variables to be set |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | public static function setDefaults($uDefaults) |
||
94 | |||
95 | /** |
||
96 | * Returns default encoding |
||
97 | * |
||
98 | * @return string name of the encoding |
||
99 | */ |
||
100 | public static function getEncoding() |
||
104 | |||
105 | /** |
||
106 | * Checks arguments in order and returns the value of the first expression that is not-null |
||
107 | * |
||
108 | * @param array $uValues values |
||
109 | * |
||
110 | * @return mixed first non-null expression in parameter list. |
||
111 | */ |
||
112 | public static function coalesce(...$uValues) |
||
130 | |||
131 | /** |
||
132 | * Prefixes all lines in the given string with the dashes or specified string |
||
133 | * |
||
134 | * @param string $uInput original string |
||
135 | * @param string $uPrefix the string will be added to beginning of all lines |
||
136 | * |
||
137 | * @return string prefixed string |
||
138 | */ |
||
139 | public static function prefixLines($uInput, $uPrefix = "- ") |
||
155 | |||
156 | /** |
||
157 | * Filters or sanitizes given value according to filter options |
||
158 | * |
||
159 | * @param mixed $uValue original value |
||
160 | * @param mixed $uFilter filter |
||
161 | * @param array $uArgs arguments |
||
162 | * |
||
163 | * @return mixed final output |
||
164 | * |
||
165 | * @todo recursive filtering option |
||
166 | */ |
||
167 | public static function filter($uValue, $uFilter, ...$uArgs) |
||
196 | |||
197 | /** |
||
198 | * Replaces placeholders given in string and formats them |
||
199 | * |
||
200 | * @param string $uString original string with placeholders |
||
201 | * @param array $uArgs arguments |
||
202 | * |
||
203 | * @return string final replaced output |
||
204 | */ |
||
205 | public static function format($uString, ...$uArgs) |
||
311 | |||
312 | /** |
||
313 | * Displays structured information of given parameter in a fancy way |
||
314 | * |
||
315 | * @param mixed $uVariable variable |
||
316 | * @param bool $uOutput whether return output as a function output or not |
||
317 | * |
||
318 | * @return string|null structure of given parameter |
||
319 | */ |
||
320 | public static function vardump($uVariable, $uOutput = true) |
||
372 | |||
373 | /** |
||
374 | * A basic hash method |
||
375 | * |
||
376 | * @param mixed $uValue value is going to be hashed |
||
377 | * |
||
378 | * @return int hash in decimal representation |
||
379 | */ |
||
380 | public static function hash($uValue) |
||
384 | |||
385 | /** |
||
386 | * Generates a random password in given length |
||
387 | * |
||
388 | * @param int $uLength password length |
||
389 | * |
||
390 | * @return string generated password |
||
391 | */ |
||
392 | public static function generatePassword($uLength) |
||
441 | |||
442 | /** |
||
443 | * Generates an unique-identifier and ensures it is in uuid format |
||
444 | * |
||
445 | * @return string generated uuid |
||
446 | */ |
||
447 | public static function generateUuid() |
||
476 | |||
477 | /** |
||
478 | * Generates a random string in given length |
||
479 | * |
||
480 | * @param int $uLength string length |
||
481 | * @param string $uCharset set of chars are going to be used in generated string |
||
482 | * |
||
483 | * @return string generated string |
||
484 | */ |
||
485 | public static function generate($uLength, $uCharset = "0123456789ABCDEF") |
||
496 | |||
497 | /** |
||
498 | * Filters the chars that occur XSS attacks |
||
499 | * |
||
500 | * @param string $uValue original value |
||
501 | * |
||
502 | * @return string filtered string |
||
503 | */ |
||
504 | public static function xss($uValue) |
||
536 | |||
537 | /** |
||
538 | * Strips given string and leaves only chars specified |
||
539 | * |
||
540 | * @param string $uString original string |
||
541 | * @param string $uValidChars set of chars are going to be allowed |
||
542 | * |
||
543 | * @return string stripped output |
||
544 | */ |
||
545 | public static function strip($uString, $uValidChars) |
||
560 | |||
561 | /** |
||
562 | * Escapes single quotes in a string |
||
563 | * |
||
564 | * @param string $uString original string |
||
565 | * @param bool $uCover whether cover output with single quotes or not |
||
566 | * |
||
567 | * @return string final output |
||
568 | */ |
||
569 | View Code Duplication | public static function squote($uString, $uCover = false) |
|
591 | |||
592 | /** |
||
593 | * Escapes double quotes in a string |
||
594 | * |
||
595 | * @param string $uString original string |
||
596 | * @param bool $uCover whether cover output with double quotes or not |
||
597 | * |
||
598 | * @return string final output |
||
599 | */ |
||
600 | View Code Duplication | public static function dquote($uString, $uCover = false) |
|
622 | |||
623 | /** |
||
624 | * Escapes single quotes in a set of strings |
||
625 | * |
||
626 | * @param string $uArray set of original strings |
||
627 | * @param bool $uCover whether cover output with single quotes or not |
||
628 | * |
||
629 | * @return array final output |
||
630 | */ |
||
631 | View Code Duplication | public static function squoteArray($uArray, $uCover = false) |
|
655 | |||
656 | /** |
||
657 | * Escapes double quotes in a set of strings |
||
658 | * |
||
659 | * @param string $uArray set of original strings |
||
660 | * @param bool $uCover whether cover output with double quotes or not |
||
661 | * |
||
662 | * @return array final output |
||
663 | */ |
||
664 | View Code Duplication | public static function dquoteArray($uArray, $uCover = false) |
|
688 | |||
689 | /** |
||
690 | * Replaces CRLF characters with given string |
||
691 | * |
||
692 | * @param string $uString original string |
||
693 | * @param string $uBreaks string that breaks are replaced with. |
||
694 | * |
||
695 | * @return string final output |
||
696 | */ |
||
697 | public static function replaceBreaks($uString, $uBreaks = "<br />") |
||
701 | |||
702 | /** |
||
703 | * Cuts a string if it exceeds given length |
||
704 | * |
||
705 | * @param string $uString original string |
||
706 | * @param int $uLength maximum length |
||
707 | * @param string $uSuffix a suffix that is going to be added to end of string if original string is cut |
||
708 | * |
||
709 | * @return string final output |
||
710 | */ |
||
711 | public static function cut($uString, $uLength, $uSuffix = "...") |
||
719 | |||
720 | /** |
||
721 | * Encodes html characters |
||
722 | * |
||
723 | * @param string $uString original string that is going to be encoded |
||
724 | * |
||
725 | * @return string encoded string |
||
726 | */ |
||
727 | View Code Duplication | public static function encodeHtml($uString) |
|
735 | |||
736 | /** |
||
737 | * Decodes encoded html characters |
||
738 | * |
||
739 | * @param string $uString original string that has encoded characters |
||
740 | * |
||
741 | * @return string decoded string |
||
742 | */ |
||
743 | View Code Duplication | public static function decodeHtml($uString) |
|
751 | |||
752 | /** |
||
753 | * Escapes special html characters |
||
754 | * |
||
755 | * @param string $uString original string that is going to be escaped |
||
756 | * |
||
757 | * @return string escaped string |
||
758 | */ |
||
759 | public static function escapeHtml($uString) |
||
763 | |||
764 | /** |
||
765 | * Unescapes escaped html characters |
||
766 | * |
||
767 | * @param string $uString original string that has escaped characters |
||
768 | * |
||
769 | * @return string unescaped string |
||
770 | */ |
||
771 | public static function unescapeHtml($uString) |
||
775 | |||
776 | /** |
||
777 | * Transforms the string to lowercase |
||
778 | * |
||
779 | * @param string $uString original string |
||
780 | * |
||
781 | * @return string lowercased string |
||
782 | */ |
||
783 | public static function toLower($uString) |
||
787 | |||
788 | /** |
||
789 | * Transforms the string to uppercase |
||
790 | * |
||
791 | * @param string $uString original string |
||
792 | * |
||
793 | * @return string uppercased string |
||
794 | */ |
||
795 | public static function toUpper($uString) |
||
799 | |||
800 | /** |
||
801 | * Capitalizes the first character of the given string |
||
802 | * |
||
803 | * @param string $uString original string |
||
804 | * |
||
805 | * @return string the string with the first character capitalized |
||
806 | */ |
||
807 | public static function capitalize($uString) |
||
811 | |||
812 | /** |
||
813 | * Capitalizes all first characters of the words in the given string |
||
814 | * |
||
815 | * @param string $uString original string |
||
816 | * @param string $uSpace the space character |
||
817 | * |
||
818 | * @return string the string with the first characters of the words capitalized |
||
819 | */ |
||
820 | public static function capitalizeWords($uString, $uSpace = " ") |
||
841 | |||
842 | /** |
||
843 | * Returns the length of the string |
||
844 | * |
||
845 | * @param string $uString the input string |
||
846 | * |
||
847 | * @return int string length |
||
848 | */ |
||
849 | public static function length($uString) |
||
854 | |||
855 | /** |
||
856 | * Checks the string if it starts with another string. |
||
857 | * |
||
858 | * @param string $uString the input string |
||
859 | * @param string $uNeedle another string |
||
860 | * |
||
861 | * @return bool true if first string starts with second one |
||
862 | */ |
||
863 | View Code Duplication | public static function startsWith($uString, $uNeedle) |
|
873 | |||
874 | /** |
||
875 | * Checks the string if it ends with another string |
||
876 | * |
||
877 | * @param string $uString the input string |
||
878 | * @param string $uNeedle another string |
||
879 | * |
||
880 | * @return bool true if first string ends with second one |
||
881 | */ |
||
882 | View Code Duplication | public static function endsWith($uString, $uNeedle) |
|
892 | |||
893 | /** |
||
894 | * Returns part of the string |
||
895 | * |
||
896 | * @param string $uString original string |
||
897 | * @param int $uStart start offset |
||
898 | * @param int $uLength length |
||
899 | * |
||
900 | * @return string sliced substring |
||
901 | */ |
||
902 | public static function substr($uString, $uStart, $uLength = null) |
||
910 | |||
911 | /** |
||
912 | * Find the offset of the first occurrence of given string |
||
913 | * |
||
914 | * @param string $uString the input string |
||
915 | * @param string $uNeedle another string |
||
916 | * @param int $uOffset start offset |
||
917 | * |
||
918 | * @return int position of first occurence |
||
919 | */ |
||
920 | public static function strpos($uString, $uNeedle, $uOffset = 0) |
||
924 | |||
925 | /** |
||
926 | * Returns the substring starting from and including the first occurrence of given string |
||
927 | * |
||
928 | * @param string $uString the input string |
||
929 | * @param string $uNeedle another string |
||
930 | * @param bool $uBeforeNeedle start offset |
||
931 | * |
||
932 | * @return string sliced substring, or false if string is not found |
||
933 | */ |
||
934 | public static function strstr($uString, $uNeedle, $uBeforeNeedle = false) |
||
938 | |||
939 | /** |
||
940 | * Returns the given bytes in short representation |
||
941 | * |
||
942 | * @param int $uSize size |
||
943 | * @param int $uPrecision precision |
||
944 | * |
||
945 | * @return string short representation |
||
946 | */ |
||
947 | View Code Duplication | public static function sizeCalc($uSize, $uPrecision = 0) |
|
956 | |||
957 | /** |
||
958 | * Returns the given number in short representation |
||
959 | * |
||
960 | * @param int $uSize size |
||
961 | * @param int $uPrecision precision |
||
962 | * |
||
963 | * @return string short representation |
||
964 | */ |
||
965 | View Code Duplication | public static function quantityCalc($uSize, $uPrecision = 0) |
|
974 | |||
975 | /** |
||
976 | * Returns the given period in short representation |
||
977 | * |
||
978 | * @param int $uTime time period |
||
979 | * |
||
980 | * @return string short representation |
||
981 | */ |
||
982 | public static function timeCalc($uTime) |
||
994 | |||
995 | /** |
||
996 | * Removes accented characters in a string |
||
997 | * |
||
998 | * @param string $uString the input string |
||
999 | * |
||
1000 | * @return string unaccented string |
||
1001 | */ |
||
1002 | public static function removeAccent($uString) |
||
1439 | |||
1440 | /** |
||
1441 | * Removes invisible characters in a string |
||
1442 | * |
||
1443 | * @param string $uString the input string |
||
1444 | * |
||
1445 | * @return string string with invisible characters removed |
||
1446 | */ |
||
1447 | public static function removeInvisibles($uString) |
||
1495 | |||
1496 | /** |
||
1497 | * Returns a transformed string that can be used as unixname and url portion |
||
1498 | * |
||
1499 | * @param string $uString the input string |
||
1500 | * @param string $uSpaceChar char that is used for spacing |
||
1501 | * |
||
1502 | * @return string transformed string |
||
1503 | */ |
||
1504 | public static function slug($uString, $uSpaceChar = "-") |
||
1514 | |||
1515 | /** |
||
1516 | * Converts a number to specified base |
||
1517 | * |
||
1518 | * @param int $uNumber the input number |
||
1519 | * @param string $uBaseChars charset available for target base |
||
1520 | * |
||
1521 | * @return string converted base |
||
1522 | */ |
||
1523 | public static function toBase($uNumber, $uBaseChars) |
||
1540 | |||
1541 | /** |
||
1542 | * Converts a number from specified base |
||
1543 | * |
||
1544 | * @param int $uNumber the input number |
||
1545 | * @param string $uBaseChars charset available for source base |
||
1546 | * |
||
1547 | * @return string converted base |
||
1548 | */ |
||
1549 | public static function fromBase($uNumber, $uBaseChars) |
||
1560 | |||
1561 | /** |
||
1562 | * Converts a number to base62 |
||
1563 | * |
||
1564 | * @param int $uNumber the input number |
||
1565 | * |
||
1566 | * @return string converted base |
||
1567 | */ |
||
1568 | public static function toBase62($uNumber) |
||
1572 | |||
1573 | /** |
||
1574 | * Converts a number from base62 |
||
1575 | * |
||
1576 | * @param int $uNumber the input number |
||
1577 | * |
||
1578 | * @return string converted base |
||
1579 | */ |
||
1580 | public static function fromBase62($uNumber) |
||
1584 | |||
1585 | /** |
||
1586 | * Converts a number to url base |
||
1587 | * |
||
1588 | * @param int $uNumber the input number |
||
1589 | * |
||
1590 | * @return string converted base |
||
1591 | */ |
||
1592 | public static function toBaseUrl($uNumber) |
||
1596 | |||
1597 | /** |
||
1598 | * Converts a number from url base |
||
1599 | * |
||
1600 | * @param int $uNumber the input number |
||
1601 | * |
||
1602 | * @return string converted base |
||
1603 | */ |
||
1604 | public static function fromBaseUrl($uNumber) |
||
1608 | |||
1609 | /** |
||
1610 | * Shortens a uuid to use it in compact format |
||
1611 | * |
||
1612 | * @param string $uString the input string |
||
1613 | * |
||
1614 | * @return string shortened uuid |
||
1615 | */ |
||
1616 | public static function shortenUuid($uString) |
||
1635 | |||
1636 | /** |
||
1637 | * Unshortens a uuid to restore it in compact format |
||
1638 | * |
||
1639 | * @param string $uString the input string |
||
1640 | * |
||
1641 | * @return string unshortened uuid |
||
1642 | */ |
||
1643 | public static function unshortenUuid($uString) |
||
1666 | |||
1667 | /** |
||
1668 | * Returns the ordinal pronounce of a number |
||
1669 | * |
||
1670 | * @param int $uNumber the input number |
||
1671 | * |
||
1672 | * @return string ordinal string |
||
1673 | */ |
||
1674 | public static function ordinalize($uNumber) |
||
1691 | |||
1692 | /** |
||
1693 | * Swaps two variables in memory |
||
1694 | * |
||
1695 | * @param mixed $uVariable1 first variable |
||
1696 | * @param mixed $uVariable2 second variable |
||
1697 | * |
||
1698 | * @return void |
||
1699 | */ |
||
1700 | public static function swap(&$uVariable1, &$uVariable2) |
||
1706 | |||
1707 | /** |
||
1708 | * Sanitize a filename to prevent filesystem vulnerabilities |
||
1709 | * |
||
1710 | * @param string $uFilename the input filename |
||
1711 | * @param bool $uRemoveAccent whether accented characters are going to be removed or not |
||
1712 | * @param bool $uRemoveSpaces whether spacing is going to be replaced with dashes or not |
||
1713 | * |
||
1714 | * @return string sanitized filename |
||
1715 | * |
||
1716 | * @todo optionally explode by '/', sanitize between |
||
1717 | */ |
||
1718 | public static function sanitizeFilename($uFilename, $uRemoveAccent = false, $uRemoveSpaces = false) |
||
1773 | |||
1774 | /** |
||
1775 | * Returns the best matching path among the alternatives |
||
1776 | * |
||
1777 | * @param array $uPathList set of paths |
||
1778 | * @param string $uFullPath the full path |
||
1779 | * |
||
1780 | * @return string|false the path if found, false otherwise |
||
1781 | */ |
||
1782 | public function matchPaths($uPathList, $uFullPath) |
||
1799 | |||
1800 | /** |
||
1801 | * Captures and replaces http links in a string |
||
1802 | * |
||
1803 | * @param string $uString the input string |
||
1804 | * @param callable $uCallback closure that gets each url address as a parameter and outputs replaced string |
||
1805 | * |
||
1806 | * @return string updated string |
||
1807 | */ |
||
1808 | public static function convertLinks($uString, /* callable */ $uCallback) |
||
1816 | } |
||
1817 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.