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 IdentityWrapper 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 IdentityWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class IdentityWrapper implements Collection |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Collection |
||
| 20 | */ |
||
| 21 | protected $collection; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var callable |
||
| 25 | */ |
||
| 26 | protected $identityExtractor; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Collection $collection |
||
| 30 | */ |
||
| 31 | public function __construct(Collection $collection) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritDoc} |
||
| 38 | */ |
||
| 39 | public function count() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritDoc} |
||
| 46 | */ |
||
| 47 | public function add($element) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritDoc} |
||
| 54 | */ |
||
| 55 | public function clear() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritDoc} |
||
| 62 | */ |
||
| 63 | public function contains($element) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritDoc} |
||
| 70 | */ |
||
| 71 | public function isEmpty() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritDoc} |
||
| 78 | */ |
||
| 79 | public function remove($key) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritDoc} |
||
| 90 | */ |
||
| 91 | public function removeElement($element) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritDoc} |
||
| 98 | */ |
||
| 99 | public function containsKey($key) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritDoc} |
||
| 106 | */ |
||
| 107 | public function get($key) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritDoc} |
||
| 116 | */ |
||
| 117 | public function getKeys() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritDoc} |
||
| 126 | */ |
||
| 127 | public function getValues() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritDoc} |
||
| 134 | */ |
||
| 135 | public function set($key, $value) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritDoc} |
||
| 149 | */ |
||
| 150 | public function toArray() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritDoc} |
||
| 164 | */ |
||
| 165 | public function first() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritDoc} |
||
| 172 | */ |
||
| 173 | public function last() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritDoc} |
||
| 180 | */ |
||
| 181 | public function key() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritDoc} |
||
| 193 | */ |
||
| 194 | public function current() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritDoc} |
||
| 201 | */ |
||
| 202 | public function next() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritDoc} |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function exists(Closure $p) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritDoc} |
||
| 224 | */ |
||
| 225 | public function filter(Closure $p) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | View Code Duplication | public function forAll(Closure $p) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritDoc} |
||
| 247 | */ |
||
| 248 | public function map(Closure $func) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritDoc} |
||
| 260 | */ |
||
| 261 | public function partition(Closure $p) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritDoc} |
||
| 278 | */ |
||
| 279 | public function indexOf($element) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritDoc} |
||
| 286 | */ |
||
| 287 | public function slice($offset, $length = null) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritDoc} |
||
| 301 | */ |
||
| 302 | public function getIterator() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritDoc} |
||
| 309 | */ |
||
| 310 | public function offsetExists($offset) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritDoc} |
||
| 317 | */ |
||
| 318 | public function offsetGet($offset) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritDoc} |
||
| 325 | */ |
||
| 326 | public function offsetSet($offset, $value) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritDoc} |
||
| 337 | */ |
||
| 338 | public function offsetUnset($offset) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param callable $identityExtractor |
||
| 345 | * @return IdentityWrapper |
||
| 346 | */ |
||
| 347 | public function setIdentityExtractor(callable $identityExtractor) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * |
||
| 356 | * @return \Core\Collection\callable |
||
| 357 | */ |
||
| 358 | protected function getIdentityExtractor() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param mixed $element |
||
| 376 | * @return mixed |
||
| 377 | */ |
||
| 378 | protected function getKey($element) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param mixed $element |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | protected function getElement($key) |
||
| 393 | } |
||
| 394 |
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.