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 AbstractPathMappingRepository 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 AbstractPathMappingRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class AbstractPathMappingRepository extends AbstractRepository |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var KeyValueStore |
||
| 42 | */ |
||
| 43 | protected $store; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $baseDirectory; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates a new repository. |
||
| 52 | * |
||
| 53 | * @param KeyValueStore $store The store of all the paths. |
||
| 54 | * @param string $baseDirectory The store of all the paths. |
||
| 55 | */ |
||
| 56 | 176 | public function __construct(KeyValueStore $store, $baseDirectory) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 154 | public function add($path, $resource) |
|
| 68 | { |
||
| 69 | 154 | $path = $this->sanitizePath($path); |
|
| 70 | |||
| 71 | 148 | View Code Duplication | if ($resource instanceof ResourceCollection) { |
| 72 | 2 | $this->ensureDirectoryExists($path); |
|
| 73 | |||
| 74 | 2 | foreach ($resource as $child) { |
|
| 75 | 2 | $this->addResource($path.'/'.$child->getName(), $child); |
|
| 76 | } |
||
| 77 | |||
| 78 | 2 | $this->sortStore(); |
|
| 79 | |||
| 80 | 2 | return; |
|
| 81 | } |
||
| 82 | |||
| 83 | 146 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
| 84 | 146 | $this->addResource($path, $resource); |
|
| 85 | 142 | $this->sortStore(); |
|
| 86 | 142 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Add the resource (internal method after checks of add()). |
||
| 90 | * |
||
| 91 | * @param string $path |
||
| 92 | * @param PuliResource $resource |
||
| 93 | */ |
||
| 94 | 148 | private function addResource($path, $resource) |
|
| 95 | { |
||
| 96 | 148 | if (!($resource instanceof FilesystemResource || $resource instanceof LinkResource)) { |
|
| 97 | 2 | throw new UnsupportedResourceException(sprintf( |
|
| 98 | 2 | 'PathMapping repositories only supports FilesystemResource and LinkedResource. Got: %s', |
|
| 99 | 2 | is_object($resource) ? get_class($resource) : gettype($resource) |
|
| 100 | )); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Don't modify resources attached to other repositories |
||
| 104 | 146 | if ($resource->isAttached()) { |
|
| 105 | 2 | $resource = clone $resource; |
|
| 106 | } |
||
| 107 | |||
| 108 | 146 | if ($resource instanceof LinkResource) { |
|
| 109 | 14 | $this->addLinkResource($path, $resource); |
|
| 110 | 146 | } elseif (Path::isBasePath($this->baseDirectory, $resource->getFilesystemPath())) { |
|
| 111 | 144 | $this->addFilesystemResource($path, $resource); |
|
| 112 | } else { |
||
| 113 | 2 | throw new UnsupportedResourceException(sprintf( |
|
| 114 | 2 | 'Can only add resources from %s. Tried to add %s.', |
|
| 115 | 2 | $this->baseDirectory, |
|
| 116 | 2 | $resource->getFilesystemPath() |
|
| 117 | )); |
||
| 118 | } |
||
| 119 | 144 | } |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Add the filesystem resource. |
||
| 123 | * |
||
| 124 | * @param string $path |
||
| 125 | * @param FilesystemResource $resource |
||
| 126 | */ |
||
| 127 | abstract protected function addFilesystemResource($path, FilesystemResource $resource); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add the link resource. |
||
| 131 | * |
||
| 132 | * @param string $path |
||
| 133 | * @param LinkResource $resource |
||
| 134 | */ |
||
| 135 | abstract protected function addLinkResource($path, LinkResource $resource); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | 2 | public function clear() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Recursively creates a directory for a path. |
||
| 153 | * |
||
| 154 | * @param string $path A directory path. |
||
| 155 | */ |
||
| 156 | 148 | protected function ensureDirectoryExists($path) |
|
| 157 | { |
||
| 158 | 148 | if ($this->store->exists($path)) { |
|
| 159 | 148 | return; |
|
| 160 | } |
||
| 161 | |||
| 162 | // Recursively initialize parent directories |
||
| 163 | 50 | if ('/' !== $path) { |
|
| 164 | 50 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
| 165 | } |
||
| 166 | |||
| 167 | 50 | $this->store->set($path, null); |
|
| 168 | 50 | } |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Create the repository root. |
||
| 172 | */ |
||
| 173 | 176 | protected function createRoot() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Count the number of elements in the store. |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | 9 | protected function countStore() |
|
| 188 | { |
||
| 189 | 9 | if (!$this->store instanceof CountableStore) { |
|
| 190 | $this->store = new CountableDecorator($this->store); |
||
| 191 | } |
||
| 192 | |||
| 193 | 9 | return $this->store->count(); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Sort the store by keys. |
||
| 198 | */ |
||
| 199 | 144 | protected function sortStore() |
|
| 200 | { |
||
| 201 | 144 | if (!$this->store instanceof SortableStore) { |
|
| 202 | $this->store = new SortableDecorator($this->store); |
||
| 203 | } |
||
| 204 | |||
| 205 | 144 | $this->store->sort(); |
|
| 206 | 144 | } |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Create a filesystem or generic resource. |
||
| 210 | * |
||
| 211 | * @param string|null $filesystemPath |
||
| 212 | * |
||
| 213 | * @return DirectoryResource|FileResource|GenericResource |
||
| 214 | */ |
||
| 215 | 97 | protected function createResource($filesystemPath, $path = null) |
|
| 216 | { |
||
| 217 | // Link resource |
||
| 218 | 97 | if (0 === strpos($filesystemPath, 'l:')) { |
|
| 219 | 8 | return $this->createLinkResource(substr($filesystemPath, 2), $path); |
|
| 220 | } |
||
| 221 | |||
| 222 | // Filesystem resource |
||
| 223 | 97 | if (is_string($filesystemPath)) { |
|
| 224 | 95 | $filesystemPath = $this->resolveRelativePath($filesystemPath); |
|
| 225 | |||
| 226 | 95 | if (file_exists($filesystemPath)) { |
|
| 227 | 95 | return $this->createFilesystemResource($filesystemPath, $path); |
|
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | 11 | return $this->createVirtualResource($path); |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Create a link resource to another resource of the repository. |
||
| 236 | * |
||
| 237 | * @param string $targetPath The target path. |
||
| 238 | * @param string|null $path The repository path. |
||
| 239 | * |
||
| 240 | * @return LinkResource The link resource. |
||
| 241 | * |
||
| 242 | * @throws RuntimeException If the targeted resource does not exist. |
||
| 243 | */ |
||
| 244 | 8 | protected function createLinkResource($targetPath, $path = null) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Create a resource using its filesystem path. |
||
| 254 | * |
||
| 255 | * If the filesystem path is a directory, a DirectoryResource will be created. |
||
| 256 | * If the filesystem path is a file, a FileResource will be created. |
||
| 257 | * If the filesystem does not exists, a GenericResource will be created. |
||
| 258 | * |
||
| 259 | * @param string $filesystemPath The filesystem path. |
||
| 260 | * @param string|null $path The repository path. |
||
| 261 | * |
||
| 262 | * @return DirectoryResource|FileResource The created resource. |
||
| 263 | * |
||
| 264 | * @throws RuntimeException If the file / directory does not exist. |
||
| 265 | */ |
||
| 266 | 95 | protected function createFilesystemResource($filesystemPath, $path = null) |
|
| 267 | { |
||
| 268 | 95 | $resource = null; |
|
| 269 | |||
| 270 | 95 | if (is_dir($filesystemPath)) { |
|
| 271 | 53 | $resource = new DirectoryResource($filesystemPath); |
|
| 272 | 68 | } elseif (is_file($filesystemPath)) { |
|
| 273 | 68 | $resource = new FileResource($filesystemPath); |
|
| 274 | } |
||
| 275 | |||
| 276 | 95 | if ($resource) { |
|
| 277 | 95 | $resource->attachTo($this, $path); |
|
| 278 | |||
| 279 | 95 | return $resource; |
|
| 280 | } |
||
| 281 | |||
| 282 | throw new RuntimeException(sprintf( |
||
| 283 | 'Trying to create a FilesystemResource on a non-existing file or directory "%s"', |
||
| 284 | $filesystemPath |
||
| 285 | )); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string|null $path |
||
| 290 | * |
||
| 291 | * @return GenericResource |
||
| 292 | */ |
||
| 293 | 11 | protected function createVirtualResource($path = null) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Transform a relative path into an absolute path. |
||
| 303 | * |
||
| 304 | * @param string $relativePath |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | 100 | protected function resolveRelativePath($relativePath) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Transform a collection of relative paths into a collection of absolute paths. |
||
| 320 | * |
||
| 321 | * @param string[] $relativePaths |
||
| 322 | * |
||
| 323 | * @return string[] |
||
| 324 | */ |
||
| 325 | 60 | protected function resolveRelativePaths($relativePaths) |
|
| 326 | { |
||
| 327 | 60 | foreach ($relativePaths as $key => $relativePath) { |
|
| 328 | 60 | $relativePaths[$key] = $this->resolveRelativePath($relativePath); |
|
| 329 | } |
||
| 330 | |||
| 331 | 60 | return $relativePaths; |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check if the given target path represents a link. |
||
| 336 | * |
||
| 337 | * @param string $targetPath |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | 18 | protected function isLinkTarget($targetPath) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Add a link tag at the beginning of the given target path to represent a link. |
||
| 348 | * |
||
| 349 | * @param string $targetPath |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 14 | protected function createLinkTarget($targetPath) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get the target path of a link by removing the link tag. |
||
| 364 | * |
||
| 365 | * @param string $targetPath |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | 6 | protected function getLinkTarget($targetPath) |
|
| 377 | } |
||
| 378 |