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 | 317 | final public function __construct(array $data = [], $context = null) |
|
34 | { |
||
35 | 317 | parent::__construct($data, $context); |
|
36 | 317 | $this->indexData(); |
|
37 | 317 | } |
|
38 | |||
39 | /** |
||
40 | * @return string |
||
41 | */ |
||
42 | 252 | public function getType() |
|
43 | { |
||
44 | 252 | return $this->type; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param string $type |
||
49 | * @internal |
||
50 | * @return $this |
||
51 | */ |
||
52 | 18 | public function setType($type) |
|
53 | { |
||
54 | 18 | $this->type = $type; |
|
55 | |||
56 | 18 | return $this; |
|
57 | } |
||
58 | |||
59 | 317 | protected function indexData() |
|
60 | { |
||
61 | 317 | $this->keys = array_keys($this->rawData); |
|
62 | 317 | foreach ($this->rawData as $offset => $row) { |
|
63 | 189 | $this->indexRow($offset, $row); |
|
64 | } |
||
65 | 317 | } |
|
66 | |||
67 | /** |
||
68 | * @param array $rawData |
||
69 | * @internal |
||
70 | * @return $this |
||
71 | */ |
||
72 | 6 | public function setRawData(array $rawData) |
|
73 | { |
||
74 | 6 | parent::setRawData($rawData); |
|
75 | 6 | $this->indexData(); |
|
76 | |||
77 | 6 | return $this; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $indexName |
||
82 | * @param int $offset |
||
83 | * @param $key |
||
84 | */ |
||
85 | 280 | protected function addToIndex($indexName, $offset, $key) |
|
91 | |||
92 | 170 | protected function indexRow($offset, $row) |
|
93 | { |
||
94 | 170 | $id = null; |
|
95 | 170 | if ($row instanceof Resource) { |
|
96 | 1 | $id = $row->getId(); |
|
97 | 169 | } elseif (is_array($row)) { |
|
98 | 95 | $id = isset($row[static::ID]) ? $row[static::ID] : null; |
|
99 | } |
||
100 | 170 | $this->addToIndex(static::ID, $offset, $id); |
|
101 | 170 | } |
|
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 | 46 | public function getBy($indexName, $key) |
|
126 | |||
127 | 174 | public function add($object) |
|
133 | |||
134 | /** |
||
135 | * @param $offset |
||
136 | * @internal |
||
137 | */ |
||
138 | 154 | protected function initialize($offset) |
|
154 | |||
155 | /** |
||
156 | * @return array |
||
157 | */ |
||
158 | 1 | public function toArray() |
|
171 | |||
172 | /** |
||
173 | * @param $offset |
||
174 | * @return mixed |
||
175 | */ |
||
176 | 170 | public function getAt($offset) |
|
180 | |||
181 | /** |
||
182 | * @param $offset |
||
183 | * @param mixed $default |
||
184 | * @return array |
||
185 | */ |
||
186 | 153 | protected function getRaw($offset, $default = []) |
|
190 | |||
191 | /** |
||
192 | * @param $offset |
||
193 | * @param $object |
||
194 | * @return $this |
||
195 | */ |
||
196 | 178 | public function setAt($offset, $object) |
|
197 | { |
||
198 | 178 | $type = $this->getType(); |
|
199 | 178 | if (!$this->isValidType($type, $object)) { |
|
200 | 1 | throw new \InvalidArgumentException(sprintf(Message::WRONG_TYPE, $offset, $type)); |
|
201 | } |
||
202 | |||
203 | 177 | if ($object instanceof ContextAwareInterface) { |
|
204 | 172 | $object->setContext($this->getContextCallback()); |
|
205 | } |
||
206 | 177 | if ($object instanceof ObjectTreeInterface) { |
|
207 | 172 | $object->parentSet($this); |
|
208 | 172 | $object->rootSet($this->rootGet()); |
|
209 | } |
||
210 | 177 | if (is_null($offset)) { |
|
211 | 175 | $this->typeData[] = $object; |
|
212 | 175 | $offset = count($this->typeData) - 1; |
|
213 | } else { |
||
214 | 3 | $this->typeData[$offset] = $object; |
|
215 | } |
||
216 | 177 | $this->initialized[$offset] = true; |
|
217 | 177 | if (!in_array($offset, $this->keys)) { |
|
218 | 177 | $this->keys[] = $offset; |
|
219 | } |
||
220 | 177 | $this->indexRow($offset, $object); |
|
221 | |||
222 | 177 | return $this; |
|
223 | } |
||
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 | 82 | 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 | 95 | public function current() |
|
251 | { |
||
252 | 95 | if (isset($this->keys[$this->pos])) { |
|
253 | 95 | return $this->getAt($this->keys[$this->pos]); |
|
254 | } |
||
255 | return null; |
||
256 | } |
||
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 | 26 | public function next() |
|
265 | { |
||
266 | 26 | $this->pos++; |
|
267 | 26 | } |
|
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() |
|
276 | { |
||
277 | 4 | if (isset($this->keys[$this->pos])) { |
|
278 | 4 | return $this->keys[$this->pos]; |
|
279 | } |
||
280 | return null; |
||
281 | } |
||
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 | 28 | public function valid() |
|
291 | { |
||
292 | 28 | if (isset($this->keys[$this->pos])) { |
|
293 | 26 | return $this->offsetExists($this->keys[$this->pos]); |
|
294 | } |
||
295 | 28 | return false; |
|
296 | } |
||
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 | 28 | 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 | 31 | 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 |