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 | 168 | public function __construct(KeyValueStore $store, $baseDirectory) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 146 | public function add($path, $resource) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Add the resource (internal method after checks of add()). |
||
| 90 | * |
||
| 91 | * @param string $path |
||
| 92 | * @param PuliResource $resource |
||
| 93 | */ |
||
| 94 | 140 | private function addResource($path, $resource) |
|
| 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 | 140 | protected function ensureDirectoryExists($path) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Create the repository root. |
||
| 172 | */ |
||
| 173 | 168 | protected function createRoot() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Count the number of elements in the store. |
||
| 184 | * |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | 9 | protected function countStore() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Sort the store by keys. |
||
| 198 | */ |
||
| 199 | 136 | protected function sortStore() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Create a filesystem or generic resource. |
||
| 210 | * |
||
| 211 | * @param string|null $filesystemPath |
||
| 212 | * |
||
| 213 | * @return DirectoryResource|FileResource|GenericResource |
||
| 214 | */ |
||
| 215 | 89 | protected function createResource($filesystemPath, $path = null) |
|
| 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 | 6 | 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 | 87 | protected function createFilesystemResource($filesystemPath, $path = null) |
|
| 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 | 92 | 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 | 56 | protected function resolveRelativePaths($relativePaths) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Check if the given target path represents a link. |
||
| 336 | * |
||
| 337 | * @param string $targetPath |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | 10 | public static 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 | 6 | public static 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 | 2 | public static function getLinkTarget($targetPath) |
|
| 377 | } |
||
| 378 |