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 AbstractEditableRepository |
||
| 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 | 288 | public static function isSymlinkSupported() |
|
| 97 | { |
||
| 98 | 288 | 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 | 288 | 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 | * @param ChangeStream|null $changeStream If provided, the repository will log |
||
| 126 | * resources changes in this change stream. |
||
| 127 | */ |
||
| 128 | 372 | public function __construct($baseDir = '/', $symlink = true, $relative = true, ChangeStream $changeStream = null) |
|
| 129 | { |
||
| 130 | 372 | parent::__construct($changeStream); |
|
| 131 | |||
| 132 | 372 | Assert::directory($baseDir); |
|
| 133 | 372 | Assert::boolean($symlink); |
|
| 134 | |||
| 135 | 372 | $this->baseDir = rtrim(Path::canonicalize($baseDir), '/'); |
|
| 136 | 372 | $this->symlink = $symlink && self::isSymlinkSupported(); |
|
| 137 | 372 | $this->relative = $this->symlink && $relative; |
|
| 138 | 372 | $this->filesystem = new Filesystem(); |
|
| 139 | 372 | } |
|
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | 143 | View Code Duplication | public function get($path) |
| 145 | { |
||
| 146 | 143 | $path = $this->sanitizePath($path); |
|
| 147 | 131 | $filesystemPath = $this->baseDir.$path; |
|
| 148 | |||
| 149 | 131 | if (!file_exists($filesystemPath)) { |
|
| 150 | 8 | throw ResourceNotFoundException::forPath($path); |
|
| 151 | } |
||
| 152 | |||
| 153 | 123 | return $this->createResource($filesystemPath, $path); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 41 | public function find($query, $language = 'glob') |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 61 | public function contains($query, $language = 'glob') |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 28 | public function hasChildren($path) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | 42 | public function listChildren($path) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | 337 | public function add($path, $resource) |
|
| 210 | { |
||
| 211 | 337 | $path = $this->sanitizePath($path); |
|
| 212 | |||
| 213 | 325 | View Code Duplication | if ($resource instanceof ResourceCollection) { |
| 214 | 4 | $this->ensureDirectoryExists($path); |
|
| 215 | 4 | foreach ($resource as $child) { |
|
| 216 | 4 | $this->addResource($path.'/'.$child->getName(), $child); |
|
| 217 | 4 | } |
|
| 218 | |||
| 219 | 4 | return; |
|
| 220 | } |
||
| 221 | |||
| 222 | 321 | if ($resource instanceof PuliResource) { |
|
| 223 | 317 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
| 224 | 317 | $this->addResource($path, $resource); |
|
| 225 | |||
| 226 | 312 | return; |
|
| 227 | } |
||
| 228 | |||
| 229 | 4 | throw new UnsupportedResourceException(sprintf( |
|
| 230 | 4 | 'The passed resource must be a PuliResource or ResourceCollection. Got: %s', |
|
| 231 | 4 | is_object($resource) ? get_class($resource) : gettype($resource) |
|
| 232 | 4 | )); |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | 49 | public function remove($query, $language = 'glob') |
|
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | 4 | public function clear() |
|
| 267 | |||
| 268 | 321 | private function ensureDirectoryExists($path) |
|
| 284 | |||
| 285 | 321 | private function addResource($path, PuliResource $resource, $checkParentsForSymlinks = true) |
|
| 286 | { |
||
| 287 | 321 | $pathInBaseDir = $this->baseDir.$path; |
|
| 288 | 321 | $hasChildren = $resource->hasChildren(); |
|
| 289 | 321 | $hasBody = $resource instanceof BodyResource; |
|
| 290 | |||
| 291 | 321 | if ($hasChildren && $hasBody) { |
|
| 292 | 1 | throw new UnsupportedResourceException(sprintf( |
|
| 293 | 'Instances of BodyResource do not support child resources in '. |
||
| 294 | 1 | 'FilesystemRepository. Tried to add a BodyResource with '. |
|
| 295 | 1 | 'children at %s.', |
|
| 296 | $path |
||
| 297 | 1 | )); |
|
| 298 | } |
||
| 299 | |||
| 300 | // Don't modify resources attached to other repositories |
||
| 301 | 320 | if ($resource->isAttached()) { |
|
| 302 | 4 | $resource = clone $resource; |
|
| 303 | 4 | } |
|
| 304 | |||
| 305 | 320 | $resource->attachTo($this, $path); |
|
| 306 | |||
| 307 | 320 | if ($this->symlink && $checkParentsForSymlinks) { |
|
| 308 | 172 | $this->replaceParentSymlinksByCopies($path); |
|
| 309 | 172 | } |
|
| 310 | |||
| 311 | 320 | if ($resource instanceof FilesystemResource) { |
|
| 312 | 33 | if ($this->symlink) { |
|
| 313 | 30 | $this->symlinkMirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 314 | 33 | } elseif ($hasBody) { |
|
| 315 | 2 | $this->filesystem->copy($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 316 | 2 | } else { |
|
| 317 | 1 | $this->filesystem->mirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
| 318 | } |
||
| 319 | |||
| 320 | 33 | $this->appendToChangeStream($resource); |
|
| 321 | |||
| 322 | 33 | return; |
|
| 323 | } |
||
| 324 | |||
| 325 | 295 | if ($resource instanceof LinkResource) { |
|
| 326 | 8 | if (!$this->symlink) { |
|
| 327 | 4 | throw new UnsupportedResourceException(sprintf( |
|
| 328 | 'LinkResource requires support of symbolic links in FilesystemRepository. '. |
||
| 329 | 4 | 'Tried to add a LinkResource at %s.', |
|
| 330 | $path |
||
| 331 | 4 | )); |
|
| 332 | } |
||
| 333 | |||
| 334 | 4 | $this->filesystem->symlink($this->baseDir.$resource->getTargetPath(), $pathInBaseDir); |
|
| 335 | |||
| 336 | 4 | $this->appendToChangeStream($resource); |
|
| 337 | |||
| 338 | 4 | return; |
|
| 339 | } |
||
| 340 | |||
| 341 | 287 | if ($hasBody) { |
|
| 342 | 159 | file_put_contents($pathInBaseDir, $resource->getBody()); |
|
|
|
|||
| 343 | |||
| 344 | 159 | $this->appendToChangeStream($resource); |
|
| 345 | |||
| 346 | 159 | return; |
|
| 347 | } |
||
| 348 | |||
| 349 | 223 | if (is_file($pathInBaseDir)) { |
|
| 350 | 1 | $this->filesystem->remove($pathInBaseDir); |
|
| 351 | } |
||
| 352 | |||
| 353 | 223 | if (!file_exists($pathInBaseDir)) { |
|
| 354 | 135 | mkdir($pathInBaseDir, 0777, true); |
|
| 355 | 135 | } |
|
| 356 | |||
| 357 | 223 | foreach ($resource->listChildren() as $child) { |
|
| 358 | 140 | $this->addResource($path.'/'.$child->getName(), $child, false); |
|
| 359 | 223 | } |
|
| 360 | |||
| 361 | 223 | $this->appendToChangeStream($resource); |
|
| 362 | 223 | } |
|
| 363 | |||
| 364 | 32 | private function removeResource($filesystemPath, &$removed) |
|
| 379 | |||
| 380 | 123 | private function createResource($filesystemPath, $path) |
|
| 406 | |||
| 407 | 24 | private function countChildren($filesystemPath) |
|
| 424 | |||
| 425 | 50 | private function iteratorToCollection(Iterator $iterator) |
|
| 448 | |||
| 449 | 66 | View Code Duplication | private function getFilesystemPath($path) |
| 450 | { |
||
| 451 | 66 | $path = $this->sanitizePath($path); |
|
| 452 | 42 | $filesystemPath = $this->baseDir.$path; |
|
| 453 | |||
| 454 | 42 | if (!file_exists($filesystemPath)) { |
|
| 455 | 8 | throw ResourceNotFoundException::forPath($path); |
|
| 456 | } |
||
| 457 | |||
| 458 | 34 | return $filesystemPath; |
|
| 459 | } |
||
| 460 | |||
| 461 | 123 | private function getGlobIterator($query, $language) |
|
| 462 | { |
||
| 463 | 123 | $this->validateSearchLanguage($language); |
|
| 464 | |||
| 465 | 120 | Assert::stringNotEmpty($query, 'The glob must be a non-empty string. Got: %s'); |
|
| 466 | 96 | Assert::startsWith($query, '/', 'The glob %s is not absolute.'); |
|
| 467 | |||
| 468 | 84 | $query = Path::canonicalize($query); |
|
| 469 | |||
| 470 | 84 | return new GlobIterator($this->baseDir.$query); |
|
| 471 | } |
||
| 472 | |||
| 473 | 70 | private function getDirectoryIterator($filesystemPath) |
|
| 480 | |||
| 481 | 30 | private function symlinkMirror($origin, $target, array $dirsToKeep = array()) |
|
| 482 | { |
||
| 483 | 30 | $targetIsDir = is_dir($target); |
|
| 484 | 30 | $forceDir = in_array($target, $dirsToKeep, true); |
|
| 485 | |||
| 486 | // Merge directories |
||
| 487 | 30 | if (is_dir($origin) && ($targetIsDir || $forceDir)) { |
|
| 488 | 16 | if (is_link($target)) { |
|
| 489 | 4 | $this->replaceLinkByCopy($target, $dirsToKeep); |
|
| 490 | 4 | } |
|
| 491 | |||
| 492 | 16 | $iterator = $this->getDirectoryIterator($origin); |
|
| 493 | |||
| 494 | 16 | foreach ($iterator as $path) { |
|
| 495 | 16 | $this->symlinkMirror($path, $target.'/'.basename($path), $dirsToKeep); |
|
| 496 | 16 | } |
|
| 497 | |||
| 498 | 16 | return; |
|
| 499 | } |
||
| 500 | |||
| 501 | // Replace target |
||
| 502 | 30 | if (file_exists($target)) { |
|
| 503 | 10 | $this->filesystem->remove($target); |
|
| 504 | 10 | } |
|
| 505 | |||
| 506 | // Try creating a relative link |
||
| 507 | 30 | if ($this->relative && $this->trySymlink(Path::makeRelative($origin, Path::getDirectory($target)), $target)) { |
|
| 508 | 15 | return; |
|
| 509 | } |
||
| 510 | |||
| 511 | // Try creating a absolute link |
||
| 512 | 15 | if ($this->trySymlink($origin, $target)) { |
|
| 513 | 15 | return; |
|
| 514 | } |
||
| 515 | |||
| 516 | // Fall back to copy |
||
| 517 | if (is_dir($origin)) { |
||
| 518 | $this->filesystem->mirror($origin, $target); |
||
| 519 | |||
| 520 | return; |
||
| 521 | } |
||
| 522 | |||
| 523 | $this->filesystem->copy($origin, $target); |
||
| 524 | } |
||
| 525 | |||
| 526 | 172 | private function replaceParentSymlinksByCopies($path) |
|
| 561 | |||
| 562 | 16 | private function replaceLinkByCopy($path, array $dirsToKeep = array()) |
|
| 563 | { |
||
| 564 | 16 | $target = Path::makeAbsolute($this->readLink($path), Path::getDirectory($path)); |
|
| 565 | 16 | $this->filesystem->remove($path); |
|
| 566 | 16 | $this->filesystem->mkdir($path); |
|
| 567 | 16 | $this->symlinkMirror($target, $path, $dirsToKeep); |
|
| 568 | 16 | } |
|
| 569 | |||
| 570 | 30 | private function trySymlink($origin, $target) |
|
| 571 | { |
||
| 572 | try { |
||
| 573 | 30 | $this->filesystem->symlink($origin, $target); |
|
| 574 | |||
| 575 | 30 | if (file_exists($target)) { |
|
| 576 | 30 | return true; |
|
| 577 | } |
||
| 578 | } catch (IOException $e) { |
||
| 579 | } |
||
| 580 | |||
| 581 | return false; |
||
| 582 | } |
||
| 583 | |||
| 584 | 22 | private function readLink($filesystemPath) |
|
| 602 | } |
||
| 603 |
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: