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 |
||
13 | class Collection extends AbstractJsonDeserializeObject implements \Iterator, \JsonSerializable, \Countable, \ArrayAccess |
||
14 | { |
||
15 | const ID = 'id'; |
||
16 | use ContextTrait; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $type; |
||
22 | |||
23 | protected $pos = 0; |
||
24 | |||
25 | protected $keys = []; |
||
26 | |||
27 | protected $index = []; |
||
28 | |||
29 | /** |
||
30 | * @param array $data |
||
31 | * @param Context|callable $context |
||
32 | */ |
||
33 | 296 | final public function __construct(array $data = [], $context = null) |
|
38 | |||
39 | /** |
||
40 | * @return string |
||
41 | */ |
||
42 | 230 | public function getType() |
|
46 | |||
47 | /** |
||
48 | * @param string $type |
||
49 | * @internal |
||
50 | * @return $this |
||
51 | */ |
||
52 | 18 | public function setType($type) |
|
58 | |||
59 | 296 | protected function indexData() |
|
66 | |||
67 | /** |
||
68 | * @param array $rawData |
||
69 | * @internal |
||
70 | * @return $this |
||
71 | */ |
||
72 | 6 | public function setRawData(array $rawData) |
|
79 | |||
80 | /** |
||
81 | * @param string $indexName |
||
82 | * @param int $offset |
||
83 | * @param $key |
||
84 | */ |
||
85 | 259 | protected function addToIndex($indexName, $offset, $key) |
|
91 | |||
92 | 156 | protected function indexRow($offset, $row) |
|
93 | { |
||
94 | 156 | $id = null; |
|
95 | 156 | if ($row instanceof Resource) { |
|
96 | 1 | $id = $row->getId(); |
|
97 | 155 | } elseif (is_array($row)) { |
|
98 | 82 | $id = isset($row[static::ID]) ? $row[static::ID] : null; |
|
99 | } |
||
100 | 156 | $this->addToIndex(static::ID, $offset, $id); |
|
101 | 156 | } |
|
102 | |||
103 | /** |
||
104 | * @param $id |
||
105 | * @return static |
||
106 | */ |
||
107 | 24 | public function getById($id) |
|
111 | |||
112 | /** |
||
113 | * @param $indexName |
||
114 | * @param $key |
||
115 | * @return mixed|null |
||
116 | */ |
||
117 | 45 | public function getBy($indexName, $key) |
|
126 | |||
127 | 154 | public function add($object) |
|
133 | |||
134 | /** |
||
135 | * @param $offset |
||
136 | * @internal |
||
137 | */ |
||
138 | 139 | protected function initialize($offset) |
|
154 | |||
155 | /** |
||
156 | * @return array |
||
157 | */ |
||
158 | public function toArray() |
||
171 | |||
172 | /** |
||
173 | * @param $offset |
||
174 | * @return mixed |
||
175 | */ |
||
176 | 154 | public function getAt($offset) |
|
180 | |||
181 | /** |
||
182 | * @param $offset |
||
183 | * @param mixed $default |
||
184 | * @return array |
||
185 | */ |
||
186 | 138 | protected function getRaw($offset, $default = []) |
|
190 | |||
191 | /** |
||
192 | * @param $offset |
||
193 | * @param $object |
||
194 | * @return $this |
||
195 | */ |
||
196 | 158 | public function setAt($offset, $object) |
|
224 | |||
225 | /** |
||
226 | * (PHP 5 >= 5.1.0)<br/> |
||
227 | * Count elements of an object |
||
228 | * @link http://php.net/manual/en/countable.count.php |
||
229 | * @return int The custom count as an integer. |
||
230 | * </p> |
||
231 | * <p> |
||
232 | * The return value is cast to an integer. |
||
233 | */ |
||
234 | 81 | public function count() |
|
242 | |||
243 | |||
244 | /** |
||
245 | * (PHP 5 >= 5.0.0)<br/> |
||
246 | * Return the current element |
||
247 | * @link http://php.net/manual/en/iterator.current.php |
||
248 | * @return mixed Can return any type. |
||
249 | */ |
||
250 | 80 | public function current() |
|
257 | |||
258 | /** |
||
259 | * (PHP 5 >= 5.0.0)<br/> |
||
260 | * Move forward to next element |
||
261 | * @link http://php.net/manual/en/iterator.next.php |
||
262 | * @return void Any returned value is ignored. |
||
263 | */ |
||
264 | 18 | public function next() |
|
268 | |||
269 | /** |
||
270 | * (PHP 5 >= 5.0.0)<br/> |
||
271 | * Return the key of the current element |
||
272 | * @link http://php.net/manual/en/iterator.key.php |
||
273 | * @return mixed scalar on success, or null on failure. |
||
274 | */ |
||
275 | 4 | public function key() |
|
282 | |||
283 | /** |
||
284 | * (PHP 5 >= 5.0.0)<br/> |
||
285 | * Checks if current position is valid |
||
286 | * @link http://php.net/manual/en/iterator.valid.php |
||
287 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
288 | * Returns true on success or false on failure. |
||
289 | */ |
||
290 | 20 | public function valid() |
|
297 | |||
298 | /** |
||
299 | * (PHP 5 >= 5.0.0)<br/> |
||
300 | * Rewind the Iterator to the first element |
||
301 | * @link http://php.net/manual/en/iterator.rewind.php |
||
302 | * @return void Any returned value is ignored. |
||
303 | */ |
||
304 | 20 | public function rewind() |
|
308 | |||
309 | /** |
||
310 | * (PHP 5 >= 5.0.0)<br/> |
||
311 | * Whether a offset exists |
||
312 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
313 | * @param mixed $offset <p> |
||
314 | * An offset to check for. |
||
315 | * </p> |
||
316 | * @return boolean true on success or false on failure. |
||
317 | * </p> |
||
318 | * <p> |
||
319 | * The return value will be casted to boolean if non-boolean was returned. |
||
320 | */ |
||
321 | 23 | public function offsetExists($offset) |
|
325 | |||
326 | /** |
||
327 | * (PHP 5 >= 5.0.0)<br/> |
||
328 | * Offset to retrieve |
||
329 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
330 | * @param mixed $offset <p> |
||
331 | * The offset to retrieve. |
||
332 | * </p> |
||
333 | * @return mixed Can return all value types. |
||
334 | */ |
||
335 | 5 | public function offsetGet($offset) |
|
339 | |||
340 | /** |
||
341 | * (PHP 5 >= 5.0.0)<br/> |
||
342 | * Offset to set |
||
343 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
344 | * @param mixed $offset <p> |
||
345 | * The offset to assign the value to. |
||
346 | * </p> |
||
347 | * @param mixed $value <p> |
||
348 | * The value to set. |
||
349 | * </p> |
||
350 | * @return void |
||
351 | */ |
||
352 | 3 | public function offsetSet($offset, $value) |
|
356 | |||
357 | /** |
||
358 | * (PHP 5 >= 5.0.0)<br/> |
||
359 | * Offset to unset |
||
360 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
361 | * @param mixed $offset <p> |
||
362 | * The offset to unset. |
||
363 | * </p> |
||
364 | * @return void |
||
365 | */ |
||
366 | 1 | public function offsetUnset($offset) |
|
371 | } |
||
372 |