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 Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Collection implements \IteratorAggregate, \Countable, \ArrayAccess, Interfaces\ICollection |
||
| 5 | { |
||
| 6 | |||
| 7 | protected $items = array(); |
||
| 8 | protected $mapping = array(); // to be deprecated ? |
||
| 9 | |||
| 10 | public $pagination = array( |
||
| 11 | 'nbPages' => 0, |
||
| 12 | 'page' => 1, |
||
| 13 | 'nbItems' => 0, |
||
| 14 | ); |
||
| 15 | |||
| 16 | //protected $iteratorPosition = 0; |
||
|
|
|||
| 17 | |||
| 18 | 17 | public function __construct($items = array()) |
|
| 22 | |||
| 23 | public function paginate($nbItemPerPage, $currentPage = 1) |
||
| 33 | |||
| 34 | public function getPossibleValuesFor($args, $key = null) |
||
| 52 | |||
| 53 | public function getValuesFor($name) |
||
| 62 | |||
| 63 | 10 | public function getItems() |
|
| 67 | |||
| 68 | /*public function addItemLink($linkId) |
||
| 69 | { |
||
| 70 | $this->items[$this->itemOffset] = $linkId; |
||
| 71 | // add mapping between item->index and $position in items pool |
||
| 72 | $this->mapping[$this->itemOffset] = $linkId; |
||
| 73 | |||
| 74 | $this->itemOffset++; |
||
| 75 | }*/ |
||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | public function getItemFromKey($key) |
||
| 80 | { |
||
| 81 | $invertedMapping = array_flip($this->mapping); |
||
| 82 | if (isset($invertedMapping[$key])) { |
||
| 83 | return $this->items[$invertedMapping[$key]]; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | // Implementation of Countable Interface |
||
| 89 | 1 | public function count() |
|
| 93 | |||
| 94 | // Implementation of IteratorAggregate Interface |
||
| 95 | public function getIterator() |
||
| 99 | /*public function current() |
||
| 100 | { |
||
| 101 | return $this->offsetGet($this->iteratorPosition); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function next() |
||
| 105 | { |
||
| 106 | ++$this->iteratorPosition; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function key() |
||
| 110 | { |
||
| 111 | return $this->iteratorPosition; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function valid() |
||
| 115 | { |
||
| 116 | return isset($this->items[$this->iteratorPosition]); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function rewind() |
||
| 120 | { |
||
| 121 | $this->iteratorPosition = 0; |
||
| 122 | }*/ |
||
| 123 | |||
| 124 | // Implementation of ArrayAccess Interface |
||
| 125 | 1 | public function offsetExists($offset) |
|
| 129 | |||
| 130 | public function offsetGet($offset) |
||
| 146 | |||
| 147 | public function offsetSet($offset, $value) |
||
| 148 | { |
||
| 149 | if (is_null($offset)) { |
||
| 150 | $this->items[] = $value; |
||
| 151 | } else { |
||
| 152 | $this->items[$offset] = $value; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | public function offsetUnset($offset) |
||
| 157 | { |
||
| 158 | unset($this->items[$offset]); |
||
| 159 | } |
||
| 160 | |||
| 161 | private function cleanStr($str) |
||
| 174 | |||
| 175 | // to be deprecated |
||
| 176 | public function getFirstItem() |
||
| 177 | { |
||
| 178 | foreach ($this->items as $currentItem) { |
||
| 179 | return $currentItem; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // to be deprecated |
||
| 184 | public function getRandom($nb = 1) |
||
| 194 | |||
| 195 | // Helpers |
||
| 196 | 1 | public function first() |
|
| 197 | { |
||
| 198 | 1 | foreach ($this->items as $currentItem) { |
|
| 199 | 1 | return $currentItem; |
|
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | 1 | public function last() |
|
| 211 | |||
| 212 | 1 | public function isEmpty() |
|
| 216 | |||
| 217 | 1 | public function sum($field = null) |
|
| 228 | |||
| 229 | public function random($nbItems = 1) |
||
| 243 | |||
| 244 | public function shuffle() |
||
| 250 | |||
| 251 | 1 | public function unique() |
|
| 255 | |||
| 256 | public function each(\Closure $callback) |
||
| 261 | |||
| 262 | public function sort(\Closure $closure) |
||
| 268 | |||
| 269 | public function sortBy($field, $reverse = false) |
||
| 296 | |||
| 297 | 1 | public function filter(\Closure $closure) |
|
| 301 | |||
| 302 | public function search($value, $strict = false) |
||
| 306 | |||
| 307 | 1 | public function has($key) |
|
| 311 | |||
| 312 | 1 | public function keys() |
|
| 316 | |||
| 317 | 1 | public function prepend($item) |
|
| 323 | |||
| 324 | 1 | public function push($item) |
|
| 330 | |||
| 331 | 1 | public function put($key, $val) |
|
| 341 | |||
| 342 | 1 | public function pop() |
|
| 346 | |||
| 347 | 1 | public function reverse() |
|
| 351 | |||
| 352 | public function reduce(callable $callback, $initial = null) |
||
| 356 | |||
| 357 | 1 | public function slice($offset, $length = null, $preserveKeys = false) |
|
| 361 | |||
| 362 | 1 | public function take($limit = null) |
|
| 370 | |||
| 371 | public function splice($offset, $length = null, $replacement = array()) |
||
| 375 | |||
| 376 | public function chunk($size, $preserveKeys = false) |
||
| 377 | { |
||
| 378 | $result = new static; |
||
| 379 | foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) { |
||
| 380 | $result->push(new static($chunk)); |
||
| 384 | } |
||
| 385 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.