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 | 10 | public function __construct($items = array()) |
|
19 | { |
||
20 | 10 | $this->items = $items; |
|
21 | 10 | } |
|
22 | |||
23 | public function paginate($nbItemPerPage, $currentPage = 1) |
||
24 | { |
||
25 | $this->pagination['page'] = $currentPage; |
||
26 | $this->pagination['nbItems'] = count($this->items); |
||
27 | $this->pagination['nbPages'] = ceil($this->pagination['nbItems'] / $nbItemPerPage); |
||
28 | |||
29 | $this->items = array_slice($this->items,($currentPage - 1) * $nbItemPerPage, $nbItemPerPage); |
||
30 | |||
31 | |||
32 | return $this; |
||
33 | } |
||
34 | |||
35 | public function getPossibleValuesFor($args, $key = null) |
||
36 | { |
||
37 | if (!is_array($args)) { |
||
38 | $args = array('format' => '%s', 'data' => array($args)); |
||
39 | } |
||
40 | |||
41 | $values = array(); |
||
42 | foreach ($this->items as $itemKey => $item) { |
||
43 | $itemValues = array(); |
||
44 | foreach ($args['data'] as $arg) { |
||
45 | $itemValues[] = dataGet($item, $arg); |
||
46 | } |
||
47 | $arrayKey = ($key !== null) ? dataGet($item, $key) : null; |
||
48 | $values[$arrayKey] = vsprintf($args['format'], $itemValues); |
||
49 | } |
||
50 | |||
51 | return $values; |
||
52 | } |
||
53 | |||
54 | public function getValuesFor($name) |
||
55 | { |
||
56 | $values = array(); |
||
57 | foreach ($this->items as $item) { |
||
58 | $values[] = dataGet($item, $name); |
||
59 | } |
||
60 | |||
61 | return $values; |
||
62 | } |
||
63 | |||
64 | 4 | public function getItems() |
|
68 | |||
69 | /*public function addItemLink($linkId) |
||
70 | { |
||
71 | $this->items[$this->itemOffset] = $linkId; |
||
72 | // add mapping between item->index and $position in items pool |
||
73 | $this->mapping[$this->itemOffset] = $linkId; |
||
74 | |||
75 | $this->itemOffset++; |
||
76 | }*/ |
||
77 | |||
78 | |||
79 | |||
80 | public function getItemFromKey($key) |
||
87 | |||
88 | |||
89 | // Implementation of Countable Interface |
||
90 | 1 | public function count() |
|
91 | { |
||
92 | 1 | return count($this->items); |
|
93 | } |
||
94 | |||
95 | // Implementation of IteratorAggregate Interface |
||
96 | public function getIterator() |
||
97 | { |
||
98 | return new \ArrayIterator($this->items); |
||
99 | } |
||
100 | /*public function current() |
||
101 | { |
||
102 | return $this->offsetGet($this->iteratorPosition); |
||
103 | } |
||
104 | |||
105 | public function next() |
||
106 | { |
||
107 | ++$this->iteratorPosition; |
||
108 | } |
||
109 | |||
110 | public function key() |
||
111 | { |
||
112 | return $this->iteratorPosition; |
||
113 | } |
||
114 | |||
115 | public function valid() |
||
116 | { |
||
117 | return isset($this->items[$this->iteratorPosition]); |
||
118 | } |
||
119 | |||
120 | public function rewind() |
||
121 | { |
||
122 | $this->iteratorPosition = 0; |
||
123 | }*/ |
||
124 | |||
125 | // Implementation of ArrayAccess Interface |
||
126 | 1 | public function offsetExists($offset) |
|
130 | |||
131 | public function offsetGet($offset) |
||
147 | |||
148 | public function offsetSet($offset, $value) |
||
156 | |||
157 | public function offsetUnset($offset) |
||
161 | |||
162 | private function cleanStr($str) |
||
175 | |||
176 | // to be deprecated |
||
177 | public function getFirstItem() |
||
178 | { |
||
179 | foreach ($this->items as $currentItem) { |
||
180 | return $currentItem; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | // to be deprecated |
||
185 | public function getRandom($nb = 1) |
||
195 | |||
196 | // Helpers |
||
197 | 1 | public function first() |
|
198 | { |
||
199 | 1 | foreach ($this->items as $currentItem) { |
|
200 | 1 | return $currentItem; |
|
201 | } |
||
202 | } |
||
203 | |||
204 | 1 | public function last() |
|
205 | { |
||
206 | 1 | if (count($this->items)) { |
|
207 | 1 | return end($this->items); |
|
208 | } else { |
||
209 | 1 | return null; |
|
210 | } |
||
211 | } |
||
212 | |||
213 | 1 | public function isEmpty() |
|
214 | { |
||
215 | 1 | return empty($this->items); |
|
216 | } |
||
217 | |||
218 | 1 | public function sum($field = null) |
|
219 | { |
||
220 | 1 | if ($field == null) { |
|
221 | 1 | return array_sum($this->items); |
|
222 | } |
||
223 | $result = 0; |
||
224 | foreach ($this->items as $item) { |
||
225 | $result += dataGet($item, $field); |
||
226 | } |
||
227 | return $result; |
||
228 | } |
||
229 | |||
230 | public function random($nbItems = 1) |
||
244 | |||
245 | public function shuffle() |
||
246 | { |
||
247 | shuffle($this->items); |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | 1 | public function unique() |
|
253 | { |
||
254 | 1 | return new static(array_unique($this->items)); |
|
255 | } |
||
256 | |||
257 | public function each(\Closure $callback) |
||
258 | { |
||
259 | array_map($callback, $this->items); |
||
260 | return $this; |
||
261 | } |
||
262 | |||
263 | public function sort(\Closure $closure) |
||
264 | { |
||
265 | uasort($this->items, $closure); |
||
266 | |||
267 | return $this; |
||
268 | } |
||
269 | |||
270 | public function sortBy($field, $reverse = false) |
||
271 | { |
||
272 | if ($reverse) { |
||
273 | View Code Duplication | $sortFunction = function($a, $b) use ($field) { |
|
274 | $first = dataGet($a, $field); |
||
275 | $second = dataGet($b, $field); |
||
276 | if ($first == $second) { |
||
277 | return 0; |
||
278 | } |
||
279 | return ($first > $second) ? -1 : 1; |
||
280 | }; |
||
281 | } else { |
||
282 | View Code Duplication | $sortFunction = function($a, $b) use($field) { |
|
283 | $first = dataGet($a, $field); |
||
284 | $second = dataGet($b, $field); |
||
285 | if ($first == $second) { |
||
286 | return 0; |
||
287 | } |
||
288 | return ($first < $second) ? -1 : 1; |
||
289 | }; |
||
290 | } |
||
291 | |||
292 | |||
293 | usort($this->items, $sortFunction); |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | 1 | public function filter(\Closure $closure) |
|
299 | { |
||
300 | 1 | return new static(array_filter($this->items, $closure)); |
|
301 | } |
||
302 | |||
303 | public function search($value, $strict = false) |
||
304 | { |
||
305 | return array_search($value, $this->items, $strict); |
||
306 | } |
||
307 | |||
308 | 1 | public function has($key) |
|
309 | { |
||
310 | 1 | return $this->offsetExists($key); |
|
311 | } |
||
312 | |||
313 | public function keys() |
||
314 | { |
||
315 | return array_keys($this->items); |
||
316 | } |
||
317 | |||
318 | public function prepend($item) |
||
324 | |||
325 | 1 | public function push($item) |
|
326 | { |
||
327 | 1 | $this->items[] = $item; |
|
328 | |||
329 | 1 | return $this; |
|
330 | } |
||
331 | |||
332 | public function put($key, $val) |
||
333 | { |
||
334 | $this->items[$key] = $val; |
||
335 | |||
336 | return $this; |
||
337 | } |
||
338 | public function shift() |
||
339 | { |
||
340 | return array_shift($this->items); |
||
341 | } |
||
342 | |||
343 | public function pop() |
||
347 | |||
348 | public function reverse() |
||
349 | { |
||
350 | return new static(array_reverse($this->items)); |
||
351 | } |
||
352 | |||
353 | public function reduce(callable $callback, $initial = null) |
||
354 | { |
||
355 | return array_reduce($this->items, $callback, $initial); |
||
356 | } |
||
357 | |||
358 | public function slice($offset, $length = null, $preserveKeys = false) |
||
362 | |||
363 | public function take($limit = null) |
||
364 | { |
||
365 | if ($limit < 0) { |
||
366 | return $this->slice(abs($limit), $limit); |
||
367 | } else { |
||
368 | return $this->slice(0, $limit); |
||
369 | } |
||
370 | } |
||
371 | |||
372 | public function splice($offset, $length = null, $replacement = array()) |
||
373 | { |
||
374 | return new static(array_splice($this->items, $offset, $length, $replacement)); |
||
375 | } |
||
376 | |||
377 | public function chunk($size, $preserveKeys = false) |
||
378 | { |
||
379 | $result = new static; |
||
380 | foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) |
||
381 | { |
||
382 | $result->push(new static($chunk)); |
||
386 | } |
||
387 |