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:
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 | 169 | public function __construct(KeyValueStore $store, $baseDirectory) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 144 | 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 | 138 | 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 | 138 | protected function ensureDirectoryExists($path) |
|
169 | |||
170 | /** |
||
171 | * Create the repository root. |
||
172 | */ |
||
173 | 169 | 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 | 134 | 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 | 90 | protected function createResource($filesystemPath, $path = null) |
|
216 | { |
||
217 | // Link resource |
||
218 | 90 | if (0 === strpos($filesystemPath, 'l:')) { |
|
219 | 4 | return $this->createLinkResource(substr($filesystemPath, 2), $path); |
|
220 | } |
||
221 | |||
222 | // Filesystem resource |
||
223 | 90 | if (is_string($filesystemPath)) { |
|
224 | 88 | $filesystemPath = $this->resolveRelativePath($filesystemPath); |
|
225 | |||
226 | 88 | if (file_exists($filesystemPath)) { |
|
227 | 88 | return $this->createFilesystemResource($filesystemPath, $path); |
|
228 | } |
||
229 | 1 | } |
|
230 | |||
231 | 12 | 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 | 4 | 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 | 88 | protected function createFilesystemResource($filesystemPath, $path = null) |
|
287 | |||
288 | /** |
||
289 | * @param string|null $path |
||
290 | * |
||
291 | * @return GenericResource |
||
292 | */ |
||
293 | 12 | 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 | 93 | 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 | 58 | protected function resolveRelativePaths($relativePaths) |
|
333 | } |
||
334 |