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 |
||
29 | class Collection implements \IteratorAggregate, \Countable, \ArrayAccess |
||
30 | { |
||
31 | /** |
||
32 | * This is the internal items container |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $items = []; |
||
36 | |||
37 | /** |
||
38 | * Set this property to the class name to set the item class policy |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $itemsInstanceOf = ''; |
||
42 | |||
43 | /** |
||
44 | * When this is TRUE an exception is thrown when access a member that does not exists |
||
45 | * When is FALSE then it only returns NULL |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $throwExceptionItemNotFound = true; |
||
49 | |||
50 | /* |
||
51 | * |
||
52 | * Real methods |
||
53 | * |
||
54 | */ |
||
55 | |||
56 | /** |
||
57 | * Remove all elements from the collection |
||
58 | */ |
||
59 | 11 | public function clear() |
|
63 | |||
64 | /** |
||
65 | * Count of elements |
||
66 | * @return int |
||
67 | */ |
||
68 | 9 | public function count() |
|
72 | |||
73 | /** |
||
74 | * Return true if the element exists (even when the content is null) |
||
75 | * @param string|int $key |
||
76 | * @return bool |
||
77 | */ |
||
78 | 11 | public function exists($key) |
|
82 | |||
83 | /** |
||
84 | * Throw a LogicException if an item does not follow the ItemsClass policy |
||
85 | * @param mixed $item |
||
86 | */ |
||
87 | 13 | protected function checkItemInstanceOf($item) |
|
93 | |||
94 | /** |
||
95 | * @param mixed $item |
||
96 | * @param string|int $key |
||
97 | * @return $this |
||
98 | */ |
||
99 | 13 | public function addItem($item, $key = null) |
|
109 | |||
110 | public function addItems($items) |
||
116 | |||
117 | /** |
||
118 | * @param string|int $key |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function remove($key) |
||
128 | |||
129 | /** |
||
130 | * An array only with the keys |
||
131 | * @return array |
||
132 | */ |
||
133 | public function keys() |
||
137 | |||
138 | /** |
||
139 | * An array only with the values (no keys) |
||
140 | * @return array |
||
141 | */ |
||
142 | public function values() |
||
146 | |||
147 | /** |
||
148 | * An element from the collection if found, if not found it will: |
||
149 | * If throwExceptionItemNotFound is true then it will create an exception |
||
150 | * Otherwise will return null |
||
151 | * |
||
152 | * @param mixed $key |
||
153 | * @return mixed |
||
154 | * @throws \OutOfRangeException If protected property $throwExceptionItemNotFound is true |
||
155 | */ |
||
156 | 10 | public function value($key) |
|
166 | |||
167 | /** |
||
168 | * The full array of key-values |
||
169 | * @return array |
||
170 | */ |
||
171 | public function keyValues() |
||
175 | |||
176 | /** |
||
177 | * Implementation of IteratorAggregate |
||
178 | * @return \ArrayIterator |
||
179 | */ |
||
180 | 10 | public function getIterator() |
|
184 | |||
185 | /** |
||
186 | * Check if an item compliance with the ItemsClass policy |
||
187 | * @param mixed $item |
||
188 | * @return bool |
||
189 | */ |
||
190 | 13 | protected function isItemInstanceOf($item) |
|
203 | |||
204 | /** |
||
205 | * Set the items class policy, if elements already exists each one is checked |
||
206 | * @param string $classname |
||
207 | */ |
||
208 | public function setItemsClassPolicy($classname) |
||
225 | |||
226 | /** |
||
227 | * Get the items class policy, if empty string then no policy is set |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getItemsClassPolicy() |
||
234 | |||
235 | /* |
||
236 | * |
||
237 | * Magic methods |
||
238 | * |
||
239 | */ |
||
240 | |||
241 | public function __isset($key) |
||
245 | |||
246 | public function __get($key) |
||
250 | |||
251 | public function __set($key, $value) |
||
255 | |||
256 | public function __unset($key) |
||
260 | |||
261 | public function __invoke($key) |
||
265 | |||
266 | 5 | public function __clone() |
|
274 | |||
275 | /* |
||
276 | * |
||
277 | * ArrayAccess methods |
||
278 | * |
||
279 | */ |
||
280 | public function offsetExists($offset) |
||
284 | |||
285 | public function offsetGet($offset) |
||
289 | |||
290 | public function offsetSet($offset, $value) |
||
294 | |||
295 | public function offsetUnset($offset) |
||
299 | } |
||
300 |