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:
1 | <?php |
||
24 | class FilesystemHelper { |
||
25 | |||
26 | /** |
||
27 | * Wrapper for scandir function |
||
28 | * @param string $path |
||
29 | * @return array |
||
30 | */ |
||
31 | public function scandir($path){ |
||
34 | |||
35 | /** |
||
36 | * Wrapper for file_exists function |
||
37 | * @param string $path |
||
38 | * @return bool |
||
39 | */ |
||
40 | public function fileExists($path){ |
||
43 | |||
44 | /** |
||
45 | * Wrapper for is_writable function |
||
46 | * @param string $path |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function isWritable($path){ |
||
52 | |||
53 | /** |
||
54 | * Wrapper for is_dir function |
||
55 | * @param string $path |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function isDir($path){ |
||
59 | return is_dir($path); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Wrapper for md5_file function |
||
64 | * @param string $path |
||
65 | * @return string |
||
66 | */ |
||
67 | public function md5File($path){ |
||
70 | |||
71 | /** |
||
72 | * Wrapper for mkdir |
||
73 | * @param string $path |
||
74 | * @param bool $isRecursive |
||
75 | * @throws \Exception on error |
||
76 | */ |
||
77 | public function mkdir($path, $isRecursive = false){ |
||
82 | |||
83 | /** |
||
84 | * Copy recursive |
||
85 | * @param string $src - source path |
||
86 | * @param string $dest - destination path |
||
87 | * @throws \Exception on error |
||
88 | */ |
||
89 | public function copyr($src, $dest, $stopOnError = true){ |
||
112 | |||
113 | /** |
||
114 | * Moves file/directory |
||
115 | * @param string $src - source path |
||
116 | * @param string $dest - destination path |
||
117 | * @throws \Exception on error |
||
118 | */ |
||
119 | public function move($src, $dest){ |
||
124 | |||
125 | /** |
||
126 | * Check permissions recursive |
||
127 | * @param string $src - path to check |
||
128 | * @param Collection $collection - object to store incorrect permissions |
||
129 | */ |
||
130 | public function checkr($src, $collection){ |
||
149 | |||
150 | public function removeIfExists($path) { |
||
161 | |||
162 | protected function rmdirr($dir) { |
||
180 | |||
181 | /** |
||
182 | * |
||
183 | * @param string $old |
||
184 | * @param string $new |
||
185 | * @param string $temp |
||
186 | * @param string $dirName |
||
187 | */ |
||
188 | public function tripleMove($old, $new, $temp, $dirName){ |
||
196 | |||
197 | } |
||
198 |
If you suppress an error, we recommend checking for the error condition explicitly: