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 AString 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 AString, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class AString extends AnyString |
||
6 | { |
||
7 | protected $token = false; |
||
8 | |||
9 | View Code Duplication | public function toArray($delim = '', $limit = null) |
|
22 | |||
23 | // INFORMATIONAL METHODS |
||
24 | |||
25 | public function charAt($offset) |
||
29 | |||
30 | /** |
||
31 | * @param integer $offset |
||
32 | */ |
||
33 | public function charCodeAt($offset) |
||
37 | |||
38 | public function compareTo($str, $flags = self::NORMAL, $length = 1) |
||
58 | |||
59 | public function indexOf($needle, $offset = 0, $flags = self::NORMAL) |
||
72 | |||
73 | public function length() |
||
77 | |||
78 | public function substr($start, $length = 'omitted') |
||
85 | |||
86 | // MODIFYING METHODS |
||
87 | |||
88 | public function append($str) |
||
92 | |||
93 | public function chunk($length = 76, $ending = "\r\n") |
||
97 | |||
98 | public function escape($flags = self::NORMAL, $charlist = '') |
||
113 | |||
114 | public function insertAt($str, $offset) |
||
118 | |||
119 | public function pad($newlength, $padding = ' ', $flags = self::END) |
||
123 | |||
124 | public function prepend($str) |
||
128 | |||
129 | public function remove($str, $flags = self::NORMAL) |
||
133 | |||
134 | public function removeSubstr($start, $length = null) |
||
138 | |||
139 | public function repeat($times) |
||
143 | |||
144 | /** |
||
145 | * @param string $replace |
||
146 | */ |
||
147 | public function replace($search, $replace, $flags = self::NORMAL) |
||
154 | |||
155 | public function replaceSubstr($replacement, $start, $length = null) |
||
162 | |||
163 | public function reverse() |
||
167 | |||
168 | public function shuffle() |
||
172 | |||
173 | public function translate($search, $replace = '') |
||
180 | |||
181 | View Code Duplication | public function trim($mask = " \t\n\r\0\x0B", $flags = self::BOTH_ENDS) |
|
193 | |||
194 | View Code Duplication | public function unescape($flags = self::NORMAL) |
|
206 | |||
207 | public function wordwrap($width = 75, $break = "\n") |
||
211 | |||
212 | public function wordwrapBreaking($width = 75, $break = "\n") |
||
216 | |||
217 | // TESTING METHODS |
||
218 | |||
219 | public function contains($needle, $offset = 0, $flags = self::NORMAL) |
||
226 | |||
227 | public function countSubstr($needle, $offset = 0, $length = null) |
||
234 | |||
235 | public function endsWith($str, $flags = self::NORMAL) |
||
241 | |||
242 | public function equals($str) |
||
249 | |||
250 | public function isAscii() |
||
261 | |||
262 | public function isEmpty() |
||
266 | |||
267 | public function startsWith($str, $flags = self::NORMAL) |
||
272 | |||
273 | // INTERFACE IMPLEMENTATION METHODS |
||
274 | |||
275 | public function count() |
||
279 | |||
280 | public function current() |
||
284 | |||
285 | public function offsetGet($offset) |
||
289 | |||
290 | public function offsetSet($offset, $value) |
||
294 | |||
295 | public function offsetUnset($offset) |
||
299 | } |
||
300 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.