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 | } |
||
| 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) |
|
| 206 | { |
||
| 207 | 295 | $path = $this->sanitizePath($path); |
|
| 208 | |||
| 209 | 283 | View Code Duplication | if ($resource instanceof ResourceCollection) { |
| 210 | 4 | $this->ensureDirectoryExists($path); |
|
| 211 | 4 | foreach ($resource as $child) { |
|
| 212 | 4 | $this->addResource($path.'/'.$child->getName(), $child); |
|
| 213 | } |
||
| 214 | |||
| 215 | 4 | return; |
|
| 216 | } |
||
| 217 | |||
| 218 | 279 | if ($resource instanceof PuliResource) { |
|
| 219 | 275 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
| 220 | 275 | $this->addResource($path, $resource); |
|
| 221 | |||
| 222 | 268 | return; |
|
| 223 | } |
||
| 224 | |||
| 225 | 4 | throw new UnsupportedResourceException(sprintf( |
|
| 226 | 4 | 'The passed resource must be a PuliResource or ResourceCollection. Got: %s', |
|
| 227 | 4 | is_object($resource) ? get_class($resource) : gettype($resource) |
|
| 228 | )); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | 49 | public function remove($query, $language = 'glob') |
|
| 235 | { |
||
| 236 | 49 | $iterator = $this->getGlobIterator($query, $language); |
|
| 237 | 36 | $removed = 0; |
|
| 238 | |||
| 239 | 36 | Assert::notEmpty(trim($query, '/'), 'The root directory cannot be removed.'); |
|
| 240 | |||
| 241 | // There's some problem with concurrent deletions at the moment |
||
| 242 | 28 | foreach (iterator_to_array($iterator) as $filesystemPath) { |
|
| 243 | 28 | $this->removeResource($filesystemPath, $removed); |
|
| 244 | } |
||
| 245 | |||
| 246 | 28 | return $removed; |
|
| 247 | } |
||
| 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 | } |
||
| 260 | |||
| 261 | 4 | return $removed; |
|
| 262 | } |
||
| 263 | |||
| 264 | 279 | private function ensureDirectoryExists($path) |
|
| 265 | { |
||
| 266 | 279 | $filesystemPath = $this->baseDir.$path; |
|
| 267 | |||
| 268 | 279 | if (is_file($filesystemPath)) { |
|
| 269 | 1 | throw new UnsupportedOperationException(sprintf( |
|
| 270 | 'Instances of BodyResource do not support child resources in '. |
||
| 271 | 1 | 'FilesystemRepository. Tried to add a child to %s.', |
|
| 272 | $filesystemPath |
||
| 273 | )); |
||
| 274 | } |
||
| 275 | |||
| 276 | 279 | if (!is_dir($filesystemPath)) { |
|
| 277 | 80 | mkdir($filesystemPath, 0777, true); |
|
| 278 | } |
||
| 279 | 279 | } |
|
| 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 | 'FilesystemRepository. Tried to add a BodyResource with '. |
||
| 291 | 1 | 'children at %s.', |
|
| 292 | $path |
||
| 293 | )); |
||
| 294 | } |
||
| 295 | |||
| 296 | 278 | if ($this->symlink && $checkParentsForSymlinks) { |
|
| 297 | 150 | $this->replaceParentSymlinksByCopies($path); |
|
| 298 | } |
||
| 299 | |||
| 300 | 278 | if ($resource instanceof FilesystemResource) { |
|
| 301 | 33 | if ($this->symlink) { |
|
| 302 | 30 | $this->symlinkMirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 303 | 3 | } elseif ($hasBody) { |
|
| 304 | 2 | $this->filesystem->copy($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 305 | } 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 | )); |
||
| 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 | } |
||
| 339 | |||
| 340 | 193 | foreach ($resource->listChildren() as $child) { |
|
| 341 | 114 | $this->addResource($path.'/'.$child->getName(), $child, false); |
|
| 342 | } |
||
| 343 | 193 | } |
|
| 344 | |||
| 345 | 32 | private function removeResource($filesystemPath, &$removed) |
|
| 346 | { |
||
| 347 | // Skip paths that have already been removed |
||
| 348 | 32 | if (!file_exists($filesystemPath)) { |
|
| 349 | return; |
||
| 350 | } |
||
| 351 | |||
| 352 | 32 | ++$removed; |
|
| 353 | |||
| 354 | 32 | if (is_dir($filesystemPath)) { |
|
| 355 | 24 | $removed += $this->countChildren($filesystemPath); |
|
| 356 | } |
||
| 357 | |||
| 358 | 32 | $this->filesystem->remove($filesystemPath); |
|
| 359 | 32 | } |
|
| 360 | |||
| 361 | 119 | private function createResource($filesystemPath, $path) |
|
| 362 | { |
||
| 363 | 119 | $resource = null; |
|
| 364 | |||
| 365 | 119 | if (is_link($filesystemPath)) { |
|
| 366 | 6 | $baseDir = rtrim($this->baseDir, '/'); |
|
| 367 | 6 | $targetFilesystemPath = $this->readLink($filesystemPath); |
|
| 368 | |||
| 369 | 6 | if (Path::isBasePath($baseDir, $targetFilesystemPath)) { |
|
| 370 | 6 | $targetPath = '/'.Path::makeRelative($targetFilesystemPath, $baseDir); |
|
| 371 | 6 | $resource = new LinkResource($targetPath); |
|
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | 119 | if (!$resource && is_dir($filesystemPath)) { |
|
| 376 | 74 | $resource = new DirectoryResource($filesystemPath); |
|
| 377 | } |
||
| 378 | |||
| 379 | 119 | if (!$resource) { |
|
| 380 | 67 | $resource = new FileResource($filesystemPath); |
|
| 381 | } |
||
| 382 | |||
| 383 | 119 | $resource->attachTo($this, $path); |
|
| 384 | |||
| 385 | 119 | return $resource; |
|
| 386 | } |
||
| 387 | |||
| 388 | 24 | private function countChildren($filesystemPath) |
|
| 389 | { |
||
| 390 | 24 | $iterator = new RecursiveIteratorIterator( |
|
| 391 | 24 | $this->getDirectoryIterator($filesystemPath), |
|
| 392 | 24 | RecursiveIteratorIterator::SELF_FIRST |
|
| 393 | ); |
||
| 394 | |||
| 395 | 24 | $iterator->rewind(); |
|
| 396 | 24 | $count = 0; |
|
| 397 | |||
| 398 | 24 | while ($iterator->valid()) { |
|
| 399 | 24 | ++$count; |
|
| 400 | 24 | $iterator->next(); |
|
| 401 | } |
||
| 402 | |||
| 403 | 24 | return $count; |
|
| 404 | } |
||
| 405 | |||
| 406 | 50 | private function iteratorToCollection(Iterator $iterator) |
|
| 407 | { |
||
| 408 | 50 | $offset = strlen($this->baseDir); |
|
| 409 | 50 | $filesystemPaths = iterator_to_array($iterator); |
|
| 410 | 50 | $resources = array(); |
|
| 411 | |||
| 412 | // RecursiveDirectoryIterator is not guaranteed to return sorted results |
||
| 413 | 50 | sort($filesystemPaths); |
|
| 414 | |||
| 415 | 50 | foreach ($filesystemPaths as $filesystemPath) { |
|
| 416 | 42 | $path = substr($filesystemPath, $offset); |
|
| 417 | |||
| 418 | 42 | $resource = is_dir($filesystemPath) |
|
| 419 | 24 | ? new DirectoryResource($filesystemPath, $path) |
|
| 420 | 42 | : new FileResource($filesystemPath, $path); |
|
| 421 | |||
| 422 | 42 | $resource->attachTo($this); |
|
| 423 | |||
| 424 | 42 | $resources[] = $resource; |
|
| 425 | } |
||
| 426 | |||
| 427 | 50 | return new FilesystemResourceCollection($resources); |
|
| 428 | } |
||
| 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) |
|
| 455 | { |
||
| 456 | 70 | return new RecursiveDirectoryIterator( |
|
| 457 | $filesystemPath, |
||
| 458 | 70 | RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS |
|
| 459 | ); |
||
| 460 | } |
||
| 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 | } |
||
| 472 | |||
| 473 | 16 | $iterator = $this->getDirectoryIterator($origin); |
|
| 474 | |||
| 475 | 16 | foreach ($iterator as $path) { |
|
| 476 | 16 | $this->symlinkMirror($path, $target.'/'.basename($path), $dirsToKeep); |
|
| 477 | } |
||
| 478 | |||
| 479 | 16 | return; |
|
| 480 | } |
||
| 481 | |||
| 482 | // Replace target |
||
| 483 | 30 | if (file_exists($target)) { |
|
| 484 | 10 | $this->filesystem->remove($target); |
|
| 485 | } |
||
| 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) |
|
| 508 | { |
||
| 509 | 150 | $previousPath = null; |
|
| 510 | |||
| 511 | // Collect all paths that MUST NOT be symlinks after doing the |
||
| 512 | // replace operation. |
||
| 513 | // |
||
| 514 | // Example: |
||
| 515 | // |
||
| 516 | // $dirsToKeep = ['/path/to/webmozart', '/path/to/webmozart/views'] |
||
| 517 | // |
||
| 518 | // Before: |
||
| 519 | // /webmozart -> target |
||
| 520 | // |
||
| 521 | // After: |
||
| 522 | // /webmozart |
||
| 523 | // /config -> target/config |
||
| 524 | // /views |
||
| 525 | // /index.html.twig -> target/views/index.html.twig |
||
| 526 | |||
| 527 | 150 | $dirsToKeep = array(); |
|
| 528 | |||
| 529 | 150 | while ($previousPath !== ($path = Path::getDirectory($path))) { |
|
| 530 | 150 | $filesystemPath = $this->baseDir.$path; |
|
| 531 | 150 | $dirsToKeep[] = $filesystemPath; |
|
| 532 | |||
| 533 | 150 | if (is_link($filesystemPath)) { |
|
| 534 | 12 | $this->replaceLinkByCopy($filesystemPath, $dirsToKeep); |
|
| 535 | |||
| 536 | 12 | return; |
|
| 537 | } |
||
| 538 | |||
| 539 | 150 | $previousPath = $path; |
|
| 540 | } |
||
| 541 | 150 | } |
|
| 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) |
||
| 590 | } |
||
| 591 |