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 | 83 | public function __construct() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | 51 | public function get($path) |
|
| 71 | { |
||
| 72 | 51 | $path = $this->sanitizePath($path); |
|
| 73 | |||
| 74 | 42 | if (!isset($this->resources[$path])) { |
|
| 75 | 6 | return $this->followLinksOrFail($path); |
|
| 76 | } |
||
| 77 | |||
| 78 | 39 | return $this->resources[$path]; |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 24 | public function find($query, $language = 'glob') |
|
| 85 | { |
||
| 86 | 24 | $this->validateSearchLanguage($language); |
|
| 87 | |||
| 88 | 22 | $query = $this->sanitizePath($query); |
|
| 89 | 16 | $resources = array(); |
|
| 90 | |||
| 91 | 16 | if (Glob::isDynamic($query)) { |
|
| 92 | 7 | $resources = $this->getGlobIterator($query); |
|
| 93 | 9 | } elseif (isset($this->resources[$query])) { |
|
| 94 | 9 | $resources = array($this->resources[$query]); |
|
| 95 | } |
||
| 96 | |||
| 97 | 16 | return new ArrayResourceCollection($resources); |
|
|
|
|||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | 16 | public function contains($query, $language = 'glob') |
|
| 104 | { |
||
| 105 | 16 | $this->validateSearchLanguage($language); |
|
| 106 | |||
| 107 | 15 | $query = $this->sanitizePath($query); |
|
| 108 | |||
| 109 | 12 | if (Glob::isDynamic($query)) { |
|
| 110 | 1 | $iterator = $this->getGlobIterator($query); |
|
| 111 | 1 | $iterator->rewind(); |
|
| 112 | |||
| 113 | 1 | return $iterator->valid(); |
|
| 114 | } |
||
| 115 | |||
| 116 | 11 | return isset($this->resources[$query]); |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 73 | public function add($path, $resource) |
|
| 123 | { |
||
| 124 | 73 | $path = $this->sanitizePath($path); |
|
| 125 | |||
| 126 | 70 | if ($resource instanceof ResourceCollection) { |
|
| 127 | 2 | $this->ensureDirectoryExists($path); |
|
| 128 | 2 | foreach ($resource as $child) { |
|
| 129 | 2 | $this->addResource($path.'/'.$child->getName(), $child); |
|
| 130 | } |
||
| 131 | |||
| 132 | // Keep the resources sorted by file name |
||
| 133 | 2 | ksort($this->resources); |
|
| 134 | |||
| 135 | 2 | return; |
|
| 136 | } |
||
| 137 | |||
| 138 | 68 | if ($resource instanceof PuliResource) { |
|
| 139 | 67 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
| 140 | 67 | $this->addResource($path, $resource); |
|
| 141 | |||
| 142 | 67 | ksort($this->resources); |
|
| 143 | |||
| 144 | 67 | return; |
|
| 145 | } |
||
| 146 | |||
| 147 | 1 | throw new UnsupportedResourceException(sprintf( |
|
| 148 | 1 | 'The passed resource must be a PuliResource or ResourceCollection. Got: %s', |
|
| 149 | 1 | is_object($resource) ? get_class($resource) : gettype($resource) |
|
| 150 | )); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | 13 | public function remove($query, $language = 'glob') |
|
| 157 | { |
||
| 158 | 13 | $resources = $this->find($query, $language); |
|
| 159 | 9 | $nbOfResources = count($this->resources); |
|
| 160 | |||
| 161 | // Run the assertion after find(), so that we know that $query is valid |
||
| 162 | 9 | Assert::notEmpty(trim($query, '/'), 'The root directory cannot be removed.'); |
|
| 163 | |||
| 164 | 7 | foreach ($resources as $resource) { |
|
| 165 | 7 | $this->removeResource($resource); |
|
| 166 | } |
||
| 167 | |||
| 168 | 7 | return $nbOfResources - count($this->resources); |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | 83 | public function clear() |
|
| 175 | { |
||
| 176 | 83 | $root = new GenericResource('/'); |
|
| 177 | 83 | $root->attachTo($this); |
|
| 178 | |||
| 179 | // Subtract root |
||
| 180 | 83 | $removed = count($this->resources) - 1; |
|
| 181 | |||
| 182 | 83 | $this->resources = array('/' => $root); |
|
| 183 | 83 | $this->links = array(); |
|
| 184 | |||
| 185 | 83 | 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 | 69 | private function ensureDirectoryExists($path) |
|
| 228 | |||
| 229 | 69 | private function addResource($path, PuliResource $resource) |
|
| 255 | |||
| 256 | 7 | private function removeResource(PuliResource $resource) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | 6 | protected function followLinks($path) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Returns an iterator for the children of a resource. |
||
| 303 | * |
||
| 304 | * @param PuliResource $resource The resource. |
||
| 305 | * |
||
| 306 | * @return RegexFilterIterator The iterator. |
||
| 307 | */ |
||
| 308 | 14 | View Code Duplication | private function getChildIterator(PuliResource $resource) |
| 320 | |||
| 321 | /** |
||
| 322 | * Returns an iterator for a glob. |
||
| 323 | * |
||
| 324 | * @param string $glob The glob. |
||
| 325 | * |
||
| 326 | * @return GlobFilterIterator The iterator. |
||
| 327 | */ |
||
| 328 | 8 | protected function getGlobIterator($glob) |
|
| 336 | } |
||
| 337 |
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.