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 FilesystemRepository 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 FilesystemRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 64 | class FilesystemRepository extends AbstractRepository implements EditableRepository |
||
| 65 | { |
||
| 66 | /** |
||
| 67 | * @var bool|null |
||
| 68 | */ |
||
| 69 | private static $symlinkSupported; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $baseDir; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $symlink; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $relative; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var Filesystem |
||
| 88 | */ |
||
| 89 | private $filesystem; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns whether symlinks are supported in the local environment. |
||
| 93 | * |
||
| 94 | * @return bool Returns `true` if symlinks are supported. |
||
| 95 | */ |
||
| 96 | 256 | public static function isSymlinkSupported() |
|
| 97 | { |
||
| 98 | 256 | if (null === self::$symlinkSupported) { |
|
| 99 | // http://php.net/manual/en/function.symlink.php |
||
| 100 | // Symlinks are only supported on Windows Vista, Server 2008 or |
||
| 101 | // greater on PHP 5.3+ |
||
| 102 | 1 | if (defined('PHP_WINDOWS_VERSION_MAJOR')) { |
|
| 103 | self::$symlinkSupported = PHP_WINDOWS_VERSION_MAJOR >= 6; |
||
| 104 | } else { |
||
| 105 | 1 | self::$symlinkSupported = true; |
|
| 106 | } |
||
| 107 | 1 | } |
|
| 108 | |||
| 109 | 256 | return self::$symlinkSupported; |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Creates a new repository. |
||
| 114 | * |
||
| 115 | * @param string $baseDir The base directory of the repository on the file |
||
| 116 | * system. |
||
| 117 | * @param bool $symlink Whether to use symbolic links for added files. If |
||
| 118 | * symbolic links are not supported on the current |
||
| 119 | * system, the repository will create hard copies |
||
| 120 | * instead. |
||
| 121 | * @param bool $relative Whether to create relative symbolic links. If |
||
| 122 | * relative links are not supported on the current |
||
| 123 | * system, the repository will create absolute links |
||
| 124 | * instead. |
||
| 125 | */ |
||
| 126 | 330 | public function __construct($baseDir = '/', $symlink = true, $relative = true) |
|
| 127 | { |
||
| 128 | 330 | Assert::directory($baseDir); |
|
| 129 | 330 | Assert::boolean($symlink); |
|
| 130 | |||
| 131 | 330 | $this->baseDir = rtrim(Path::canonicalize($baseDir), '/'); |
|
| 132 | 330 | $this->symlink = $symlink && self::isSymlinkSupported(); |
|
| 133 | 330 | $this->relative = $this->symlink && $relative; |
|
| 134 | 330 | $this->filesystem = new Filesystem(); |
|
| 135 | 330 | } |
|
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | 135 | View Code Duplication | public function get($path) |
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | 41 | public function find($query, $language = 'glob') |
|
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | 61 | public function contains($query, $language = 'glob') |
|
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | 28 | public function hasChildren($path) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | 42 | public function listChildren($path) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | 295 | public function add($path, $resource) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | 49 | public function remove($query, $language = 'glob') |
|
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | 4 | public function clear() |
|
| 253 | { |
||
| 254 | 4 | $iterator = $this->getDirectoryIterator($this->baseDir); |
|
| 255 | 4 | $removed = 0; |
|
| 256 | |||
| 257 | 4 | foreach ($iterator as $filesystemPath) { |
|
| 258 | 4 | $this->removeResource($filesystemPath, $removed); |
|
| 259 | 4 | } |
|
| 260 | |||
| 261 | 4 | return $removed; |
|
| 262 | } |
||
| 263 | |||
| 264 | 279 | private function ensureDirectoryExists($path) |
|
| 280 | |||
| 281 | 279 | private function addResource($path, PuliResource $resource, $checkParentsForSymlinks = true) |
|
| 282 | { |
||
| 283 | 279 | $pathInBaseDir = $this->baseDir.$path; |
|
| 284 | 279 | $hasChildren = $resource->hasChildren(); |
|
| 285 | 279 | $hasBody = $resource instanceof BodyResource; |
|
| 286 | |||
| 287 | 279 | if ($hasChildren && $hasBody) { |
|
| 288 | 1 | throw new UnsupportedResourceException(sprintf( |
|
| 289 | 'Instances of BodyResource do not support child resources in '. |
||
| 290 | 1 | 'FilesystemRepository. Tried to add a BodyResource with '. |
|
| 291 | 1 | 'children at %s.', |
|
| 292 | $path |
||
| 293 | 1 | )); |
|
| 294 | } |
||
| 295 | |||
| 296 | 278 | if ($this->symlink && $checkParentsForSymlinks) { |
|
| 297 | 150 | $this->replaceParentSymlinksByCopies($path); |
|
| 298 | 150 | } |
|
| 299 | |||
| 300 | 278 | if ($resource instanceof FilesystemResource) { |
|
| 301 | 33 | if ($this->symlink) { |
|
| 302 | 30 | $this->symlinkMirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 303 | 33 | } elseif ($hasBody) { |
|
| 304 | 2 | $this->filesystem->copy($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 305 | 2 | } else { |
|
| 306 | 1 | $this->filesystem->mirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 307 | } |
||
| 308 | |||
| 309 | 33 | return; |
|
| 310 | } |
||
| 311 | |||
| 312 | 253 | if ($resource instanceof LinkResource) { |
|
| 313 | 10 | if (!$this->symlink) { |
|
| 314 | 6 | throw new UnsupportedResourceException(sprintf( |
|
| 315 | 'LinkResource requires support of symbolic links in FilesystemRepository. '. |
||
| 316 | 6 | 'Tried to add a LinkResource at %s.', |
|
| 317 | $path |
||
| 318 | 6 | )); |
|
| 319 | } |
||
| 320 | |||
| 321 | 4 | $this->filesystem->symlink($this->baseDir.$resource->getTargetPath(), $pathInBaseDir); |
|
| 322 | |||
| 323 | 4 | return; |
|
| 324 | } |
||
| 325 | |||
| 326 | 245 | if ($hasBody) { |
|
| 327 | 121 | file_put_contents($pathInBaseDir, $resource->getBody()); |
|
| 328 | |||
| 329 | 121 | return; |
|
| 330 | } |
||
| 331 | |||
| 332 | 193 | if (is_file($pathInBaseDir)) { |
|
| 333 | $this->filesystem->remove($pathInBaseDir); |
||
| 334 | } |
||
| 335 | |||
| 336 | 193 | if (!file_exists($pathInBaseDir)) { |
|
| 337 | 117 | mkdir($pathInBaseDir, 0777, true); |
|
| 338 | 117 | } |
|
| 339 | |||
| 340 | 193 | foreach ($resource->listChildren() as $child) { |
|
| 341 | 114 | $this->addResource($path.'/'.$child->getName(), $child, false); |
|
| 342 | 193 | } |
|
| 343 | 193 | } |
|
| 344 | |||
| 345 | 32 | private function removeResource($filesystemPath, &$removed) |
|
| 360 | |||
| 361 | 119 | private function createResource($filesystemPath, $path) |
|
| 387 | |||
| 388 | 24 | private function countChildren($filesystemPath) |
|
| 405 | |||
| 406 | 50 | private function iteratorToCollection(Iterator $iterator) |
|
| 429 | |||
| 430 | 66 | View Code Duplication | private function getFilesystemPath($path) |
| 441 | |||
| 442 | 123 | private function getGlobIterator($query, $language) |
|
| 453 | |||
| 454 | 70 | private function getDirectoryIterator($filesystemPath) |
|
| 461 | |||
| 462 | 30 | private function symlinkMirror($origin, $target, array $dirsToKeep = array()) |
|
| 463 | { |
||
| 464 | 30 | $targetIsDir = is_dir($target); |
|
| 465 | 30 | $forceDir = in_array($target, $dirsToKeep, true); |
|
| 466 | |||
| 467 | // Merge directories |
||
| 468 | 30 | if (is_dir($origin) && ($targetIsDir || $forceDir)) { |
|
| 469 | 16 | if (is_link($target)) { |
|
| 470 | 4 | $this->replaceLinkByCopy($target, $dirsToKeep); |
|
| 471 | 4 | } |
|
| 472 | |||
| 473 | 16 | $iterator = $this->getDirectoryIterator($origin); |
|
| 474 | |||
| 475 | 16 | foreach ($iterator as $path) { |
|
| 476 | 16 | $this->symlinkMirror($path, $target.'/'.basename($path), $dirsToKeep); |
|
| 477 | 16 | } |
|
| 478 | |||
| 479 | 16 | return; |
|
| 480 | } |
||
| 481 | |||
| 482 | // Replace target |
||
| 483 | 30 | if (file_exists($target)) { |
|
| 484 | 10 | $this->filesystem->remove($target); |
|
| 485 | 10 | } |
|
| 486 | |||
| 487 | // Try creating a relative link |
||
| 488 | 30 | if ($this->relative && $this->trySymlink(Path::makeRelative($origin, Path::getDirectory($target)), $target)) { |
|
| 489 | 15 | return; |
|
| 490 | } |
||
| 491 | |||
| 492 | // Try creating a absolute link |
||
| 493 | 15 | if ($this->trySymlink($origin, $target)) { |
|
| 494 | 15 | return; |
|
| 495 | } |
||
| 496 | |||
| 497 | // Fall back to copy |
||
| 498 | if (is_dir($origin)) { |
||
| 499 | $this->filesystem->mirror($origin, $target); |
||
| 500 | |||
| 501 | return; |
||
| 502 | } |
||
| 503 | |||
| 504 | $this->filesystem->copy($origin, $target); |
||
| 505 | } |
||
| 506 | |||
| 507 | 150 | private function replaceParentSymlinksByCopies($path) |
|
| 542 | |||
| 543 | 16 | private function replaceLinkByCopy($path, array $dirsToKeep = array()) |
|
| 550 | |||
| 551 | 30 | private function trySymlink($origin, $target) |
|
| 564 | |||
| 565 | 22 | private function readLink($filesystemPath) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * This repository does not need this as the filesystem will resolve itself links. |
||
| 586 | */ |
||
| 587 | protected function followLinks($path) |
||
| 588 | { |
||
| 590 | } |
||
| 591 |