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 |
||
| 50 | class OptimizedPathMappingRepository extends AbstractPathMappingRepository implements EditableRepository |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | 48 | public function get($path) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 11 | public function find($query, $language = 'glob') |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 19 | public function contains($query, $language = 'glob') |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 44 | public function remove($query, $language = 'glob') |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 11 | public function listChildren($path) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | 7 | public function hasChildren($path) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $path |
||
| 162 | * @param FilesystemResource $resource |
||
| 163 | */ |
||
| 164 | 66 | protected function addFilesystemResource($path, FilesystemResource $resource) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | 3 | protected function addLinkResource($path, LinkResource $resource) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | */ |
||
| 193 | 4 | protected function followLinks($path) |
|
| 194 | { |
||
| 195 | 4 | $store = $this->store->getMultiple($this->store->keys()); |
|
| 196 | |||
| 197 | 4 | foreach ($store as $resourcePath => $filesystemPath) { |
|
| 198 | 4 | if (0 === strpos($filesystemPath, 'l:') && 0 === strpos($path, $resourcePath)) { |
|
| 199 | 1 | $targetPath = substr($filesystemPath, 2); |
|
| 200 | 1 | $realpath = substr_replace($path, rtrim($targetPath, '/'), 0, strlen($resourcePath)); |
|
| 201 | |||
| 202 | 1 | if ($this->store->exists($realpath)) { |
|
| 203 | 1 | return $this->createResource($this->store->get($realpath), $realpath); |
|
| 204 | } |
||
| 205 | 1 | } |
|
| 206 | 4 | } |
|
| 207 | |||
| 208 | 4 | return null; |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $path |
||
| 213 | */ |
||
| 214 | 7 | private function removePath($path) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Returns an iterator for the children paths of a resource. |
||
| 233 | * |
||
| 234 | * @param PuliResource $resource The resource. |
||
| 235 | * |
||
| 236 | * @return RegexFilterIterator The iterator of paths. |
||
| 237 | */ |
||
| 238 | 9 | View Code Duplication | private function getChildIterator(PuliResource $resource) |
| 249 | |||
| 250 | /** |
||
| 251 | * Returns a recursive iterator for the children paths under a given path. |
||
| 252 | * |
||
| 253 | * @param string $path The path. |
||
| 254 | * |
||
| 255 | * @return RegexFilterIterator The iterator of paths. |
||
| 256 | */ |
||
| 257 | 7 | View Code Duplication | private function getRecursivePathChildIterator($path) |
| 268 | |||
| 269 | /** |
||
| 270 | * Returns an iterator for a glob. |
||
| 271 | * |
||
| 272 | * @param string $glob The glob. |
||
| 273 | * |
||
| 274 | * @return GlobFilterIterator The iterator of paths. |
||
| 275 | */ |
||
| 276 | 8 | private function getGlobIterator($glob) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Transform an iterator of paths into a collection of resources. |
||
| 286 | * |
||
| 287 | * @param Iterator $iterator |
||
| 288 | * |
||
| 289 | * @return ArrayResourceCollection |
||
| 290 | */ |
||
| 291 | 12 | private function iteratorToCollection(Iterator $iterator) |
|
| 302 | } |
||
| 303 |