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 |
||
| 44 | class InMemoryRepository extends AbstractRepository implements EditableRepository |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var PuliResource[] |
||
| 48 | */ |
||
| 49 | private $resources = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * This array contains a copy of each LinkResource in $resources |
||
| 53 | * for performance reasons. |
||
| 54 | * |
||
| 55 | * @var LinkResource[] |
||
| 56 | */ |
||
| 57 | private $links = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Creates a new repository. |
||
| 61 | */ |
||
| 62 | 79 | public function __construct() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | 47 | public function get($path) |
|
| 71 | { |
||
| 72 | 47 | $path = $this->sanitizePath($path); |
|
| 73 | |||
| 74 | 38 | if (!isset($this->resources[$path])) { |
|
| 75 | 4 | return $this->followLinksOrFail($path); |
|
| 76 | } |
||
| 77 | |||
| 78 | 35 | return $this->resources[$path]; |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 24 | public function find($query, $language = 'glob') |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | 16 | public function contains($query, $language = 'glob') |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 69 | public function add($path, $resource) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | 13 | public function remove($query, $language = 'glob') |
|
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | 79 | public function clear() |
|
| 175 | { |
||
| 176 | 79 | $root = new GenericResource('/'); |
|
| 177 | 79 | $root->attachTo($this); |
|
| 178 | |||
| 179 | // Subtract root |
||
| 180 | 79 | $removed = count($this->resources) - 1; |
|
| 181 | |||
| 182 | 79 | $this->resources = array('/' => $root); |
|
| 183 | 79 | $this->links = array(); |
|
| 184 | |||
| 185 | 79 | return $removed; |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | 10 | public function listChildren($path) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | 6 | public function hasChildren($path) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Recursively creates a directory for a path. |
||
| 211 | * |
||
| 212 | * @param string $path A directory path. |
||
| 213 | */ |
||
| 214 | 65 | private function ensureDirectoryExists($path) |
|
| 228 | |||
| 229 | 65 | private function addResource($path, PuliResource $resource) |
|
| 230 | { |
||
| 231 | // Don't modify resources attached to other repositories |
||
| 232 | 65 | if ($resource->isAttached()) { |
|
| 233 | 2 | $resource = clone $resource; |
|
| 234 | 2 | } |
|
| 235 | |||
| 236 | 65 | $basePath = '/' === $path ? $path : $path.'/'; |
|
| 237 | |||
| 238 | // Read children before attaching the resource to this repository |
||
| 239 | 65 | $children = $resource->listChildren(); |
|
| 240 | |||
| 241 | 65 | $resource->attachTo($this, $path); |
|
| 242 | |||
| 243 | // Add the resource before adding its children, so that the array |
||
| 244 | // stays sorted |
||
| 245 | 65 | $this->resources[$path] = $resource; |
|
| 246 | |||
| 247 | 65 | if ($resource instanceof LinkResource) { |
|
| 248 | 3 | $this->links[$path] = $resource; |
|
| 249 | 3 | } |
|
| 250 | |||
| 251 | 65 | foreach ($children as $name => $child) { |
|
| 252 | 30 | $this->addResource($basePath.$name, $child); |
|
| 253 | 65 | } |
|
| 254 | 65 | } |
|
| 255 | |||
| 256 | 7 | private function removeResource(PuliResource $resource) |
|
| 257 | { |
||
| 258 | 7 | $path = $resource->getPath(); |
|
| 259 | |||
| 260 | // Ignore non-existing resources |
||
| 261 | 7 | if (!isset($this->resources[$path])) { |
|
| 262 | return; |
||
| 263 | } |
||
| 264 | |||
| 265 | // Recursively register directory contents |
||
| 266 | 7 | foreach ($this->getChildIterator($resource) as $child) { |
|
| 267 | 5 | $this->removeResource($child); |
|
| 268 | 7 | } |
|
| 269 | |||
| 270 | 7 | unset($this->resources[$path]); |
|
| 271 | |||
| 272 | 7 | if ($resource instanceof LinkResource && array_key_exists($path, $this->links)) { |
|
| 273 | unset($this->links[$path]); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Detach from locator |
||
| 277 | 7 | $resource->detach(); |
|
| 278 | 7 | } |
|
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | 4 | protected function followLinks($path) |
|
| 284 | { |
||
| 285 | 4 | foreach ($this->links as $link) { |
|
| 286 | 1 | $linkPath = rtrim($link->getPath(), '/'); |
|
| 287 | |||
| 288 | 1 | if (0 === strpos($path, $linkPath.'/')) { |
|
| 289 | 1 | $realTargetPath = rtrim($this->getRealTarget($link)->getPath(), '/'); |
|
| 290 | 1 | $realPath = substr_replace($path, $realTargetPath, 0, strlen($linkPath)); |
|
| 291 | |||
| 292 | 1 | if (array_key_exists($realPath, $this->resources)) { |
|
| 293 | 1 | return $this->resources[$realPath]; |
|
| 294 | } |
||
| 295 | } |
||
| 296 | 4 | } |
|
| 297 | |||
| 298 | 3 | return null; |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Find the real target of a given link (resolve all the links until |
||
| 303 | * a non-link resource is found). |
||
| 304 | * |
||
| 305 | * @param LinkResource $linkResource |
||
| 306 | * |
||
| 307 | * @return PuliResource |
||
| 308 | */ |
||
| 309 | 1 | private function getRealTarget(LinkResource $linkResource) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Returns an iterator for the children of a resource. |
||
| 322 | * |
||
| 323 | * @param PuliResource $resource The resource. |
||
| 324 | * |
||
| 325 | * @return RegexFilterIterator The iterator. |
||
| 326 | */ |
||
| 327 | 14 | View Code Duplication | private function getChildIterator(PuliResource $resource) |
| 339 | |||
| 340 | /** |
||
| 341 | * Returns an iterator for a glob. |
||
| 342 | * |
||
| 343 | * @param string $glob The glob. |
||
| 344 | * |
||
| 345 | * @return GlobFilterIterator The iterator. |
||
| 346 | */ |
||
| 347 | 8 | protected function getGlobIterator($glob) |
|
| 355 | } |
||
| 356 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.