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 | 58 | 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 | 44 | 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 | 31 | public function read($pasteid) |
|
95 | |||
96 | /** |
||
97 | * Delete a paste and its discussion. |
||
98 | * |
||
99 | * @access public |
||
100 | * @param string $pasteid |
||
101 | */ |
||
102 | 13 | public function delete($pasteid) |
|
126 | |||
127 | /** |
||
128 | * Test if a paste exists. |
||
129 | * |
||
130 | * @access public |
||
131 | * @param string $pasteid |
||
132 | * @return bool |
||
133 | */ |
||
134 | 58 | public function exists($pasteid) |
|
138 | |||
139 | /** |
||
140 | * Create a comment in a paste. |
||
141 | * |
||
142 | * @access public |
||
143 | * @param string $pasteid |
||
144 | * @param string $parentid |
||
145 | * @param string $commentid |
||
146 | * @param array $comment |
||
147 | * @return bool |
||
148 | */ |
||
149 | 4 | public function createComment($pasteid, $parentid, $commentid, $comment) |
|
161 | |||
162 | /** |
||
163 | * Read all comments of paste. |
||
164 | * |
||
165 | * @access public |
||
166 | * @param string $pasteid |
||
167 | * @return array |
||
168 | */ |
||
169 | 17 | public function readComments($pasteid) |
|
200 | |||
201 | /** |
||
202 | * Test if a comment exists. |
||
203 | * |
||
204 | * @access public |
||
205 | * @param string $pasteid |
||
206 | * @param string $parentid |
||
207 | * @param string $commentid |
||
208 | * @return bool |
||
209 | */ |
||
210 | 7 | public function existsComment($pasteid, $parentid, $commentid) |
|
217 | |||
218 | /** |
||
219 | * Returns up to batch size number of paste ids that have expired |
||
220 | * |
||
221 | * @access private |
||
222 | * @param int $batchsize |
||
223 | * @return array |
||
224 | */ |
||
225 | 1 | protected function _getExpiredPastes($batchsize) |
|
284 | |||
285 | /** |
||
286 | * Convert paste id to storage path. |
||
287 | * |
||
288 | * The idea is to creates subdirectories in order to limit the number of files per directory. |
||
289 | * (A high number of files in a single directory can slow things down.) |
||
290 | * eg. "f468483c313401e8" will be stored in "data/f4/68/f468483c313401e8" |
||
291 | * High-trafic websites may want to deepen the directory structure (like Squid does). |
||
292 | * |
||
293 | * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/' |
||
294 | * |
||
295 | * @access private |
||
296 | * @static |
||
297 | * @param string $dataid |
||
298 | * @return string |
||
299 | */ |
||
300 | 58 | private static function _dataid2path($dataid) |
|
307 | |||
308 | /** |
||
309 | * Convert paste id to discussion storage path. |
||
310 | * |
||
311 | * eg. input 'e3570978f9e4aa90' --> output 'data/e3/57/e3570978f9e4aa90.discussion/' |
||
312 | * |
||
313 | * @access private |
||
314 | * @static |
||
315 | * @param string $dataid |
||
316 | * @return string |
||
317 | */ |
||
318 | 24 | private static function _dataid2discussionpath($dataid) |
|
323 | |||
324 | /** |
||
325 | * Check that the given element is a valid first level directory. |
||
326 | * |
||
327 | * @access private |
||
328 | * @static |
||
329 | * @param string $element |
||
330 | * @return bool |
||
331 | */ |
||
332 | 1 | private static function _isFirstLevelDir($element) |
|
337 | |||
338 | /** |
||
339 | * Check that the given element is a valid second level directory. |
||
340 | * |
||
341 | * @access private |
||
342 | * @static |
||
343 | * @param string $element |
||
344 | * @return bool |
||
345 | */ |
||
346 | 1 | private static function _isSecondLevelDir($element) |
|
350 | } |
||
351 |