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 TAR 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 TAR, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class TAR extends Archive { |
||
| 36 | const PLAIN = 0; |
||
| 37 | const GZIP = 1; |
||
| 38 | const BZIP = 2; |
||
| 39 | |||
| 40 | private $fileList; |
||
| 41 | private $cachedHeaders; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \Archive_Tar tar |
||
| 45 | */ |
||
| 46 | private $tar = null; |
||
| 47 | private $path; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $source |
||
| 51 | */ |
||
| 52 | function __construct($source) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * try to detect the type of tar compression |
||
| 60 | * |
||
| 61 | * @param string $file |
||
| 62 | * @return integer |
||
| 63 | */ |
||
| 64 | static public function getTarType($file) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * add an empty folder to the archive |
||
| 86 | * |
||
| 87 | * @param string $path |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | function addFolder($path) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * add a file to the archive |
||
| 115 | * |
||
| 116 | * @param string $path |
||
| 117 | * @param string $source either a local file or string data |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | function addFile($path, $source = '') { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * rename a file or folder in the archive |
||
| 135 | * |
||
| 136 | * @param string $source |
||
| 137 | * @param string $dest |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | function rename($source, $dest) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $file |
||
| 157 | */ |
||
| 158 | private function getHeader($file) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * get the uncompressed size of a file in the archive |
||
| 176 | * |
||
| 177 | * @param string $path |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | function filesize($path) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * get the last modified time of a file in the archive |
||
| 187 | * |
||
| 188 | * @param string $path |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | function mtime($path) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * get the files in a folder |
||
| 198 | * |
||
| 199 | * @param string $path |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | function getFolder($path) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * get all files in the archive |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | function getFiles() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * get the content of a file |
||
| 245 | * |
||
| 246 | * @param string $path |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | function getFile($path) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * extract a single file from the archive |
||
| 255 | * |
||
| 256 | * @param string $path |
||
| 257 | * @param string $dest |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | function extractFile($path, $dest) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * extract the archive |
||
| 279 | * |
||
| 280 | * @param string $dest |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | function extract($dest) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * check if a file or folder exists in the archive |
||
| 289 | * |
||
| 290 | * @param string $path |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | function fileExists($path) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * remove a file or folder from the archive |
||
| 318 | * |
||
| 319 | * @param string $path |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | function remove($path) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * get a file handler |
||
| 341 | * |
||
| 342 | * @param string $path |
||
| 343 | * @param string $mode |
||
| 344 | * @return resource |
||
| 345 | */ |
||
| 346 | function getStream($path, $mode) { |
||
| 366 | |||
| 367 | private static $tempFiles = array(); |
||
| 368 | |||
| 369 | /** |
||
| 370 | * write back temporary files |
||
| 371 | */ |
||
| 372 | function writeBack($tmpFile) { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * reopen the archive to ensure everything is written |
||
| 381 | */ |
||
| 382 | private function reopen() { |
||
| 390 | } |
||
| 391 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.