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:
Complex classes like AbstractCollection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | abstract class AbstractCollection implements CollectionInterface |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * An array containing the entries of this collection. |
||
| 41 | * |
||
| 42 | * @var VersionInterface[] |
||
| 43 | */ |
||
| 44 | private $elements; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Initializes a new AbstractCollection. |
||
| 48 | * |
||
| 49 | * @param VersionInterface[] $elements |
||
| 50 | * |
||
| 51 | * @throws AlreadyExistsException |
||
| 52 | * @throws CollectionException |
||
| 53 | * @throws InvalidArgumentException |
||
| 54 | */ |
||
| 55 | 158 | public function __construct($elements = []) |
|
| 56 | { |
||
| 57 | 158 | if (!is_array($elements)) { |
|
| 58 | 4 | if ($elements instanceof \Traversable) { |
|
| 59 | 3 | $elements = ArrayUtils::iteratorToArray($elements); |
|
| 60 | } else { |
||
| 61 | 1 | throw new InvalidArgumentException( |
|
| 62 | 1 | "Constructor parameter 'versions' must be an array or Traversable object." |
|
| 63 | ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | 157 | $this->elements = []; |
|
| 68 | 157 | foreach ($elements as $element) { |
|
| 69 | 141 | if (!is_object($element) || !$element instanceof VersionInterface) { |
|
| 70 | 1 | throw CollectionException::invalidObjectException($element, VersionInterface::class); |
|
| 71 | } |
||
| 72 | 140 | $this->add($element); |
|
| 73 | } |
||
| 74 | 156 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritDoc} |
||
| 78 | */ |
||
| 79 | 21 | final public function toArray() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | */ |
||
| 87 | 26 | final public function first() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | 13 | final public function last() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritDoc} |
||
| 102 | */ |
||
| 103 | 1 | final public function key() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritDoc} |
||
| 110 | */ |
||
| 111 | 1 | final public function next() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritDoc} |
||
| 118 | */ |
||
| 119 | 1 | final public function current() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritDoc} |
||
| 126 | */ |
||
| 127 | 4 | public function remove($key) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritDoc} |
||
| 142 | */ |
||
| 143 | 144 | final public function contains($key) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritDoc} |
||
| 151 | */ |
||
| 152 | 1 | final public function containsVersion(VersionInterface $version) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritDoc} |
||
| 160 | */ |
||
| 161 | 2 | View Code Duplication | final public function exists(Closure $p) |
|
|
|||
| 162 | { |
||
| 163 | 2 | foreach ($this->elements as $key => $element) { |
|
| 164 | 2 | if ($p($key, $element)) { |
|
| 165 | 2 | return true; |
|
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | 1 | return false; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritDoc} |
||
| 174 | */ |
||
| 175 | 59 | final public function get($key) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritDoc} |
||
| 183 | */ |
||
| 184 | 34 | final public function getKeys() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritDoc} |
||
| 191 | */ |
||
| 192 | 1 | final public function getValues() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritDoc} |
||
| 199 | */ |
||
| 200 | 53 | final public function count() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritDoc} |
||
| 207 | */ |
||
| 208 | 10 | public function replace(VersionInterface $version) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritDoc} |
||
| 219 | */ |
||
| 220 | 143 | public function add(VersionInterface $value) |
|
| 221 | { |
||
| 222 | 143 | $key = $value->getId()->toString(); |
|
| 223 | 143 | if ($this->contains($key)) { |
|
| 224 | 1 | throw new AlreadyExistsException(sprintf( |
|
| 225 | 1 | 'Element with key "%s" already exists. Remove it first or use replace() if you want to overwrite it.', |
|
| 226 | $key |
||
| 227 | )); |
||
| 228 | } |
||
| 229 | 143 | $this->elements[$key] = $value; |
|
| 230 | 143 | return true; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritDoc} |
||
| 235 | */ |
||
| 236 | 3 | final public function isEmpty() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Required by interface IteratorAggregate. |
||
| 243 | * |
||
| 244 | * {@inheritDoc} |
||
| 245 | */ |
||
| 246 | 54 | final public function getIterator() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritDoc} |
||
| 253 | */ |
||
| 254 | 34 | final public function map(Closure $func) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritDoc} |
||
| 261 | */ |
||
| 262 | 1 | final public function filter(Closure $p) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritDoc} |
||
| 269 | */ |
||
| 270 | 4 | View Code Duplication | final public function forAll(Closure $p) |
| 271 | { |
||
| 272 | 4 | foreach ($this->elements as $key => $element) { |
|
| 273 | 4 | if ( ! $p($key, $element)) { |
|
| 274 | 4 | return false; |
|
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | 3 | return true; |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritDoc} |
||
| 283 | */ |
||
| 284 | 14 | final public function partition(Closure $p) |
|
| 285 | { |
||
| 286 | 14 | $matches = $noMatches = array(); |
|
| 287 | |||
| 288 | 14 | foreach ($this->elements as $key => $element) { |
|
| 289 | 14 | if ($p($key, $element)) { |
|
| 290 | 12 | $matches[$key] = $element; |
|
| 291 | } else { |
||
| 292 | 14 | $noMatches[$key] = $element; |
|
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | 14 | return array(new static($matches), new static($noMatches)); |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns a string representation of this object. |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 1 | public function __toString() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritDoc} |
||
| 311 | */ |
||
| 312 | 8 | public function clear() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @inheritDoc |
||
| 319 | */ |
||
| 320 | 2 | final public function slice($offset, $length = null) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @inheritdoc |
||
| 327 | */ |
||
| 328 | 10 | final public function merge(CollectionInterface $collection) |
|
| 329 | { |
||
| 330 | 10 | foreach ($collection as $version) { |
|
| 331 | 9 | $this->replace($version); |
|
| 332 | } |
||
| 333 | |||
| 334 | 10 | return $this; |
|
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @inheritdoc |
||
| 339 | */ |
||
| 340 | 33 | final public function getPosition($key) { |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @inheritdoc |
||
| 346 | */ |
||
| 347 | 34 | final public function getByPosition($position) |
|
| 356 | } |
||
| 357 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.