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  | 
            ||
| 29 | class FilesystemHelper { | 
            ||
| 30 | |||
| 31 | /**  | 
            ||
| 32 | * Wrapper for filemtime function  | 
            ||
| 33 | * @param string $path  | 
            ||
| 34 | * @return integer  | 
            ||
| 35 | */  | 
            ||
| 36 | 	public function filemtime($path){ | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * Wrapper for scandir function.  | 
            ||
| 42 | * Filters current and parent directories  | 
            ||
| 43 | * @param string $path  | 
            ||
| 44 | * @return array  | 
            ||
| 45 | */  | 
            ||
| 46 | 	public function scandirFiltered($path){ | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * Wrapper for scandir function  | 
            ||
| 56 | * @param string $path  | 
            ||
| 57 | * @return array  | 
            ||
| 58 | */  | 
            ||
| 59 | 	public function scandir($path){ | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * Wrapper for file_exists function  | 
            ||
| 65 | * @param string $path  | 
            ||
| 66 | * @return bool  | 
            ||
| 67 | */  | 
            ||
| 68 | 	public function fileExists($path){ | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Wrapper for is_writable function  | 
            ||
| 74 | * @param string $path  | 
            ||
| 75 | * @return bool  | 
            ||
| 76 | */  | 
            ||
| 77 | 	public function isWritable($path){ | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * Wrapper for is_dir function  | 
            ||
| 83 | * @param string $path  | 
            ||
| 84 | * @return bool  | 
            ||
| 85 | */  | 
            ||
| 86 | 	public function isDir($path){ | 
            ||
| 89 | |||
| 90 | /**  | 
            ||
| 91 | * Wrapper for md5_file function  | 
            ||
| 92 | * @param string $path  | 
            ||
| 93 | * @return string  | 
            ||
| 94 | */  | 
            ||
| 95 | 	public function md5File($path){ | 
            ||
| 98 | |||
| 99 | /**  | 
            ||
| 100 | * Wrapper for mkdir  | 
            ||
| 101 | * @param string $path  | 
            ||
| 102 | * @param bool $isRecursive  | 
            ||
| 103 | * @throws \Exception on error  | 
            ||
| 104 | */  | 
            ||
| 105 | 	public function mkdir($path, $isRecursive = false){ | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Copy recursive  | 
            ||
| 113 | * @param string $src - source path  | 
            ||
| 114 | * @param string $dest - destination path  | 
            ||
| 115 | * @throws \Exception on error  | 
            ||
| 116 | */  | 
            ||
| 117 | 	public function copyr($src, $dest, $stopOnError = true){ | 
            ||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * Moves file/directory  | 
            ||
| 143 | * @param string $src - source path  | 
            ||
| 144 | * @param string $dest - destination path  | 
            ||
| 145 | * @throws \Exception on error  | 
            ||
| 146 | */  | 
            ||
| 147 | 	public function move($src, $dest){ | 
            ||
| 152 | |||
| 153 | /**  | 
            ||
| 154 | * Check permissions recursive  | 
            ||
| 155 | * @param string $src - path to check  | 
            ||
| 156 | * @param Collection $collection - object to store incorrect permissions  | 
            ||
| 157 | */  | 
            ||
| 158 | 	public function checkr($src, $collection){ | 
            ||
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * @param string $path  | 
            ||
| 180 | */  | 
            ||
| 181 | 	public function removeIfExists($path) { | 
            ||
| 192 | |||
| 193 | /**  | 
            ||
| 194 | * @param $dir  | 
            ||
| 195 | * @return bool  | 
            ||
| 196 | */  | 
            ||
| 197 | 	public function rmdirr($dir) { | 
            ||
| 215 | |||
| 216 | /**  | 
            ||
| 217 | *  | 
            ||
| 218 | * @param string $old  | 
            ||
| 219 | * @param string $new  | 
            ||
| 220 | * @param string $temp  | 
            ||
| 221 | * @param string $dirName  | 
            ||
| 222 | */  | 
            ||
| 223 | 	public function tripleMove($old, $new, $temp, $dirName){ | 
            ||
| 233 | |||
| 234 | }  | 
            ||
| 235 | 
If you suppress an error, we recommend checking for the error condition explicitly: