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 | 3 | public function __construct($items = array()) |
|
19 | { |
||
20 | 3 | $this->items = $items; |
|
21 | 3 | } |
|
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 | 1 | 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) |
||
81 | { |
||
82 | $invertedMapping = array_flip($this->mapping); |
||
83 | if (isset($invertedMapping[$key])) { |
||
84 | return $this->items[$invertedMapping[$key]]; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | |||
89 | // Implementation of Countable Interface |
||
90 | public function count() |
||
91 | { |
||
92 | 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 | public function offsetExists($offset) |
||
127 | { |
||
128 | return isset($this->items[$offset]); |
||
129 | } |
||
130 | |||
131 | public function offsetGet($offset) |
||
132 | { |
||
133 | $item =isset($this->items[$offset]) ? $this->items[$offset] : null; |
||
134 | if (gettype($item) == 'object' || $item == null) { |
||
135 | return $item; |
||
136 | } else { |
||
137 | // Lazy load |
||
138 | $itemType = $this::ITEM_TYPE; |
||
139 | $itemToLoad = new $itemType; |
||
140 | $itemToLoad->load($this->items[$offset]); |
||
141 | |||
142 | $this->items[$offset] = $itemToLoad; |
||
143 | |||
144 | return $this->items[$offset]; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | public function offsetSet($offset, $value) |
||
149 | { |
||
150 | if (is_null($offset)) { |
||
151 | $this->items[] = $value; |
||
152 | } else { |
||
153 | $this->items[$offset] = $value; |
||
154 | } |
||
155 | } |
||
156 | |||
157 | public function offsetUnset($offset) |
||
158 | { |
||
159 | unset($this->items[$offset]); |
||
160 | } |
||
161 | |||
162 | private function cleanStr($str) |
||
163 | { |
||
164 | |||
165 | $str = mb_strtolower($str, 'utf-8'); |
||
166 | $str = strtr( |
||
167 | $str, |
||
168 | array( |
||
169 | 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'a'=>'a', 'a'=>'a', 'a'=>'a', 'ç'=>'c', 'c'=>'c', 'c'=>'c', 'c'=>'c', 'c'=>'c', 'd'=>'d', 'd'=>'d', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'e'=>'e', 'g'=>'g', 'g'=>'g', 'g'=>'g', 'h'=>'h', 'h'=>'h', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', 'i'=>'i', '?'=>'i', 'j'=>'j', 'k'=>'k', '?'=>'k', 'l'=>'l', 'l'=>'l', 'l'=>'l', '?'=>'l', 'l'=>'l', 'ñ'=>'n', 'n'=>'n', 'n'=>'n', 'n'=>'n', '?'=>'n', '?'=>'n', 'ð'=>'o', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'o'=>'o', 'o'=>'o', 'o'=>'o', 'œ'=>'o', 'ø'=>'o', 'r'=>'r', 'r'=>'r', 's'=>'s', 's'=>'s', 's'=>'s', 'š'=>'s', '?'=>'s', 't'=>'t', 't'=>'t', 't'=>'t', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'u'=>'u', 'w'=>'w', 'ý'=>'y', 'ÿ'=>'y', 'y'=>'y', 'z'=>'z', 'z'=>'z', 'ž'=>'z' |
||
170 | ) |
||
171 | ); |
||
172 | |||
173 | return $str; |
||
174 | } |
||
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) |
||
186 | { |
||
187 | $keys = (array) array_rand($this->items, $nb); |
||
188 | $result = array(); |
||
189 | foreach ($keys as $currentKey) { |
||
190 | $result[$currentKey] = $this->items[$currentKey]; |
||
191 | } |
||
192 | |||
193 | return $result; |
||
194 | } |
||
195 | |||
196 | // Helpers |
||
197 | public function first() |
||
198 | { |
||
199 | foreach ($this->items as $currentItem) { |
||
200 | return $currentItem; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | public function last() |
||
205 | { |
||
206 | if (count($this->items)) { |
||
207 | return end($this->items); |
||
208 | } else { |
||
209 | return null; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | 1 | public function isEmpty() |
|
217 | |||
218 | public function sum($field = null) |
||
219 | { |
||
220 | if ($field == null) { |
||
221 | 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) |
||
231 | { |
||
232 | if ($this->isEmpty()) { |
||
233 | return null; |
||
244 | |||
245 | public function shuffle() |
||
251 | |||
252 | public function unique() |
||
256 | |||
257 | public function each(\Closure $callback) |
||
262 | |||
263 | public function sort(\Closure $closure) |
||
269 | |||
270 | public function sortBy($field, $reverse = false) |
||
297 | |||
298 | public function filter(\Closure $closure) |
||
302 | |||
303 | public function search($value, $strict = false) |
||
307 | |||
308 | public function has($key) |
||
312 | |||
313 | public function keys() |
||
317 | |||
318 | public function prepend($item) |
||
324 | |||
325 | public function push($item) |
||
331 | |||
332 | public function put($key, $val) |
||
342 | |||
343 | public function pop() |
||
347 | |||
348 | public function reverse() |
||
352 | |||
353 | public function reduce(callable $callback, $initial = null) |
||
357 | |||
358 | public function slice($offset, $length = null, $preserveKeys = false) |
||
362 | |||
363 | public function take($limit = null) |
||
371 | |||
372 | public function splice($offset, $length = null, $replacement = array()) |
||
376 | |||
377 | public function chunk($size, $preserveKeys = false) |
||
386 | } |
||
387 |