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 |
||
| 63 | class FilesystemRepository extends AbstractEditableRepository |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * @var bool|null |
||
| 67 | */ |
||
| 68 | private static $symlinkSupported; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $baseDir; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private $baseDirLength; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | private $symlink; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | private $relative; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var Filesystem |
||
| 92 | */ |
||
| 93 | private $filesystem; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns whether symlinks are supported in the local environment. |
||
| 97 | * |
||
| 98 | * @return bool Returns `true` if symlinks are supported. |
||
| 99 | */ |
||
| 100 | 303 | public static function isSymlinkSupported() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Creates a new repository. |
||
| 118 | * |
||
| 119 | * @param string $baseDir The base directory of the repository on the file |
||
| 120 | * system. |
||
| 121 | * @param bool $symlink Whether to use symbolic links for added files. If |
||
| 122 | * symbolic links are not supported on the current |
||
| 123 | * system, the repository will create hard copies |
||
| 124 | * instead. |
||
| 125 | * @param bool $relative Whether to create relative symbolic links. If |
||
| 126 | * relative links are not supported on the current |
||
| 127 | * system, the repository will create absolute links |
||
| 128 | * instead. |
||
| 129 | * @param ChangeStream|null $changeStream If provided, the repository will log |
||
| 130 | * resources changes in this change stream. |
||
| 131 | */ |
||
| 132 | 392 | public function __construct($baseDir = '/', $symlink = true, $relative = true, ChangeStream $changeStream = null) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 159 | View Code Duplication | public function get($path) |
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | 41 | public function find($query, $language = 'glob') |
|
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | 61 | public function contains($query, $language = 'glob') |
|
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | 28 | public function hasChildren($path) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | 42 | public function listChildren($path) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 357 | public function add($path, $resource) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | 61 | public function remove($query, $language = 'glob') |
|
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | 12 | public function clear() |
|
| 277 | |||
| 278 | 341 | private function ensureDirectoryExists($path) |
|
| 294 | |||
| 295 | 341 | private function addResource($path, PuliResource $resource, $checkParentsForSymlinks = true) |
|
| 373 | |||
| 374 | 48 | private function removeResource($filesystemPath, &$removed) |
|
| 396 | |||
| 397 | 139 | private function createResource($filesystemPath, $path) |
|
| 423 | |||
| 424 | 50 | private function iteratorToCollection(Iterator $iterator) |
|
| 444 | |||
| 445 | 66 | View Code Duplication | private function getFilesystemPath($path) |
| 456 | |||
| 457 | 135 | private function getGlobIterator($query, $language) |
|
| 468 | |||
| 469 | 82 | private function getDirectoryIterator($filesystemPath) |
|
| 476 | |||
| 477 | 30 | private function symlinkMirror($origin, $target, array $dirsToKeep = array()) |
|
| 478 | { |
||
| 479 | 30 | $targetIsDir = is_dir($target); |
|
| 480 | 30 | $forceDir = in_array($target, $dirsToKeep, true); |
|
| 481 | |||
| 482 | // Merge directories |
||
| 483 | 30 | if (is_dir($origin) && ($targetIsDir || $forceDir)) { |
|
| 484 | 16 | if (is_link($target)) { |
|
| 485 | 4 | $this->replaceLinkByCopy($target, $dirsToKeep); |
|
| 486 | } |
||
| 487 | |||
| 488 | 16 | $iterator = $this->getDirectoryIterator($origin); |
|
| 489 | |||
| 490 | 16 | foreach ($iterator as $path) { |
|
| 491 | 16 | $this->symlinkMirror($path, $target.'/'.basename($path), $dirsToKeep); |
|
| 492 | } |
||
| 493 | |||
| 494 | 16 | return; |
|
| 495 | } |
||
| 496 | |||
| 497 | // Replace target |
||
| 498 | 30 | if (file_exists($target)) { |
|
| 499 | 10 | $this->filesystem->remove($target); |
|
| 500 | } |
||
| 501 | |||
| 502 | // Try creating a relative link |
||
| 503 | 30 | if ($this->relative && $this->trySymlink(Path::makeRelative($origin, Path::getDirectory($target)), $target)) { |
|
| 504 | 15 | return; |
|
| 505 | } |
||
| 506 | |||
| 507 | // Try creating a absolute link |
||
| 508 | 15 | if ($this->trySymlink($origin, $target)) { |
|
| 509 | 15 | return; |
|
| 510 | } |
||
| 511 | |||
| 512 | // Fall back to copy |
||
| 513 | if (is_dir($origin)) { |
||
| 514 | $this->filesystem->mirror($origin, $target); |
||
| 515 | |||
| 516 | return; |
||
| 517 | } |
||
| 518 | |||
| 519 | $this->filesystem->copy($origin, $target); |
||
| 520 | } |
||
| 521 | |||
| 522 | 182 | private function replaceParentSymlinksByCopies($path) |
|
| 523 | { |
||
| 524 | 182 | $previousPath = null; |
|
| 525 | |||
| 526 | // Collect all paths that MUST NOT be symlinks after doing the |
||
| 527 | // replace operation. |
||
| 528 | // |
||
| 529 | // Example: |
||
| 530 | // |
||
| 531 | // $dirsToKeep = ['/path/to/webmozart', '/path/to/webmozart/views'] |
||
| 532 | // |
||
| 533 | // Before: |
||
| 534 | // /webmozart -> target |
||
| 535 | // |
||
| 536 | // After: |
||
| 537 | // /webmozart |
||
| 538 | // /config -> target/config |
||
| 539 | // /views |
||
| 540 | // /index.html.twig -> target/views/index.html.twig |
||
| 541 | |||
| 542 | 182 | $dirsToKeep = array(); |
|
| 543 | |||
| 544 | 182 | while ($previousPath !== ($path = Path::getDirectory($path))) { |
|
| 545 | 182 | $filesystemPath = $this->baseDir.$path; |
|
| 546 | 182 | $dirsToKeep[] = $filesystemPath; |
|
| 547 | |||
| 548 | 182 | if (is_link($filesystemPath)) { |
|
| 549 | 12 | $this->replaceLinkByCopy($filesystemPath, $dirsToKeep); |
|
| 550 | |||
| 551 | 12 | return; |
|
| 552 | } |
||
| 553 | |||
| 554 | 182 | $previousPath = $path; |
|
| 555 | } |
||
| 556 | 182 | } |
|
| 557 | |||
| 558 | 16 | private function replaceLinkByCopy($path, array $dirsToKeep = array()) |
|
| 559 | { |
||
| 560 | 16 | $target = Path::makeAbsolute($this->readLink($path), Path::getDirectory($path)); |
|
| 561 | 16 | $this->filesystem->remove($path); |
|
| 562 | 16 | $this->filesystem->mkdir($path); |
|
| 563 | 16 | $this->symlinkMirror($target, $path, $dirsToKeep); |
|
| 564 | 16 | } |
|
| 565 | |||
| 566 | 30 | private function trySymlink($origin, $target) |
|
| 579 | |||
| 580 | 22 | private function readLink($filesystemPath) |
|
| 598 | |||
| 599 | 90 | private function getPath($filesystemPath) |
|
| 603 | } |
||
| 604 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: