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 text_Core 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 text_Core, and based on these observations, apply Extract Interface, too.
1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
12 | class text_Core |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Limits a phrase to a given number of words. |
||
17 | * |
||
18 | * @param string phrase to limit words of |
||
19 | * @param integer number of words to limit to |
||
20 | * @param string end character or entity |
||
21 | * @return string |
||
22 | */ |
||
23 | public static function limit_words($str, $limit = 100, $end_char = null) |
||
42 | |||
43 | /** |
||
44 | * Limits a phrase to a given number of characters. |
||
45 | * |
||
46 | * @param string phrase to limit characters of |
||
47 | * @param integer number of characters to limit to |
||
48 | * @param string end character or entity |
||
49 | * @param boolean enable or disable the preservation of words while limiting |
||
50 | * @return string |
||
51 | */ |
||
52 | public static function limit_chars($str, $limit = 100, $end_char = null, $preserve_words = false) |
||
74 | |||
75 | /** |
||
76 | * Alternates between two or more strings. |
||
77 | * |
||
78 | * @param string strings to alternate between |
||
79 | * @return string |
||
80 | */ |
||
81 | public static function alternate() |
||
93 | |||
94 | /** |
||
95 | * Generates a random string of a given type and length. |
||
96 | * |
||
97 | * @param string a type of pool, or a string of characters to use as the pool |
||
98 | * @param integer length of string to return |
||
99 | * @return string |
||
100 | * |
||
101 | * @tutorial alnum alpha-numeric characters |
||
102 | * @tutorial alpha alphabetical characters |
||
103 | * @tutorial hexdec hexadecimal characters, 0-9 plus a-f |
||
104 | * @tutorial numeric digit characters, 0-9 |
||
105 | * @tutorial nozero digit characters, 1-9 |
||
106 | * @tutorial distinct clearly distinct alpha-numeric characters |
||
107 | */ |
||
108 | public static function random($type = 'alnum', $length = 8) |
||
162 | |||
163 | /** |
||
164 | * Reduces multiple slashes in a string to single slashes. |
||
165 | * |
||
166 | * @param string string to reduce slashes of |
||
167 | * @return string |
||
168 | */ |
||
169 | public static function reduce_slashes($str) |
||
173 | |||
174 | /** |
||
175 | * Replaces the given words with a string. |
||
176 | * |
||
177 | * @param string phrase to replace words in |
||
178 | * @param array words to replace |
||
179 | * @param string replacement string |
||
180 | * @param boolean replace words across word boundries (space, period, etc) |
||
181 | * @return string |
||
182 | */ |
||
183 | public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = false) |
||
205 | |||
206 | /** |
||
207 | * Finds the text that is similar between a set of words. |
||
208 | * |
||
209 | * @param array words to find similar text of |
||
210 | * @return string |
||
211 | */ |
||
212 | public static function similar(array $words) |
||
229 | |||
230 | /** |
||
231 | * Converts text email addresses and anchors into links. |
||
232 | * |
||
233 | * @param string text to auto link |
||
234 | * @return string |
||
235 | */ |
||
236 | public static function auto_link($text) |
||
241 | |||
242 | /** |
||
243 | * Converts text anchors into links. |
||
244 | * |
||
245 | * @param string text to auto link |
||
246 | * @return string |
||
247 | */ |
||
248 | public static function auto_link_urls($text) |
||
268 | |||
269 | /** |
||
270 | * Converts text email addresses into links. |
||
271 | * |
||
272 | * @param string text to auto link |
||
273 | * @return string |
||
274 | */ |
||
275 | public static function auto_link_emails($text) |
||
289 | |||
290 | /** |
||
291 | * Automatically applies <p> and <br /> markup to text. Basically nl2br() on steroids. |
||
292 | * |
||
293 | * @param string subject |
||
294 | * @return string |
||
295 | */ |
||
296 | public static function auto_p($str) |
||
336 | |||
337 | /** |
||
338 | * Returns human readable sizes. |
||
339 | * @see Based on original functions written by: |
||
340 | * @see Aidan Lister: http://aidanlister.com/repos/v/function.size_readable.php |
||
341 | * @see Quentin Zervaas: http://www.phpriot.com/d/code/strings/filesize-format/ |
||
342 | * |
||
343 | * @param integer size in bytes |
||
344 | * @param string a definitive unit |
||
345 | * @param string the return string format |
||
346 | * @param boolean whether to use SI prefixes or IEC |
||
347 | * @return string |
||
348 | */ |
||
349 | public static function bytes($bytes, $force_unit = null, $format = null, $si = true) |
||
372 | |||
373 | /** |
||
374 | * Prevents widow words by inserting a non-breaking space between the last two words. |
||
375 | * @see http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin |
||
376 | * |
||
377 | * @param string string to remove widows from |
||
378 | * @return string |
||
379 | */ |
||
380 | public static function widont($str) |
||
391 | } // End text |
||
392 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.