Complex classes like Contact 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 Contact, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Contact |
||
29 | { |
||
30 | /** |
||
31 | * @param string|null $identifier |
||
32 | * @return bool |
||
33 | */ |
||
34 | protected static function upsertHasId(string $identifier = null): bool |
||
38 | |||
39 | /******************************************* |
||
40 | * CREATE |
||
41 | *******************************************/ |
||
42 | |||
43 | /** |
||
44 | * @param array $payload |
||
45 | * @param ConnectionInterface|null $connection |
||
46 | * @param LoggerInterface $logger |
||
47 | * @param array $config |
||
48 | * @return ResponseInterface |
||
49 | */ |
||
50 | public static function create( |
||
63 | |||
64 | /** |
||
65 | * @param array $payload |
||
66 | * @param ConnectionInterface|null $connection |
||
67 | * @param LoggerInterface $logger |
||
68 | * @param array $config |
||
69 | * @return callable |
||
70 | */ |
||
71 | public static function createRelay( |
||
87 | |||
88 | |||
89 | /******************************************* |
||
90 | * READ |
||
91 | *******************************************/ |
||
92 | |||
93 | /** |
||
94 | * @param ConnectionInterface|null $connection |
||
95 | * @param CacheInterface|null $cache |
||
96 | * @param string $identifier |
||
97 | * @param LoggerInterface|null $logger |
||
98 | * @param array $config |
||
99 | * @return ResponseInterface |
||
100 | */ |
||
101 | public static function read( |
||
116 | |||
117 | /** |
||
118 | * @param ConnectionInterface|null $connection |
||
119 | * @param CacheInterface|null $cache |
||
120 | * @param string $identifier |
||
121 | * @param LoggerInterface|null $logger |
||
122 | * @param array $config |
||
123 | * @return callable |
||
124 | */ |
||
125 | public static function readRelay( |
||
164 | |||
165 | /******************************************* |
||
166 | * READ BY ID |
||
167 | *******************************************/ |
||
168 | |||
169 | /** |
||
170 | * @param ConnectionInterface|null $connection |
||
171 | * @param CacheInterface|null $cache |
||
172 | * @param string $identifier |
||
173 | * @param LoggerInterface|null $logger |
||
174 | * @param array $config |
||
175 | * @return ResponseInterface |
||
176 | */ |
||
177 | public static function readById( |
||
192 | |||
193 | /** |
||
194 | * @param ConnectionInterface|null $connection |
||
195 | * @param CacheInterface|null $cache |
||
196 | * @param string $identifier |
||
197 | * @param LoggerInterface|null $logger |
||
198 | * @param array $config |
||
199 | * @return callable |
||
200 | */ |
||
201 | public static function readByIdRelay( |
||
219 | |||
220 | /******************************************* |
||
221 | * READ BY EMAIL |
||
222 | *******************************************/ |
||
223 | |||
224 | /** |
||
225 | * @param ConnectionInterface|null $connection |
||
226 | * @param CacheInterface|null $cache |
||
227 | * @param string $identifier |
||
228 | * @param LoggerInterface|null $logger |
||
229 | * @param array $config |
||
230 | * @return ResponseInterface |
||
231 | */ |
||
232 | public static function readByEmail( |
||
247 | |||
248 | /** |
||
249 | * @param ConnectionInterface|null $connection |
||
250 | * @param CacheInterface|null $cache |
||
251 | * @param string $identifier |
||
252 | * @param LoggerInterface|null $logger |
||
253 | * @param array $config |
||
254 | * @return callable |
||
255 | */ |
||
256 | public static function readByEmailRelay( |
||
274 | |||
275 | /******************************************* |
||
276 | * READ BY TOKEN |
||
277 | *******************************************/ |
||
278 | |||
279 | /** |
||
280 | * @param ConnectionInterface|null $connection |
||
281 | * @param CacheInterface|null $cache |
||
282 | * @param string $identifier |
||
283 | * @param LoggerInterface|null $logger |
||
284 | * @param array $config |
||
285 | * @return ResponseInterface |
||
286 | */ |
||
287 | public static function readByToken( |
||
302 | |||
303 | /** |
||
304 | * @param ConnectionInterface|null $connection |
||
305 | * @param CacheInterface|null $cache |
||
306 | * @param string $identifier |
||
307 | * @param LoggerInterface|null $logger |
||
308 | * @param array $config |
||
309 | * @return callable |
||
310 | */ |
||
311 | public static function readByTokenRelay( |
||
329 | |||
330 | /******************************************* |
||
331 | * UPDATE |
||
332 | *******************************************/ |
||
333 | |||
334 | /** |
||
335 | * @param string $identifier |
||
336 | * @param array $payload |
||
337 | * @param ConnectionInterface|null $connection |
||
338 | * @param CacheInterface|null $cache |
||
339 | * @param LoggerInterface|null $logger |
||
340 | * @param array $config |
||
341 | * @return ResponseInterface |
||
342 | */ |
||
343 | public static function update( |
||
360 | |||
361 | /** |
||
362 | * @param string $identifier |
||
363 | * @param array $payload |
||
364 | * @param ConnectionInterface|null $connection |
||
365 | * @param CacheInterface|null $cache |
||
366 | * @param LoggerInterface|null $logger |
||
367 | * @param array $config |
||
368 | * @return callable |
||
369 | */ |
||
370 | public static function updateRelay( |
||
390 | |||
391 | |||
392 | /******************************************* |
||
393 | * UPSERT |
||
394 | *******************************************/ |
||
395 | |||
396 | /** |
||
397 | * @param ConnectionInterface|null $connection |
||
398 | * @param CacheInterface|null $cache |
||
399 | * @param array $payload |
||
400 | * @param string|null $identifier |
||
401 | * @param LoggerInterface|null $logger |
||
402 | * @param array $config |
||
403 | * @return ResponseInterface |
||
404 | */ |
||
405 | public static function upsert( |
||
422 | |||
423 | /** |
||
424 | * @param ConnectionInterface|null $connection |
||
425 | * @param CacheInterface|null $cache |
||
426 | * @param array $payload |
||
427 | * @param string|null $identifier |
||
428 | * @param LoggerInterface|null $logger |
||
429 | * @param array $config |
||
430 | * @return callable |
||
431 | */ |
||
432 | public static function upsertRelay( |
||
459 | |||
460 | |||
461 | /******************************************* |
||
462 | * DELETE |
||
463 | *******************************************/ |
||
464 | |||
465 | /** |
||
466 | * @param string $identifier |
||
467 | * @param ConnectionInterface|null $connection |
||
468 | * @param CacheInterface|null $cache |
||
469 | * @param LoggerInterface $logger |
||
470 | * @param array $config |
||
471 | * @return ResponseInterface |
||
472 | */ |
||
473 | public static function delete( |
||
488 | |||
489 | /** |
||
490 | * @param string $identifier |
||
491 | * @param ConnectionInterface|null $connection |
||
492 | * @param CacheInterface|null $cache |
||
493 | * @param LoggerInterface $logger |
||
494 | * @param array $config |
||
495 | * @return callable |
||
496 | */ |
||
497 | public static function deleteRelay( |
||
515 | |||
516 | |||
517 | /******************************************* |
||
518 | * BATCH |
||
519 | *******************************************/ |
||
520 | |||
521 | /** |
||
522 | * @param ConnectionInterface $connection |
||
523 | * @param array $payload |
||
524 | * @param LoggerInterface $logger |
||
525 | * @param array $config |
||
526 | * @return ResponseInterface |
||
527 | */ |
||
528 | public static function batch( |
||
541 | |||
542 | /** |
||
543 | * @param ConnectionInterface $connection |
||
544 | * @param array $payload |
||
545 | * @param LoggerInterface $logger |
||
546 | * @param array $config |
||
547 | * @return callable |
||
548 | */ |
||
549 | public static function batchRelay( |
||
565 | } |
||
566 |