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 FilesystemHelper 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 FilesystemHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class FilesystemHelper { |
||
25 | |||
26 | /** |
||
27 | * Wrapper for filemtime function |
||
28 | * @param string $path |
||
29 | * @return array |
||
30 | */ |
||
31 | public function filemtime($path){ |
||
34 | |||
35 | /** |
||
36 | * Wrapper for scandir function |
||
37 | * @param string $path |
||
38 | * @return array |
||
39 | */ |
||
40 | public function scandir($path){ |
||
43 | |||
44 | /** |
||
45 | * Wrapper for file_exists function |
||
46 | * @param string $path |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function fileExists($path){ |
||
52 | |||
53 | /** |
||
54 | * Wrapper for is_writable function |
||
55 | * @param string $path |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function isWritable($path){ |
||
61 | |||
62 | /** |
||
63 | * Wrapper for is_dir function |
||
64 | * @param string $path |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function isDir($path){ |
||
70 | |||
71 | /** |
||
72 | * Wrapper for md5_file function |
||
73 | * @param string $path |
||
74 | * @return string |
||
75 | */ |
||
76 | public function md5File($path){ |
||
79 | |||
80 | /** |
||
81 | * Wrapper for mkdir |
||
82 | * @param string $path |
||
83 | * @param bool $isRecursive |
||
84 | * @throws \Exception on error |
||
85 | */ |
||
86 | public function mkdir($path, $isRecursive = false){ |
||
91 | |||
92 | /** |
||
93 | * Copy recursive |
||
94 | * @param string $src - source path |
||
95 | * @param string $dest - destination path |
||
96 | * @throws \Exception on error |
||
97 | */ |
||
98 | public function copyr($src, $dest, $stopOnError = true){ |
||
121 | |||
122 | /** |
||
123 | * Moves file/directory |
||
124 | * @param string $src - source path |
||
125 | * @param string $dest - destination path |
||
126 | * @throws \Exception on error |
||
127 | */ |
||
128 | public function move($src, $dest){ |
||
133 | |||
134 | /** |
||
135 | * Check permissions recursive |
||
136 | * @param string $src - path to check |
||
137 | * @param Collection $collection - object to store incorrect permissions |
||
138 | */ |
||
139 | public function checkr($src, $collection){ |
||
158 | |||
159 | public function removeIfExists($path) { |
||
170 | |||
171 | protected function rmdirr($dir) { |
||
189 | |||
190 | /** |
||
191 | * |
||
192 | * @param string $old |
||
193 | * @param string $new |
||
194 | * @param string $temp |
||
195 | * @param string $dirName |
||
196 | */ |
||
197 | public function tripleMove($old, $new, $temp, $dirName){ |
||
205 | |||
206 | } |
||
207 |
If you suppress an error, we recommend checking for the error condition explicitly: