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 | const COLLECTION_TYPE = Collection::TYPE_LIST; |
||
1 ignored issue
–
show
|
|||
18 | const TYPE_LIST = 'List'; |
||
19 | const TYPE_MAP = 'Map'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $type; |
||
25 | |||
26 | protected $pos = 0; |
||
27 | |||
28 | protected $keys = []; |
||
29 | |||
30 | protected $index = []; |
||
31 | |||
32 | /** |
||
33 | * @param array $data |
||
34 | * @param Context|callable $context |
||
35 | */ |
||
36 | 474 | final public function __construct(array $data = [], $context = null) |
|
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | */ |
||
45 | 406 | public function getType() |
|
49 | |||
50 | /** |
||
51 | * @param string $type |
||
52 | * @internal |
||
53 | * @return $this |
||
54 | */ |
||
55 | 18 | public function setType($type) |
|
61 | |||
62 | 474 | protected function indexData() |
|
69 | |||
70 | /** |
||
71 | * @param array $rawData |
||
72 | * @internal |
||
73 | * @return $this |
||
74 | */ |
||
75 | 6 | public function setRawData(array $rawData) |
|
82 | |||
83 | /** |
||
84 | * @param string $indexName |
||
85 | * @param int $offset |
||
86 | * @param $key |
||
87 | */ |
||
88 | 435 | protected function addToIndex($indexName, $offset, $key) |
|
94 | |||
95 | 246 | protected function indexRow($offset, $row) |
|
105 | |||
106 | /** |
||
107 | * @param $id |
||
108 | * @return static |
||
109 | */ |
||
110 | 50 | public function getById($id) |
|
114 | |||
115 | /** |
||
116 | * @param $indexName |
||
117 | * @param $key |
||
118 | * @return mixed|null |
||
119 | */ |
||
120 | 81 | public function getBy($indexName, $key) |
|
129 | |||
130 | 290 | public function add($object) |
|
136 | |||
137 | /** |
||
138 | * @param $offset |
||
139 | * @internal |
||
140 | */ |
||
141 | 264 | protected function initialize($offset) |
|
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | 85 | public function toArray() |
|
174 | |||
175 | /** |
||
176 | * @param $offset |
||
177 | * @return mixed |
||
178 | */ |
||
179 | 284 | public function getAt($offset) |
|
183 | |||
184 | /** |
||
185 | * @param $offset |
||
186 | * @param mixed $default |
||
187 | * @return array |
||
188 | */ |
||
189 | 263 | protected function getRaw($offset, $default = []) |
|
193 | |||
194 | /** |
||
195 | * @param $offset |
||
196 | * @param $object |
||
197 | * @return $this |
||
198 | */ |
||
199 | 295 | public function setAt($offset, $object) |
|
227 | |||
228 | /** |
||
229 | * (PHP 5 >= 5.1.0)<br/> |
||
230 | * Count elements of an object |
||
231 | * @link http://php.net/manual/en/countable.count.php |
||
232 | * @return int The custom count as an integer. |
||
233 | * </p> |
||
234 | * <p> |
||
235 | * The return value is cast to an integer. |
||
236 | */ |
||
237 | 107 | public function count() |
|
245 | |||
246 | |||
247 | /** |
||
248 | * (PHP 5 >= 5.0.0)<br/> |
||
249 | * Return the current element |
||
250 | * @link http://php.net/manual/en/iterator.current.php |
||
251 | * @return mixed Can return any type. |
||
252 | */ |
||
253 | 177 | public function current() |
|
260 | |||
261 | /** |
||
262 | * (PHP 5 >= 5.0.0)<br/> |
||
263 | * Move forward to next element |
||
264 | * @link http://php.net/manual/en/iterator.next.php |
||
265 | * @return void Any returned value is ignored. |
||
266 | */ |
||
267 | 63 | public function next() |
|
271 | |||
272 | /** |
||
273 | * (PHP 5 >= 5.0.0)<br/> |
||
274 | * Return the key of the current element |
||
275 | * @link http://php.net/manual/en/iterator.key.php |
||
276 | * @return mixed scalar on success, or null on failure. |
||
277 | */ |
||
278 | 33 | public function key() |
|
285 | |||
286 | /** |
||
287 | * (PHP 5 >= 5.0.0)<br/> |
||
288 | * Checks if current position is valid |
||
289 | * @link http://php.net/manual/en/iterator.valid.php |
||
290 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
291 | * Returns true on success or false on failure. |
||
292 | */ |
||
293 | 65 | public function valid() |
|
300 | |||
301 | /** |
||
302 | * (PHP 5 >= 5.0.0)<br/> |
||
303 | * Rewind the Iterator to the first element |
||
304 | * @link http://php.net/manual/en/iterator.rewind.php |
||
305 | * @return void Any returned value is ignored. |
||
306 | */ |
||
307 | 67 | public function rewind() |
|
311 | |||
312 | /** |
||
313 | * (PHP 5 >= 5.0.0)<br/> |
||
314 | * Whether a offset exists |
||
315 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
316 | * @param mixed $offset <p> |
||
317 | * An offset to check for. |
||
318 | * </p> |
||
319 | * @return boolean true on success or false on failure. |
||
320 | * </p> |
||
321 | * <p> |
||
322 | * The return value will be casted to boolean if non-boolean was returned. |
||
323 | */ |
||
324 | 69 | public function offsetExists($offset) |
|
328 | |||
329 | /** |
||
330 | * (PHP 5 >= 5.0.0)<br/> |
||
331 | * Offset to retrieve |
||
332 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
333 | * @param mixed $offset <p> |
||
334 | * The offset to retrieve. |
||
335 | * </p> |
||
336 | * @return mixed Can return all value types. |
||
337 | */ |
||
338 | 5 | public function offsetGet($offset) |
|
342 | |||
343 | /** |
||
344 | * (PHP 5 >= 5.0.0)<br/> |
||
345 | * Offset to set |
||
346 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
347 | * @param mixed $offset <p> |
||
348 | * The offset to assign the value to. |
||
349 | * </p> |
||
350 | * @param mixed $value <p> |
||
351 | * The value to set. |
||
352 | * </p> |
||
353 | * @return void |
||
354 | */ |
||
355 | 4 | public function offsetSet($offset, $value) |
|
359 | |||
360 | /** |
||
361 | * (PHP 5 >= 5.0.0)<br/> |
||
362 | * Offset to unset |
||
363 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
364 | * @param mixed $offset <p> |
||
365 | * The offset to unset. |
||
366 | * </p> |
||
367 | * @return void |
||
368 | */ |
||
369 | 3 | public function offsetUnset($offset) |
|
378 | |||
379 | 236 | protected function toJson() |
|
389 | } |
||
390 |
This check looks for accesses to local static members using the fully qualified name instead of
self::
.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBC
could just as well be replaced byself::TRIPLEDES_CBC
. Referencing local members withself::
assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.