Complex classes like Tools 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 Tools, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Tools |
||
26 | { |
||
27 | |||
28 | use Singleton; |
||
29 | use ServiceGetterStatic; |
||
30 | |||
31 | const DATE_TIME_DB_EMPTY = '0000-00-00 00:00:00'; |
||
32 | const DATE_DB_EMPTY = '0000-00-00'; |
||
33 | const TIME_DB_EMPTY = '00:00:00'; |
||
34 | const YEAR_DB_EMPTY = '1901'; |
||
35 | const YEAR_DB_MAX = '2155'; |
||
36 | |||
37 | const DATETIME_DATABASE = 'Y-m-d H:i:s'; |
||
38 | const DATE_DATABASE = 'Y-m-d'; |
||
39 | const TIME_DATABASE = 'H:i:s'; |
||
40 | const YEAR_DATABASE = 'Y'; |
||
41 | |||
42 | /* |
||
43 | * Returns the current active client Id @return integer |
||
44 | * |
||
45 | * @return integer |
||
46 | */ |
||
47 | public static function getClientId() |
||
55 | |||
56 | |||
57 | /* |
||
58 | * Returns the current active client code @return string |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public static function getClientCode() |
||
70 | |||
71 | /** |
||
72 | * Determine if a value is a closure object |
||
73 | * @param mixed $t |
||
74 | * @return boolean |
||
75 | */ |
||
76 | public static function is_closure($t) { |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Return a class name with namespace removed |
||
83 | * |
||
84 | * @param object $object |
||
85 | * @return string|false |
||
86 | */ |
||
87 | public static function getClassName($object = null) |
||
98 | |||
99 | |||
100 | /** |
||
101 | * Determine default table name for an ActiveRecord class |
||
102 | * @param \fangface\db\ActiveRecord $object |
||
103 | * @return string |
||
104 | */ |
||
105 | |||
106 | public static function getDefaultTableNameFromClass($object = null) |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Return a class name with namespace cleaned of characters not required |
||
140 | * Typically used when the class name needs to be used as a key for anything |
||
141 | * |
||
142 | * @param object $object |
||
143 | * @return string|false |
||
144 | */ |
||
145 | public static function getCleanClassNameWithNamespace($object) |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Converts a size in bytes to something human readable. |
||
164 | */ |
||
165 | public static function getHumanBytes($bytes, $precision = 2) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Chcek if two values are numerically the same. |
||
187 | * Casts the |
||
188 | * arguments as integers to type check. |
||
189 | * |
||
190 | * @param integer $arg_1 |
||
191 | * @param integer $arg_2 |
||
192 | * @return boolean |
||
193 | */ |
||
194 | public static function intEquals($arg_1, $arg_2) |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Chcek if two values are equal string. |
||
202 | * Casts the |
||
203 | * arguments as strings to type check. |
||
204 | * |
||
205 | * @param string $arg_1 |
||
206 | * @param string $arg_2 |
||
207 | * @return boolean |
||
208 | */ |
||
209 | public static function strEquals($arg_1, $arg_2) |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Returns the indexed element for the mixed object. |
||
217 | * You can specify |
||
218 | * a default value to return in $default and specify if you want to |
||
219 | * just find out of the index exists ($check_index_exists). The latter |
||
220 | * will return a boolean. |
||
221 | * |
||
222 | * @param mixed $object |
||
223 | * @param string $index |
||
224 | * @param mixed $default |
||
225 | * @param boolean $check_index_exists |
||
226 | * @return boolean|mixed |
||
227 | */ |
||
228 | public static function get($object, $index, $default = FALSE, $check_index_exists = FALSE) |
||
242 | |||
243 | |||
244 | /** |
||
245 | * Checks if the variable is of the specified type and a valid value |
||
246 | * |
||
247 | * @param mixed $mixed |
||
248 | * @param string $expected_type |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public static function isValid($mixed, $expected_type = 'INT') |
||
279 | |||
280 | |||
281 | /** |
||
282 | * List the services currently specified in the service manager |
||
283 | * (useful for debug) |
||
284 | * @param string $lineBreak (default is "\n") |
||
285 | */ |
||
286 | public static function listServices($lineBreak = "\n") { |
||
294 | |||
295 | |||
296 | /** |
||
297 | * |
||
298 | * @param number|string|boolean|null $value |
||
299 | * @param string|array|\yii\db\ColumnSchema $dataType type of attribute or an array of values specifying the column schema |
||
300 | * @param integer $length number of characters for the attribute within the db table [OPTIONAL] default 0 |
||
301 | * @param integer $decimals how many decimal characters exist for this attribute within the db table [OPTIONAL] default 0 |
||
302 | * @param boolean $unsigned is the atribute unsigned within the db table [OPTIONAL] default false |
||
303 | * @param boolean $zerofill is the attribute zerofilled within the db table [OPTIONAL] default false |
||
304 | * @param boolean $isNullable is the attribute nullable within the db table [OPTIONAL] default false |
||
305 | * @param boolean $autoIncrement is the attribute auto incrementing within the db table [OPTIONAL] default false |
||
306 | * @param boolean $primaryKey is the attribute the primary key within the table [OPTIONAL] default false |
||
307 | * @param mixed $defaultValue default value [OPTIONAL] default '' |
||
308 | * @return number|string|boolean|null |
||
309 | */ |
||
310 | public static function formatAttributeValue($value, $dataType, $length = 0, $decimals = 0, $unsigned = false, $zerofill = false, $isNullable = false, $autoIncrement = false, $primaryKey = false, $defaultValue = '') |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Generate a random string to the specified length and optionally exclude often |
||
486 | * mis-read characters |
||
487 | * |
||
488 | * @param integer $len number of characters to generate |
||
489 | * @param boolean $excludeEasyMisRead [optional] exclude typically mis-ead characters, default false |
||
490 | * @param boolean $upperCase [optional] force to upper case |
||
491 | * @return string |
||
492 | */ |
||
493 | public static function randomString($len, $excludeEasyMisRead = false, $upperCase = false) |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Translates a camel case string into a string with |
||
540 | * underscores (e.g. firstName -> first_name) |
||
541 | * |
||
542 | * @param string $str String to convert |
||
543 | * @return string $str translated from camel case |
||
544 | */ |
||
545 | public static function unCamelCase($str) { |
||
548 | |||
549 | |||
550 | /** |
||
551 | * Translates a string with underscores |
||
552 | * into camel case (e.g. first_name -> firstName) |
||
553 | * |
||
554 | * @param string $str String to convert |
||
555 | * @param bool $capitaliseFirst If true, capitalise the first char in $str |
||
556 | * @return string $str translated into camel case |
||
557 | */ |
||
558 | function camelCase($str, $capitaliseFirst = false) { |
||
566 | |||
567 | |||
568 | /** |
||
569 | * Debug helper function. This is a wrapper for var_dump() that adds |
||
570 | * the <pre /> tags, cleans up newlines and indents, and runs |
||
571 | * htmlentities() before output. |
||
572 | * |
||
573 | * @param mixed $var The variable to dump. |
||
574 | * @param string $label OPTIONAL Label to prepend to output. |
||
575 | * @param bool $echo OPTIONAL Echo output if true. |
||
576 | * @return string |
||
577 | */ |
||
578 | public static function debug($var, $label = null, $echo = true) |
||
604 | |||
605 | |||
606 | /** |
||
607 | * Check the email provided is of a valid syntax and check that the domain portion |
||
608 | * of the email exists in DNS |
||
609 | * |
||
610 | * @param string $email |
||
611 | * @return boolean |
||
612 | */ |
||
613 | public static function emailsyntax_is_valid($email) |
||
639 | |||
640 | |||
641 | /** |
||
642 | * Replicate the provided character the number of times specified |
||
643 | * |
||
644 | * @param string $strInput |
||
645 | * @param integer $intCount |
||
646 | * @return string |
||
647 | */ |
||
648 | public static function repli($strInput, $intCount) |
||
656 | |||
657 | |||
658 | /** |
||
659 | * Sort a multi dimension array by the key specificed |
||
660 | * @return array |
||
661 | */ |
||
662 | public static function array_csort() |
||
685 | |||
686 | |||
687 | /** |
||
688 | * Format a numeric value to the preferred decimal format |
||
689 | * |
||
690 | * @param float|double|string $value |
||
691 | * @param integer $decimals, default 2 |
||
692 | * @return string |
||
693 | */ |
||
694 | public static function decimalFormat($value, $decimals = 2) |
||
698 | |||
699 | |||
700 | /** |
||
701 | * Format a numeric value to the preferred tax rate format |
||
702 | * |
||
703 | * @param float|string $value |
||
704 | * @param integer $decimals, default 6 |
||
705 | * @return string |
||
706 | */ |
||
707 | public static function rateFormat($value, $decimals = 6) |
||
711 | |||
712 | |||
713 | /** |
||
714 | * Round values up to the specified level of precision |
||
715 | * |
||
716 | * @param float|double $value |
||
717 | * @param integer $precision |
||
718 | * @return double |
||
719 | */ |
||
720 | public function roundUp($value, $precision = 0) |
||
730 | |||
731 | |||
732 | /** |
||
733 | * Round half values up to the specified level of precision |
||
734 | * |
||
735 | * @param double $value |
||
736 | * @param integer $precision |
||
737 | * @return double |
||
738 | */ |
||
739 | public function roundHalfUp($value, $precision = 0) |
||
751 | |||
752 | |||
753 | /** |
||
754 | * Round the provided number up by the specified level of significance |
||
755 | * |
||
756 | * @param double $number |
||
757 | * @param integer $significance |
||
758 | * @return double |
||
759 | */ |
||
760 | function roundUpBy($number, $significance = 1) |
||
764 | |||
765 | |||
766 | /** |
||
767 | * Convert a multi-lined string to an array |
||
768 | * |
||
769 | * @param string $valuex the string to be converted |
||
770 | * @param boolean $isVariableList specify that the string contains a list of variables, default false |
||
771 | * @param boolean $returnVariables should array of variables be returned, default false |
||
772 | * @param boolean $returnIsInvalidVariables should invalid variables be returned as part of the array, default false |
||
773 | * @param boolean $returnLowerVariables convert variables to lower case and return those, default false |
||
774 | * @param boolean $returnLowerVariablesAsExtra return lower cases variables as part of the 'extra' array element, default false |
||
775 | * @param string $validKeys regular expression of valid keys |
||
776 | * @param boolean $keysOnly return only the variable names, default false |
||
777 | * @return array|boolean|integer |
||
778 | */ |
||
779 | public static function multiLineRecordToArray($valuex, $isVariableList = false, $returnVariables = false, $returnIsInvalidVariables = false, $returnLowerVariables = false, $returnLowerVariablesAsExtra = false, $validKeys = '', $keysOnly = false) |
||
882 | |||
883 | |||
884 | /** |
||
885 | * Return string converted to upper case |
||
886 | * |
||
887 | * @param string $str string to be converted |
||
888 | * @param string $encoding encoding default UTF-8 |
||
889 | * @return string |
||
890 | */ |
||
891 | public static function fullUpper($str, $encoding = 'UTF-8') |
||
895 | |||
896 | |||
897 | /** |
||
898 | * Return string converted to lower case |
||
899 | * |
||
900 | * @param string $str string to be converted |
||
901 | * @param string $encoding encoding default UTF-8 |
||
902 | * @return string |
||
903 | */ |
||
904 | public static function fullLower($str, $encoding = 'UTF-8') |
||
908 | |||
909 | /** |
||
910 | * Return length of a string |
||
911 | * |
||
912 | * @param string $str string to be checked |
||
913 | * @param string $encoding encoding default UTF-8 |
||
914 | * @return integer |
||
915 | */ |
||
916 | public static function strlen($str, $encoding = 'UTF-8') |
||
920 | |||
921 | |||
922 | /** |
||
923 | * Return a substring of a string |
||
924 | * |
||
925 | * @param string $str string to be conerted |
||
926 | * @param integer $start |
||
927 | * @param integer|null $length |
||
928 | * @param string $encoding encoding default UTF-8 |
||
929 | * @return string |
||
930 | */ |
||
931 | public static function substr($str, $start, $length = null, $encoding = 'UTF-8') |
||
935 | |||
936 | |||
937 | public static function titleCase($string, $delimiters = array(" ", "-", ".", "'", "O'", "Mc"), $exceptions = array("and", "�t", "�s", "utca", "t�r", "krt", "k�r�t", "s�t�ny", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX", "GTFO", "AP", "CD", "OS", "rue", "des", "UK", "USA"), $enc = 'UTF-8') |
||
968 | |||
969 | |||
970 | /** |
||
971 | * Convert a string to an array of lines word wrapped at a fixed number of characters using |
||
972 | * a multi-byte safe word wrap function |
||
973 | * |
||
974 | * @param string $inputText |
||
975 | * @param integer $lines |
||
976 | * @param integer $width |
||
977 | * @param boolean $cut |
||
978 | * @param string $break |
||
979 | * @param string $charSet |
||
980 | * @return string |
||
981 | */ |
||
982 | public static function convertLongTextToLines($inputText, $lines = 5, $width = 35, $cut = true, $break = "\n", $charSet = 'utf-8') |
||
994 | |||
995 | |||
996 | /** |
||
997 | * Multi-byte safe Word wrap |
||
998 | * |
||
999 | * @param string $string |
||
1000 | * @param integer $width |
||
1001 | * @param string $break |
||
1002 | * @param boolean $cut |
||
1003 | * @param string $charset |
||
1004 | * @return string |
||
1005 | */ |
||
1006 | public static function iconv_wordwrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8') |
||
1054 | |||
1055 | |||
1056 | /** |
||
1057 | * Count the number of words within a string |
||
1058 | * @param string $string |
||
1059 | * @return integer |
||
1060 | */ |
||
1061 | public static function wordCount($string) |
||
1070 | |||
1071 | |||
1072 | /** |
||
1073 | * Take a deep copy og an object without leaving in place any references to itself |
||
1074 | * @param mixed $object |
||
1075 | * @return mixed |
||
1076 | */ |
||
1077 | public static function deepCopy($object) |
||
1081 | |||
1082 | |||
1083 | /** |
||
1084 | * Given a string in datetime format YYYY-MM-DD HH:MM:SS return a string |
||
1085 | * that represents how by default the app would like such values to be shown |
||
1086 | * |
||
1087 | * @param string $datetime |
||
1088 | * @return string |
||
1089 | */ |
||
1090 | public static function stdDateTimeFormat($datetime) |
||
1099 | |||
1100 | |||
1101 | /** |
||
1102 | * |
||
1103 | * @param string $key the key identifying the flash message. Note that flash messages |
||
1104 | * and normal session variables share the same name space. If you have a normal |
||
1105 | * session variable using the same name, its value will be overwritten by this method. |
||
1106 | * Can be [alert|note]-[success|info|warning|danger|error] e.g. alert-success, note-success or success |
||
1107 | * @param mixed $value flash message |
||
1108 | * @param string|false $title [optional] Title to be used in flash message when supported |
||
1109 | * @param boolean $dismiss [optional] Allow flash message to be dismissable |
||
1110 | * @param string|false $class [optional] Extra class attribute to be added to flash message div when supported |
||
1111 | * @param string|array|false $extra [optional] Array of extra values to be listed (li) with the flash message when supported |
||
1112 | */ |
||
1113 | public static function setFlash($key, $value, $title = false, $dismiss = true, $class = false, $extra = false) |
||
1124 | |||
1125 | public static function sessionArrayRecord($value, $attribute, $default = false, $element = 'auto') |
||
1137 | |||
1138 | /** |
||
1139 | * Check to see if a string exists within another that has been structured for such a search |
||
1140 | * @param string $needle 'stack' |
||
1141 | * @param string $haystack '/hay/stack/to/search/' |
||
1142 | * @param string $delimiter [optional] default '/' |
||
1143 | * @return boolean |
||
1144 | */ |
||
1145 | public static function in($needle, $haystack, $delimiter = '/') |
||
1155 | |||
1156 | /** |
||
1157 | * Obtain class name |
||
1158 | * |
||
1159 | * @return string the fully qualified name of this class. |
||
1160 | */ |
||
1161 | public static function className() |
||
1165 | |||
1166 | } |
||
1167 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.