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 | 167 | 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) |
|
296 | { |
||
297 | 341 | $pathInBaseDir = $this->baseDir.$path; |
|
298 | 341 | $hasChildren = $resource->hasChildren(); |
|
299 | 341 | $hasBody = $resource instanceof BodyResource; |
|
300 | |||
301 | 341 | if ($hasChildren && $hasBody) { |
|
302 | 1 | throw new UnsupportedResourceException(sprintf( |
|
303 | 'Instances of BodyResource do not support child resources in '. |
||
304 | 'FilesystemRepository. Tried to add a BodyResource with '. |
||
305 | 1 | 'children at %s.', |
|
306 | $path |
||
307 | )); |
||
308 | } |
||
309 | |||
310 | 340 | $resource = clone $resource; |
|
311 | 340 | $resource->attachTo($this, $path); |
|
312 | |||
313 | 340 | if ($this->symlink && $checkParentsForSymlinks) { |
|
314 | 182 | $this->replaceParentSymlinksByCopies($path); |
|
315 | } |
||
316 | |||
317 | 340 | if ($resource instanceof FilesystemResource) { |
|
318 | 33 | if ($this->symlink) { |
|
319 | 30 | $this->symlinkMirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
320 | 3 | } elseif ($hasBody) { |
|
321 | 2 | $this->filesystem->copy($resource->getFilesystemPath(), $pathInBaseDir); |
|
322 | } else { |
||
323 | 1 | $this->filesystem->mirror($resource->getFilesystemPath(), $pathInBaseDir); |
|
324 | } |
||
325 | |||
326 | 33 | $this->storeVersion($resource); |
|
327 | |||
328 | 33 | return; |
|
329 | } |
||
330 | |||
331 | 315 | if ($resource instanceof LinkResource) { |
|
332 | 8 | if (!$this->symlink) { |
|
333 | 4 | throw new UnsupportedResourceException(sprintf( |
|
334 | 'LinkResource requires support of symbolic links in FilesystemRepository. '. |
||
335 | 4 | 'Tried to add a LinkResource at %s.', |
|
336 | $path |
||
337 | )); |
||
338 | } |
||
339 | |||
340 | 4 | $this->filesystem->symlink($this->baseDir.$resource->getTargetPath(), $pathInBaseDir); |
|
341 | |||
342 | 4 | $this->storeVersion($resource); |
|
343 | |||
344 | 4 | return; |
|
345 | } |
||
346 | |||
347 | 307 | if ($hasBody) { |
|
348 | 175 | file_put_contents($pathInBaseDir, $resource->getBody()); |
|
|
|||
349 | |||
350 | 175 | $this->storeVersion($resource); |
|
351 | |||
352 | 175 | return; |
|
353 | } |
||
354 | |||
355 | 239 | if (is_file($pathInBaseDir)) { |
|
356 | $this->filesystem->remove($pathInBaseDir); |
||
357 | } |
||
358 | |||
359 | 239 | if (!file_exists($pathInBaseDir)) { |
|
360 | 147 | mkdir($pathInBaseDir, 0777, true); |
|
361 | } |
||
362 | |||
363 | 239 | foreach ($resource->listChildren() as $child) { |
|
364 | 148 | $this->addResource($path.'/'.$child->getName(), $child, false); |
|
365 | } |
||
366 | |||
367 | 239 | $this->storeVersion($resource); |
|
368 | 239 | } |
|
369 | |||
370 | 48 | private function removeResource($filesystemPath, &$removed) |
|
371 | { |
||
372 | // Skip paths that have already been removed |
||
373 | 48 | if (!file_exists($filesystemPath)) { |
|
374 | return; |
||
375 | } |
||
376 | |||
377 | 48 | $this->removeVersions($this->getPath($filesystemPath)); |
|
378 | |||
379 | 48 | ++$removed; |
|
380 | |||
381 | 48 | if (is_dir($filesystemPath)) { |
|
382 | 32 | $iterator = $this->getDirectoryIterator($filesystemPath); |
|
383 | |||
384 | 32 | foreach ($iterator as $childFilesystemPath) { |
|
385 | // Remove children and child versions |
||
386 | 32 | $this->removeResource($childFilesystemPath, $removed); |
|
387 | } |
||
388 | } |
||
389 | |||
390 | 48 | $this->filesystem->remove($filesystemPath); |
|
391 | 48 | } |
|
392 | |||
393 | 147 | private function createResource($filesystemPath, $path) |
|
394 | { |
||
395 | 147 | $resource = null; |
|
396 | |||
397 | 147 | if (is_link($filesystemPath)) { |
|
398 | 6 | $baseDir = rtrim($this->baseDir, '/'); |
|
399 | 6 | $targetFilesystemPath = $this->readLink($filesystemPath); |
|
400 | |||
401 | 6 | if (Path::isBasePath($baseDir, $targetFilesystemPath)) { |
|
402 | 6 | $targetPath = '/'.Path::makeRelative($targetFilesystemPath, $baseDir); |
|
403 | 6 | $resource = new LinkResource($targetPath); |
|
404 | } |
||
405 | } |
||
406 | |||
407 | 147 | if (!$resource && is_dir($filesystemPath)) { |
|
408 | 98 | $resource = new DirectoryResource($filesystemPath); |
|
409 | } |
||
410 | |||
411 | 147 | if (!$resource) { |
|
412 | 79 | $resource = new FileResource($filesystemPath); |
|
413 | } |
||
414 | |||
415 | 147 | $resource->attachTo($this, $path); |
|
416 | |||
417 | 147 | return $resource; |
|
418 | } |
||
419 | |||
420 | 50 | private function iteratorToCollection(Iterator $iterator) |
|
440 | |||
441 | 66 | View Code Duplication | private function getFilesystemPath($path) |
452 | |||
453 | 135 | private function getGlobIterator($query, $language) |
|
454 | { |
||
455 | 135 | $this->failUnlessGlob($language); |
|
456 | |||
457 | 132 | Assert::stringNotEmpty($query, 'The glob must be a non-empty string. Got: %s'); |
|
458 | 108 | Assert::startsWith($query, '/', 'The glob %s is not absolute.'); |
|
459 | |||
460 | 96 | $query = Path::canonicalize($query); |
|
461 | |||
462 | 96 | return new GlobIterator($this->baseDir.$query); |
|
463 | } |
||
464 | |||
465 | 82 | private function getDirectoryIterator($filesystemPath) |
|
472 | |||
473 | 30 | private function symlinkMirror($origin, $target, array $dirsToKeep = array()) |
|
474 | { |
||
475 | 30 | $targetIsDir = is_dir($target); |
|
476 | 30 | $forceDir = in_array($target, $dirsToKeep, true); |
|
517 | |||
518 | 182 | private function replaceParentSymlinksByCopies($path) |
|
553 | |||
554 | 16 | private function replaceLinkByCopy($path, array $dirsToKeep = array()) |
|
561 | |||
562 | 30 | private function trySymlink($origin, $target) |
|
575 | |||
576 | 22 | private function readLink($filesystemPath) |
|
594 | |||
595 | 90 | private function getPath($filesystemPath) |
|
599 | } |
||
600 |
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: