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 StringUtil 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 StringUtil, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class StringUtil |
||
|
|||
23 | { |
||
24 | 277 | public static function parseString($value, $nullable = true) |
|
44 | |||
45 | 28 | public static function parseBoolean($value, $nullable = true) |
|
46 | { |
||
47 | 28 | if ($nullable && (null === $value || 'null' === $value)) { |
|
48 | 4 | return null; |
|
49 | } |
||
50 | |||
51 | 24 | if (is_bool($value)) { |
|
52 | 2 | return $value; |
|
53 | } |
||
54 | |||
55 | 22 | if (is_string($value) || is_int($value)) { |
|
56 | 20 | switch ((string) $value) { |
|
57 | case '': |
||
58 | case 'false': |
||
59 | case '0': |
||
60 | case 'no': |
||
61 | case 'off': |
||
62 | 8 | return false; |
|
63 | |||
64 | case 'true': |
||
65 | case '1': |
||
66 | case 'yes': |
||
67 | 5 | case 'on': |
|
68 | 7 | return true; |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 7 | throw new InvalidValueException(sprintf( |
|
73 | 7 | 'The value "%s" cannot be parsed as boolean.', |
|
74 | $value |
||
75 | )); |
||
76 | } |
||
77 | |||
78 | 36 | View Code Duplication | public static function parseInteger($value, $nullable = true) |
79 | { |
||
80 | 36 | if ($nullable && (null === $value || 'null' === $value)) { |
|
81 | 4 | return null; |
|
82 | } |
||
83 | |||
84 | 32 | if (is_numeric($value) || is_bool($value)) { |
|
85 | 20 | return (int) $value; |
|
86 | } |
||
87 | |||
88 | 12 | throw new InvalidValueException(sprintf( |
|
89 | 12 | 'The value "%s" cannot be parsed as integer.', |
|
90 | $value |
||
91 | )); |
||
92 | } |
||
93 | |||
94 | 30 | View Code Duplication | public static function parseFloat($value, $nullable = true) |
95 | { |
||
96 | 30 | if ($nullable && (null === $value || 'null' === $value)) { |
|
97 | 4 | return null; |
|
98 | } |
||
99 | |||
100 | 26 | if (is_numeric($value) || is_bool($value)) { |
|
101 | 14 | return (float) $value; |
|
102 | } |
||
103 | |||
104 | 12 | throw new InvalidValueException(sprintf( |
|
105 | 12 | 'The value "%s" cannot be parsed as float.', |
|
106 | $value |
||
107 | )); |
||
108 | } |
||
109 | |||
110 | 21 | public static function getLength($string, Formatter $formatter = null) |
|
111 | { |
||
112 | 21 | if ($formatter) { |
|
113 | 21 | $string = $formatter->removeFormat($string); |
|
114 | } |
||
115 | |||
116 | 21 | if (!function_exists('mb_strwidth')) { |
|
117 | return strlen($string); |
||
118 | } |
||
119 | |||
120 | 21 | if (false === $encoding = mb_detect_encoding($string)) { |
|
121 | return strlen($string); |
||
122 | } |
||
123 | |||
124 | 21 | return mb_strwidth($string, $encoding); |
|
125 | } |
||
126 | |||
127 | 14 | View Code Duplication | public static function getMaxWordLength($string, Formatter $formatter = null) |
128 | { |
||
129 | 14 | if ($formatter) { |
|
130 | 14 | $string = $formatter->removeFormat($string); |
|
131 | } |
||
132 | |||
133 | 14 | $maxLength = 0; |
|
134 | 14 | $words = preg_split('/\s+/', $string); |
|
135 | |||
136 | 14 | foreach ($words as $word) { |
|
137 | // No need to pass the formatter because the tags are already |
||
138 | // removed |
||
139 | 14 | $maxLength = max($maxLength, self::getLength($word)); |
|
140 | } |
||
141 | |||
142 | 14 | return $maxLength; |
|
143 | } |
||
144 | |||
145 | 14 | View Code Duplication | public static function getMaxLineLength($string, Formatter $formatter = null) |
146 | { |
||
147 | 14 | if ($formatter) { |
|
148 | 14 | $string = $formatter->removeFormat($string); |
|
149 | } |
||
150 | |||
151 | 14 | $maxLength = 0; |
|
152 | 14 | $lines = explode("\n", $string); |
|
153 | |||
154 | 14 | foreach ($lines as $word) { |
|
155 | // No need to pass the formatter because the tags are already |
||
156 | // removed |
||
157 | 14 | $maxLength = max($maxLength, self::getLength($word)); |
|
158 | } |
||
159 | |||
160 | 14 | return $maxLength; |
|
161 | } |
||
162 | |||
163 | private function __construct() |
||
166 | } |
||
167 |