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 EntityCollection 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 EntityCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class EntityCollection extends Collection |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Wrapper Factory. |
||
| 15 | * |
||
| 16 | * @var \Analogue\ORM\System\Wrappers\Factory |
||
| 17 | */ |
||
| 18 | protected $factory; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * EntityCollection constructor. |
||
| 22 | * |
||
| 23 | * @param array|null $entities |
||
| 24 | */ |
||
| 25 | public function __construct(array $entities = null) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Find an entity in the collection by key. |
||
| 34 | * |
||
| 35 | * @param mixed $key |
||
| 36 | * @param mixed $default |
||
| 37 | * |
||
| 38 | * @throws MappingException |
||
| 39 | * |
||
| 40 | * @return \Analogue\ORM\Entity |
||
| 41 | */ |
||
| 42 | public function find($key, $default = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add an entity to the collection. |
||
| 55 | * |
||
| 56 | * @param Mappable $entity |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function add($entity) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Remove an entity from the collection. |
||
| 69 | * |
||
| 70 | * @param $entity |
||
| 71 | * |
||
| 72 | * @throws MappingException |
||
| 73 | * |
||
| 74 | * @return mixed |
||
| 75 | */ |
||
| 76 | public function remove($entity) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Push an item onto the end of the collection. |
||
| 85 | * |
||
| 86 | * @param mixed $value |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function push($value) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Put an item in the collection by key. |
||
| 97 | * |
||
| 98 | * @param mixed $key |
||
| 99 | * @param mixed $value |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public function put($key, $value) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set the item at a given offset. |
||
| 110 | * |
||
| 111 | * @param mixed $key |
||
| 112 | * @param mixed $value |
||
| 113 | * |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function offsetSet($key, $value) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Determine if a key exists in the collection. |
||
| 127 | * |
||
| 128 | * @param mixed $key |
||
| 129 | * @param mixed|null $value |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | // public function contains($key, $value = null) |
||
| 134 | // { |
||
| 135 | // if (func_num_args() == 2) { |
||
| 136 | // return !$this->where($key, $value)->isEmpty(); |
||
| 137 | // } |
||
| 138 | |||
| 139 | // if ($this->useAsCallable($key)) { |
||
| 140 | // return !is_null($this->first($key)); |
||
| 141 | // } |
||
| 142 | |||
| 143 | // return !is_null($this->find($key)); |
||
| 144 | // } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Fetch a nested element of the collection. |
||
| 148 | * |
||
| 149 | * @param string $key |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | public function fetch($key) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Generic function for returning class.key value pairs. |
||
| 160 | * |
||
| 161 | * @throws MappingException |
||
| 162 | * |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function getEntityHashes() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get a subset of the collection from entity hashes. |
||
| 181 | * |
||
| 182 | * @param array $hashes |
||
| 183 | * |
||
| 184 | * @throws MappingException |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function getSubsetByHashes(array $hashes) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Merge the collection with the given items. |
||
| 209 | * |
||
| 210 | * @param array $items |
||
| 211 | * |
||
| 212 | * @throws MappingException |
||
| 213 | * |
||
| 214 | * @return self |
||
| 215 | */ |
||
| 216 | public function merge($items) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Diff the collection with the given items. |
||
| 229 | * |
||
| 230 | * @param \ArrayAccess|array $items |
||
| 231 | * |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function diff($items) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Intersect the collection with the given items. |
||
| 251 | * |
||
| 252 | * @param \ArrayAccess|array $items |
||
| 253 | * |
||
| 254 | * @throws MappingException |
||
| 255 | * |
||
| 256 | * @return self |
||
| 257 | */ |
||
| 258 | View Code Duplication | public function intersect($items) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Returns only the models from the collection with the specified keys. |
||
| 275 | * |
||
| 276 | * @param mixed $keys |
||
| 277 | * |
||
| 278 | * @return self |
||
| 279 | */ |
||
| 280 | public function only($keys) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns all models in the collection except the models with specified keys. |
||
| 289 | * |
||
| 290 | * @param mixed $keys |
||
| 291 | * |
||
| 292 | * @return self |
||
| 293 | */ |
||
| 294 | public function except($keys) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get a dictionary keyed by primary keys. |
||
| 303 | * |
||
| 304 | * @param \ArrayAccess|array $items |
||
| 305 | * |
||
| 306 | * @throws MappingException |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | public function getDictionary($items = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @throws MappingException |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function getEntityKeys() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param $entity |
||
| 335 | * |
||
| 336 | * @throws MappingException |
||
| 337 | * |
||
| 338 | * @return mixed |
||
| 339 | */ |
||
| 340 | protected function getEntityKey($entity) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get the max value of a given key. |
||
| 351 | * |
||
| 352 | * @param string|null $key |
||
| 353 | * |
||
| 354 | * @throws MappingException |
||
| 355 | * |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function max($key = null) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get the min value of a given key. |
||
| 370 | * |
||
| 371 | * @param string|null $key |
||
| 372 | * |
||
| 373 | * @throws MappingException |
||
| 374 | * |
||
| 375 | * @return mixed |
||
| 376 | */ |
||
| 377 | View Code Duplication | public function min($key = null) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get an array with the values of a given key. |
||
| 389 | * |
||
| 390 | * @param string $value |
||
| 391 | * @param string|null $key |
||
| 392 | * |
||
| 393 | * @return self |
||
| 394 | */ |
||
| 395 | public function pluck($value, $key = null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Alias for the "pluck" method. |
||
| 402 | * |
||
| 403 | * @param string $value |
||
| 404 | * @param string|null $key |
||
| 405 | * |
||
| 406 | * @return self |
||
| 407 | */ |
||
| 408 | public function lists($value, $key = null) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return only unique items from the collection. |
||
| 415 | * |
||
| 416 | * @param string|null $key |
||
| 417 | * @param bool $strict |
||
| 418 | * |
||
| 419 | * @throws MappingException |
||
| 420 | * |
||
| 421 | * @return self |
||
| 422 | */ |
||
| 423 | public function unique($key = null, $strict = false) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get a base Support collection instance from this collection. |
||
| 432 | * |
||
| 433 | * @return \Illuminate\Support\Collection |
||
| 434 | */ |
||
| 435 | public function toBase() |
||
| 439 | |||
| 440 | public function toArray() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the collection of items as JSON. |
||
| 447 | * |
||
| 448 | * @param int $options |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function toJson($options = 0) |
||
| 458 | } |
||
| 459 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.