Complex classes like StripeObject 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 StripeObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Arcanedev\Stripe; |
||
| 24 | class StripeObject implements ObjectInterface, ArrayAccess, JsonSerializable, Arrayable, Jsonable |
||
| 25 | { |
||
| 26 | /* ------------------------------------------------------------------------------------------------ |
||
| 27 | | Constants |
||
| 28 | | ------------------------------------------------------------------------------------------------ |
||
| 29 | */ |
||
| 30 | const ATTACHED_OBJECT_CLASS = 'Arcanedev\\Stripe\\AttachedObject'; |
||
| 31 | |||
| 32 | /* ------------------------------------------------------------------------------------------------ |
||
| 33 | | Properties |
||
| 34 | | ------------------------------------------------------------------------------------------------ |
||
| 35 | */ |
||
| 36 | /** |
||
| 37 | * @var RequestOptions|string|array |
||
| 38 | */ |
||
| 39 | protected $opts; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | protected $values; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Unsaved Values. |
||
| 46 | * |
||
| 47 | * @var UtilSet |
||
| 48 | */ |
||
| 49 | protected $unsavedValues; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Transient (Deleted) Values. |
||
| 53 | * |
||
| 54 | * @var UtilSet |
||
| 55 | */ |
||
| 56 | protected $transientValues; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Retrieve parameters used to query the object. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $retrieveParameters; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Attributes that should not be sent to the API because |
||
| 67 | * they're not updatable (e.g. API key, ID). |
||
| 68 | * |
||
| 69 | * @var UtilSet |
||
| 70 | */ |
||
| 71 | public static $permanentAttributes; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Attributes that are nested but still updatable from the |
||
| 75 | * parent class's URL (e.g. metadata). |
||
| 76 | * |
||
| 77 | * @var UtilSet |
||
| 78 | */ |
||
| 79 | public static $nestedUpdatableAttributes; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Allow to check attributes while setting. |
||
| 83 | * |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $checkUnsavedAttributes = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The last response. |
||
| 90 | * |
||
| 91 | * @var \Arcanedev\Stripe\Http\Response |
||
| 92 | */ |
||
| 93 | protected $lastResponse; |
||
| 94 | |||
| 95 | /* ------------------------------------------------------------------------------------------------ |
||
| 96 | | Constructor |
||
| 97 | | ------------------------------------------------------------------------------------------------ |
||
| 98 | */ |
||
| 99 | /** |
||
| 100 | * Make a Stripe object instance. |
||
| 101 | * |
||
| 102 | * @param string|null $id |
||
| 103 | * @param string|array|null $options |
||
| 104 | */ |
||
| 105 | 920 | public function __construct($id = null, $options = null) |
|
| 122 | |||
| 123 | /* ------------------------------------------------------------------------------------------------ |
||
| 124 | | Getters & Setters (+Magics) |
||
| 125 | | ------------------------------------------------------------------------------------------------ |
||
| 126 | */ |
||
| 127 | /** |
||
| 128 | * Set the Id. |
||
| 129 | * |
||
| 130 | * @param array|string|null $id |
||
| 131 | * |
||
| 132 | * @throws ApiException |
||
| 133 | * |
||
| 134 | * @return self |
||
| 135 | */ |
||
| 136 | 920 | private function setId($id) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Set the Id from Array. |
||
| 149 | * |
||
| 150 | * @param array|string|null $id |
||
| 151 | * |
||
| 152 | * @throws ApiException |
||
| 153 | */ |
||
| 154 | 920 | private function setIdIfArray(&$id) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get Retrieve Parameters. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 5 | protected function getRetrieveParams() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Standard get accessor. |
||
| 176 | * |
||
| 177 | * @param string|int $key |
||
| 178 | * |
||
| 179 | * @return mixed|null |
||
| 180 | */ |
||
| 181 | 638 | public function &__get($key) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Standard set accessor. |
||
| 194 | * |
||
| 195 | * @param string $key |
||
| 196 | * @param mixed $value |
||
| 197 | * |
||
| 198 | * @throws InvalidArgumentException |
||
| 199 | */ |
||
| 200 | 768 | public function __set($key, $value) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Set value. |
||
| 210 | * |
||
| 211 | * @param string $key |
||
| 212 | * @param mixed $value |
||
| 213 | * |
||
| 214 | * @throws InvalidArgumentException |
||
| 215 | */ |
||
| 216 | 768 | private function setValue($key, $value) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Get the last response from the Stripe API. |
||
| 238 | * |
||
| 239 | * @return \Arcanedev\Stripe\Http\Response |
||
| 240 | */ |
||
| 241 | 15 | public function getLastResponse() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Set the last response from the Stripe API. |
||
| 248 | * |
||
| 249 | * @param \Arcanedev\Stripe\Http\Response $response |
||
| 250 | * |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | 638 | public function setLastResponse(Response $response) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Check has a value by key. |
||
| 262 | * |
||
| 263 | * @param string $key |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | 180 | public function __isset($key) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Unset element from values. |
||
| 274 | * |
||
| 275 | * @param string $key |
||
| 276 | */ |
||
| 277 | 60 | public function __unset($key) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Convert StripeObject to string. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 5 | public function __toString() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Json serialize. |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | 5 | public function jsonSerialize() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Convert StripeObject to array. |
||
| 307 | * |
||
| 308 | * @param bool $recursive |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 25 | public function toArray($recursive = false) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Convert StripeObject to JSON. |
||
| 321 | * |
||
| 322 | * @param int $options |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 5 | public function toJson($options = 0) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get only value keys. |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | 788 | public function keys() |
|
| 344 | |||
| 345 | /* ------------------------------------------------------------------------------------------------ |
||
| 346 | | ArrayAccess methods |
||
| 347 | | ------------------------------------------------------------------------------------------------ |
||
| 348 | */ |
||
| 349 | 65 | public function offsetSet($key, $value) |
|
| 353 | |||
| 354 | 633 | public function offsetExists($key) |
|
| 358 | |||
| 359 | 5 | public function offsetUnset($key) |
|
| 363 | |||
| 364 | 500 | public function offsetGet($key) |
|
| 370 | |||
| 371 | /* ------------------------------------------------------------------------------------------------ |
||
| 372 | | Main Functions |
||
| 373 | | ------------------------------------------------------------------------------------------------ |
||
| 374 | */ |
||
| 375 | /** |
||
| 376 | * This unfortunately needs to be public to be used in Util.php |
||
| 377 | * Return The object constructed from the given values. |
||
| 378 | * |
||
| 379 | * @param string $class |
||
| 380 | * @param array $values |
||
| 381 | * @param string $options |
||
| 382 | * |
||
| 383 | * @return self |
||
| 384 | */ |
||
| 385 | 643 | public static function scopedConstructFrom($class, $values, $options) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Refreshes this object using the provided values. |
||
| 396 | * |
||
| 397 | * @param array $values |
||
| 398 | * @param RequestOptions|array|string|null $opts |
||
| 399 | * @param boolean $partial |
||
| 400 | */ |
||
| 401 | 643 | public function refreshFrom($values, $opts, $partial = false) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Clean refreshed StripeObject. |
||
| 424 | * |
||
| 425 | * @param array $values |
||
| 426 | * @param bool|false $partial |
||
| 427 | */ |
||
| 428 | 643 | private function cleanObject($values, $partial) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Construct Value. |
||
| 447 | * |
||
| 448 | * @param string $key |
||
| 449 | * @param mixed $value |
||
| 450 | * @param array $opts |
||
| 451 | * |
||
| 452 | * @return self|StripeResource|Collection|array |
||
| 453 | */ |
||
| 454 | 643 | private function constructValue($key, $value, $opts) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Pretend to have late static bindings. |
||
| 463 | * |
||
| 464 | * @param string $method |
||
| 465 | * |
||
| 466 | * @return mixed |
||
| 467 | */ |
||
| 468 | 400 | protected function lsb($method) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Scoped Late Static Bindings. |
||
| 478 | * |
||
| 479 | * @param string $class |
||
| 480 | * @param string $method |
||
| 481 | * |
||
| 482 | * @return mixed |
||
| 483 | */ |
||
| 484 | protected static function scopedLsb($class, $method) |
||
| 490 | |||
| 491 | /* ------------------------------------------------------------------------------------------------ |
||
| 492 | | Check Functions |
||
| 493 | | ------------------------------------------------------------------------------------------------ |
||
| 494 | */ |
||
| 495 | /** |
||
| 496 | * Check if array has id. |
||
| 497 | * |
||
| 498 | * @param array $array |
||
| 499 | * |
||
| 500 | * @throws ApiException |
||
| 501 | */ |
||
| 502 | 15 | private function checkIdIsInArray($array) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Check if object has retrieve parameters. |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | 5 | public function hasRetrieveParams() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Check if attribute deletion. |
||
| 521 | * |
||
| 522 | * @param string $key |
||
| 523 | * @param mixed|null $value |
||
| 524 | * |
||
| 525 | * @throws InvalidArgumentException |
||
| 526 | */ |
||
| 527 | 768 | private function checkIfAttributeDeletion($key, $value) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Check metadata attribute. |
||
| 541 | * |
||
| 542 | * @param string $key |
||
| 543 | * @param mixed|null $value |
||
| 544 | * |
||
| 545 | * @throws InvalidArgumentException |
||
| 546 | */ |
||
| 547 | 768 | private function checkMetadataAttribute($key, $value) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Check permanent attributes. |
||
| 561 | * |
||
| 562 | * @param string $key |
||
| 563 | */ |
||
| 564 | 768 | private function checkPermanentAttributes($key) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Check unsaved attributes. |
||
| 573 | * |
||
| 574 | * @param array $supported |
||
| 575 | * |
||
| 576 | * @throws InvalidArgumentException |
||
| 577 | */ |
||
| 578 | 768 | private function checkUnsavedAttributes($supported) |
|
| 591 | |||
| 592 | /* ------------------------------------------------------------------------------------------------ |
||
| 593 | | Other Functions |
||
| 594 | | ------------------------------------------------------------------------------------------------ |
||
| 595 | */ |
||
| 596 | /** |
||
| 597 | * A recursive mapping of attributes to values for this object, |
||
| 598 | * including the proper value for deleted attributes. |
||
| 599 | * |
||
| 600 | * @return array |
||
| 601 | */ |
||
| 602 | 165 | protected function serializeParameters() |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Serialize unsaved values. |
||
| 614 | * |
||
| 615 | * @param array $params |
||
| 616 | */ |
||
| 617 | 165 | private function serializeUnsavedValues(&$params) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Serialize nested updatable attributes. |
||
| 626 | * |
||
| 627 | * @param array $params |
||
| 628 | */ |
||
| 629 | 165 | private function serializeNestedUpdatableAttributes(&$params) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Show undefined property warning message. |
||
| 644 | * |
||
| 645 | * @param string $class |
||
| 646 | * @param string $key |
||
| 647 | */ |
||
| 648 | 10 | private function showUndefinedPropertyMsg($class, $key) |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Show available attributes for undefined property warning message. |
||
| 666 | * |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | 5 | private function showUndefinedPropertyMsgAttributes() |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Check not found attributes exception. |
||
| 678 | * |
||
| 679 | * @param array $notFound |
||
| 680 | * |
||
| 681 | * @throws InvalidArgumentException |
||
| 682 | */ |
||
| 683 | 40 | private function checkNotFoundAttributesException($notFound) |
|
| 691 | } |
||
| 692 |