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 AbstractEditableRepository |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var PuliResource[] |
||
| 48 | */ |
||
| 49 | private $resources = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Create the repository. |
||
| 53 | * |
||
| 54 | * @param ChangeStream|null $changeStream If provided, the repository will log |
||
| 55 | * resources changes in this change stream. |
||
| 56 | */ |
||
| 57 | 93 | public function __construct(ChangeStream $changeStream = null) |
|
| 58 | { |
||
| 59 | 93 | parent::__construct($changeStream); |
|
| 60 | |||
| 61 | 93 | $this->clear(); |
|
| 62 | 93 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | 46 | public function get($path) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 24 | public function find($query, $language = 'glob') |
|
| 82 | { |
||
| 83 | 24 | $this->validateSearchLanguage($language); |
|
| 84 | |||
| 85 | 22 | $query = $this->sanitizePath($query); |
|
| 86 | 16 | $resources = array(); |
|
| 87 | |||
| 88 | 16 | if (Glob::isDynamic($query)) { |
|
| 89 | 7 | $resources = $this->getGlobIterator($query); |
|
| 90 | 9 | } elseif (isset($this->resources[$query])) { |
|
| 91 | 9 | $resources = array($this->resources[$query]); |
|
| 92 | } |
||
| 93 | |||
| 94 | 16 | return new ArrayResourceCollection($resources); |
|
|
|
|||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 16 | public function contains($query, $language = 'glob') |
|
| 101 | { |
||
| 102 | 16 | $this->validateSearchLanguage($language); |
|
| 103 | |||
| 104 | 15 | $query = $this->sanitizePath($query); |
|
| 105 | |||
| 106 | 12 | if (Glob::isDynamic($query)) { |
|
| 107 | 1 | $iterator = $this->getGlobIterator($query); |
|
| 108 | 1 | $iterator->rewind(); |
|
| 109 | |||
| 110 | 1 | return $iterator->valid(); |
|
| 111 | } |
||
| 112 | |||
| 113 | 11 | return isset($this->resources[$query]); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | 71 | public function add($path, $resource) |
|
| 120 | { |
||
| 121 | 71 | $path = $this->sanitizePath($path); |
|
| 122 | |||
| 123 | 68 | if ($resource instanceof ResourceCollection) { |
|
| 124 | 2 | $this->ensureDirectoryExists($path); |
|
| 125 | 2 | foreach ($resource as $child) { |
|
| 126 | 2 | $this->addResource($path.'/'.$child->getName(), $child); |
|
| 127 | } |
||
| 128 | |||
| 129 | // Keep the resources sorted by file name |
||
| 130 | 2 | ksort($this->resources); |
|
| 131 | |||
| 132 | 2 | return; |
|
| 133 | } |
||
| 134 | |||
| 135 | 66 | if ($resource instanceof PuliResource) { |
|
| 136 | 65 | $this->ensureDirectoryExists(Path::getDirectory($path)); |
|
| 137 | 65 | $this->addResource($path, $resource); |
|
| 138 | |||
| 139 | 65 | ksort($this->resources); |
|
| 140 | |||
| 141 | 65 | return; |
|
| 142 | } |
||
| 143 | |||
| 144 | 1 | throw new UnsupportedResourceException(sprintf( |
|
| 145 | 1 | 'The passed resource must be a PuliResource or ResourceCollection. Got: %s', |
|
| 146 | 1 | is_object($resource) ? get_class($resource) : gettype($resource) |
|
| 147 | )); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | 13 | public function remove($query, $language = 'glob') |
|
| 154 | { |
||
| 155 | 13 | $resources = $this->find($query, $language); |
|
| 156 | 9 | $nbOfResources = count($this->resources); |
|
| 157 | |||
| 158 | // Run the assertion after find(), so that we know that $query is valid |
||
| 159 | 9 | Assert::notEmpty(trim($query, '/'), 'The root directory cannot be removed.'); |
|
| 160 | |||
| 161 | 7 | foreach ($resources as $resource) { |
|
| 162 | 7 | $this->removeResource($resource); |
|
| 163 | } |
||
| 164 | |||
| 165 | 7 | return $nbOfResources - count($this->resources); |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 93 | public function clear() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | 10 | public function listChildren($path) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | 6 | public function hasChildren($path) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Recursively creates a directory for a path. |
||
| 207 | * |
||
| 208 | * @param string $path A directory path. |
||
| 209 | */ |
||
| 210 | 67 | private function ensureDirectoryExists($path) |
|
| 224 | |||
| 225 | 67 | private function addResource($path, PuliResource $resource) |
|
| 226 | { |
||
| 227 | // Don't modify resources attached to other repositories |
||
| 228 | 67 | if ($resource->isAttached()) { |
|
| 229 | 3 | $resource = clone $resource; |
|
| 230 | } |
||
| 231 | |||
| 232 | 67 | $basePath = '/' === $path ? $path : $path.'/'; |
|
| 233 | |||
| 234 | // Read children before attaching the resource to this repository |
||
| 235 | 67 | $children = $resource->listChildren(); |
|
| 236 | |||
| 237 | 67 | $resource->attachTo($this, $path); |
|
| 238 | |||
| 239 | // Add the resource before adding its children, so that the array |
||
| 240 | // stays sorted |
||
| 241 | 67 | $this->resources[$path] = $resource; |
|
| 242 | |||
| 243 | 67 | foreach ($children as $name => $child) { |
|
| 244 | 29 | $this->addResource($basePath.$name, $child); |
|
| 245 | } |
||
| 246 | |||
| 247 | 67 | $this->appendToChangeStream($resource); |
|
| 248 | 67 | } |
|
| 249 | |||
| 250 | 7 | private function removeResource(PuliResource $resource) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Returns an iterator for the children of a resource. |
||
| 272 | * |
||
| 273 | * @param PuliResource $resource The resource. |
||
| 274 | * |
||
| 275 | * @return RegexFilterIterator The iterator. |
||
| 276 | */ |
||
| 277 | 14 | View Code Duplication | private function getChildIterator(PuliResource $resource) |
| 289 | |||
| 290 | /** |
||
| 291 | * Returns an iterator for a glob. |
||
| 292 | * |
||
| 293 | * @param string $glob The glob. |
||
| 294 | * |
||
| 295 | * @return GlobFilterIterator The iterator. |
||
| 296 | */ |
||
| 297 | 8 | protected function getGlobIterator($glob) |
|
| 305 | } |
||
| 306 |
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.