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 Filesystem 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 Filesystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Filesystem extends AbstractData |
||
24 | { |
||
25 | /** |
||
26 | * get instance of singleton |
||
27 | * |
||
28 | * @access public |
||
29 | * @static |
||
30 | * @param array $options |
||
31 | * @return Filesystem |
||
32 | */ |
||
33 | 60 | public static function getInstance($options = null) |
|
48 | |||
49 | /** |
||
50 | * Create a paste. |
||
51 | * |
||
52 | * @access public |
||
53 | * @param string $pasteid |
||
54 | * @param array $paste |
||
55 | * @return bool |
||
56 | */ |
||
57 | 45 | public function create($pasteid, $paste) |
|
69 | |||
70 | /** |
||
71 | * Read a paste. |
||
72 | * |
||
73 | * @access public |
||
74 | * @param string $pasteid |
||
75 | * @return stdClass|false |
||
76 | */ |
||
77 | 33 | public function read($pasteid) |
|
93 | |||
94 | /** |
||
95 | * Delete a paste and its discussion. |
||
96 | * |
||
97 | * @access public |
||
98 | * @param string $pasteid |
||
99 | */ |
||
100 | 14 | public function delete($pasteid) |
|
124 | |||
125 | /** |
||
126 | * Test if a paste exists. |
||
127 | * |
||
128 | * @access public |
||
129 | * @param string $pasteid |
||
130 | * @return bool |
||
131 | */ |
||
132 | 60 | public function exists($pasteid) |
|
170 | |||
171 | /** |
||
172 | * Create a comment in a paste. |
||
173 | * |
||
174 | * @access public |
||
175 | * @param string $pasteid |
||
176 | * @param string $parentid |
||
177 | * @param string $commentid |
||
178 | * @param array $comment |
||
179 | * @return bool |
||
180 | */ |
||
181 | 4 | public function createComment($pasteid, $parentid, $commentid, $comment) |
|
193 | |||
194 | /** |
||
195 | * Read all comments of paste. |
||
196 | * |
||
197 | * @access public |
||
198 | * @param string $pasteid |
||
199 | * @return array |
||
200 | */ |
||
201 | 19 | public function readComments($pasteid) |
|
231 | |||
232 | /** |
||
233 | * Test if a comment exists. |
||
234 | * |
||
235 | * @access public |
||
236 | * @param string $pasteid |
||
237 | * @param string $parentid |
||
238 | * @param string $commentid |
||
239 | * @return bool |
||
240 | */ |
||
241 | 8 | public function existsComment($pasteid, $parentid, $commentid) |
|
248 | |||
249 | /** |
||
250 | * Returns up to batch size number of paste ids that have expired |
||
251 | * |
||
252 | * @access private |
||
253 | * @param int $batchsize |
||
254 | * @return array |
||
255 | */ |
||
256 | 2 | protected function _getExpiredPastes($batchsize) |
|
322 | |||
323 | /** |
||
324 | * Convert paste id to storage path. |
||
325 | * |
||
326 | * The idea is to creates subdirectories in order to limit the number of files per directory. |
||
327 | * (A high number of files in a single directory can slow things down.) |
||
328 | * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8" |
||
329 | * High-trafic websites may want to deepen the directory structure (like Squid does). |
||
330 | * |
||
331 | * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/' |
||
332 | * |
||
333 | * @access private |
||
334 | * @static |
||
335 | * @param string $dataid |
||
336 | * @return string |
||
337 | */ |
||
338 | 60 | private static function _dataid2path($dataid) |
|
345 | |||
346 | /** |
||
347 | * Convert paste id to discussion storage path. |
||
348 | * |
||
349 | * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/' |
||
350 | * |
||
351 | * @access private |
||
352 | * @static |
||
353 | * @param string $dataid |
||
354 | * @return string |
||
355 | */ |
||
356 | 26 | private static function _dataid2discussionpath($dataid) |
|
361 | |||
362 | /** |
||
363 | * Check that the given element is a valid first level directory. |
||
364 | * |
||
365 | * @access private |
||
366 | * @static |
||
367 | * @param string $element |
||
368 | * @return bool |
||
369 | */ |
||
370 | 2 | private static function _isFirstLevelDir($element) |
|
375 | |||
376 | /** |
||
377 | * Check that the given element is a valid second level directory. |
||
378 | * |
||
379 | * @access private |
||
380 | * @static |
||
381 | * @param string $element |
||
382 | * @return bool |
||
383 | */ |
||
384 | 2 | private static function _isSecondLevelDir($element) |
|
388 | } |
||
389 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.