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( |
||
| 162 | |||
| 163 | /******************************************* |
||
| 164 | * READ BY ID |
||
| 165 | *******************************************/ |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param ConnectionInterface|null $connection |
||
| 169 | * @param CacheInterface|null $cache |
||
| 170 | * @param string $identifier |
||
| 171 | * @param LoggerInterface|null $logger |
||
| 172 | * @param array $config |
||
| 173 | * @return ResponseInterface |
||
| 174 | */ |
||
| 175 | public static function readById( |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param ConnectionInterface|null $connection |
||
| 193 | * @param CacheInterface|null $cache |
||
| 194 | * @param string $identifier |
||
| 195 | * @param LoggerInterface|null $logger |
||
| 196 | * @param array $config |
||
| 197 | * @return callable |
||
| 198 | */ |
||
| 199 | public static function readByIdRelay( |
||
| 217 | |||
| 218 | /******************************************* |
||
| 219 | * READ BY EMAIL |
||
| 220 | *******************************************/ |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param ConnectionInterface|null $connection |
||
| 224 | * @param CacheInterface|null $cache |
||
| 225 | * @param string $identifier |
||
| 226 | * @param LoggerInterface|null $logger |
||
| 227 | * @param array $config |
||
| 228 | * @return ResponseInterface |
||
| 229 | */ |
||
| 230 | public static function readByEmail( |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param ConnectionInterface|null $connection |
||
| 248 | * @param CacheInterface|null $cache |
||
| 249 | * @param string $identifier |
||
| 250 | * @param LoggerInterface|null $logger |
||
| 251 | * @param array $config |
||
| 252 | * @return callable |
||
| 253 | */ |
||
| 254 | public static function readByEmailRelay( |
||
| 272 | |||
| 273 | /******************************************* |
||
| 274 | * READ BY TOKEN |
||
| 275 | *******************************************/ |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param ConnectionInterface|null $connection |
||
| 279 | * @param CacheInterface|null $cache |
||
| 280 | * @param string $identifier |
||
| 281 | * @param LoggerInterface|null $logger |
||
| 282 | * @param array $config |
||
| 283 | * @return ResponseInterface |
||
| 284 | */ |
||
| 285 | public static function readByToken( |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param ConnectionInterface|null $connection |
||
| 303 | * @param CacheInterface|null $cache |
||
| 304 | * @param string $identifier |
||
| 305 | * @param LoggerInterface|null $logger |
||
| 306 | * @param array $config |
||
| 307 | * @return callable |
||
| 308 | */ |
||
| 309 | public static function readByTokenRelay( |
||
| 327 | |||
| 328 | /******************************************* |
||
| 329 | * UPDATE |
||
| 330 | *******************************************/ |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $identifier |
||
| 334 | * @param array $payload |
||
| 335 | * @param ConnectionInterface|null $connection |
||
| 336 | * @param CacheInterface|null $cache |
||
| 337 | * @param LoggerInterface|null $logger |
||
| 338 | * @param array $config |
||
| 339 | * @return ResponseInterface |
||
| 340 | */ |
||
| 341 | public static function update( |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $identifier |
||
| 361 | * @param array $payload |
||
| 362 | * @param ConnectionInterface|null $connection |
||
| 363 | * @param CacheInterface|null $cache |
||
| 364 | * @param LoggerInterface|null $logger |
||
| 365 | * @param array $config |
||
| 366 | * @return callable |
||
| 367 | */ |
||
| 368 | public static function updateRelay( |
||
| 388 | |||
| 389 | |||
| 390 | /******************************************* |
||
| 391 | * UPSERT |
||
| 392 | *******************************************/ |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param ConnectionInterface|null $connection |
||
| 396 | * @param CacheInterface|null $cache |
||
| 397 | * @param array $payload |
||
| 398 | * @param string|null $identifier |
||
| 399 | * @param LoggerInterface|null $logger |
||
| 400 | * @param array $config |
||
| 401 | * @return ResponseInterface |
||
| 402 | */ |
||
| 403 | public static function upsert( |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param ConnectionInterface|null $connection |
||
| 423 | * @param CacheInterface|null $cache |
||
| 424 | * @param array $payload |
||
| 425 | * @param string|null $identifier |
||
| 426 | * @param LoggerInterface|null $logger |
||
| 427 | * @param array $config |
||
| 428 | * @return callable |
||
| 429 | */ |
||
| 430 | public static function upsertRelay( |
||
| 457 | |||
| 458 | |||
| 459 | /******************************************* |
||
| 460 | * DELETE |
||
| 461 | *******************************************/ |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $identifier |
||
| 465 | * @param ConnectionInterface|null $connection |
||
| 466 | * @param CacheInterface|null $cache |
||
| 467 | * @param LoggerInterface $logger |
||
| 468 | * @param array $config |
||
| 469 | * @return ResponseInterface |
||
| 470 | */ |
||
| 471 | public static function delete( |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param string $identifier |
||
| 489 | * @param ConnectionInterface|null $connection |
||
| 490 | * @param CacheInterface|null $cache |
||
| 491 | * @param LoggerInterface $logger |
||
| 492 | * @param array $config |
||
| 493 | * @return callable |
||
| 494 | */ |
||
| 495 | public static function deleteRelay( |
||
| 513 | |||
| 514 | |||
| 515 | /******************************************* |
||
| 516 | * BATCH |
||
| 517 | *******************************************/ |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param ConnectionInterface $connection |
||
| 521 | * @param array $payload |
||
| 522 | * @param LoggerInterface $logger |
||
| 523 | * @param array $config |
||
| 524 | * @return ResponseInterface |
||
| 525 | */ |
||
| 526 | public static function batch( |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param ConnectionInterface $connection |
||
| 542 | * @param array $payload |
||
| 543 | * @param LoggerInterface $logger |
||
| 544 | * @param array $config |
||
| 545 | * @return callable |
||
| 546 | */ |
||
| 547 | public static function batchRelay( |
||
| 563 | } |
||
| 564 |