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 | 810 | 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 | 810 | 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 | 810 | 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 | 70 | public function &__get($key) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Standard set accessor. |
||
| 196 | * |
||
| 197 | * @param string $key |
||
| 198 | * @param mixed $value |
||
| 199 | * |
||
| 200 | * @throws InvalidArgumentException |
||
| 201 | */ |
||
| 202 | 255 | public function __set($key, $value) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Set value. |
||
| 212 | * |
||
| 213 | * @param string $key |
||
| 214 | * @param mixed $value |
||
| 215 | * |
||
| 216 | * @throws InvalidArgumentException |
||
| 217 | */ |
||
| 218 | 255 | private function setValue($key, $value) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Get the last response from the Stripe API. |
||
| 241 | * |
||
| 242 | * @return \Arcanedev\Stripe\Http\Response |
||
| 243 | */ |
||
| 244 | public function getLastResponse() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set the last response from the Stripe API. |
||
| 251 | * |
||
| 252 | * @param \Arcanedev\Stripe\Http\Response $response |
||
| 253 | * |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | 30 | public function setLastResponse(Response $response) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Check has a value by key. |
||
| 265 | * |
||
| 266 | * @param string $key |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 25 | public function __isset($key) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Unset element from values. |
||
| 277 | * |
||
| 278 | * @param string $key |
||
| 279 | */ |
||
| 280 | 20 | public function __unset($key) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Convert StripeObject to string. |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | 5 | public function __toString() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Json serialize. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | 5 | public function jsonSerialize() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Convert StripeObject to array. |
||
| 310 | * |
||
| 311 | * @param bool $recursive |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 20 | public function toArray($recursive = false) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Convert StripeObject to JSON. |
||
| 324 | * |
||
| 325 | * @param int $options |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 5 | public function toJson($options = 0) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Get only value keys. |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | 265 | public function keys() |
|
| 347 | |||
| 348 | /* ------------------------------------------------------------------------------------------------ |
||
| 349 | | ArrayAccess methods |
||
| 350 | | ------------------------------------------------------------------------------------------------ |
||
| 351 | */ |
||
| 352 | 15 | public function offsetSet($key, $value) |
|
| 356 | |||
| 357 | 40 | public function offsetExists($key) |
|
| 361 | |||
| 362 | 5 | public function offsetUnset($key) |
|
| 366 | |||
| 367 | 185 | public function offsetGet($key) |
|
| 373 | |||
| 374 | /* ------------------------------------------------------------------------------------------------ |
||
| 375 | | Main Functions |
||
| 376 | | ------------------------------------------------------------------------------------------------ |
||
| 377 | */ |
||
| 378 | /** |
||
| 379 | * This unfortunately needs to be public to be used in Util.php |
||
| 380 | * Return The object constructed from the given values. |
||
| 381 | * |
||
| 382 | * @param string $class |
||
| 383 | * @param array $values |
||
| 384 | * @param string $options |
||
| 385 | * |
||
| 386 | * @return self |
||
| 387 | */ |
||
| 388 | 35 | public static function scopedConstructFrom($class, $values, $options) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Refreshes this object using the provided values. |
||
| 399 | * |
||
| 400 | * @param array $values |
||
| 401 | * @param RequestOptions|array|string|null $opts |
||
| 402 | * @param boolean $partial |
||
| 403 | */ |
||
| 404 | 35 | public function refreshFrom($values, $opts, $partial = false) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Clean refreshed StripeObject. |
||
| 427 | * |
||
| 428 | * @param array $values |
||
| 429 | * @param bool|false $partial |
||
| 430 | */ |
||
| 431 | 35 | private function cleanObject($values, $partial) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Construct Value. |
||
| 450 | * |
||
| 451 | * @param string $key |
||
| 452 | * @param mixed $value |
||
| 453 | * @param array $opts |
||
| 454 | * |
||
| 455 | * @return self|StripeResource|Collection|array |
||
| 456 | */ |
||
| 457 | 35 | private function constructValue($key, $value, $opts) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Pretend to have late static bindings. |
||
| 466 | * |
||
| 467 | * @param string $method |
||
| 468 | * |
||
| 469 | * @return mixed |
||
| 470 | */ |
||
| 471 | 125 | protected function lsb($method) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Scoped Late Static Bindings. |
||
| 481 | * |
||
| 482 | * @param string $class |
||
| 483 | * @param string $method |
||
| 484 | * |
||
| 485 | * @return mixed |
||
| 486 | */ |
||
| 487 | protected static function scopedLsb($class, $method) |
||
| 493 | |||
| 494 | /* ------------------------------------------------------------------------------------------------ |
||
| 495 | | Check Functions |
||
| 496 | | ------------------------------------------------------------------------------------------------ |
||
| 497 | */ |
||
| 498 | /** |
||
| 499 | * Check if array has id. |
||
| 500 | * |
||
| 501 | * @param array $array |
||
| 502 | * |
||
| 503 | * @throws ApiException |
||
| 504 | */ |
||
| 505 | 15 | private function checkIdIsInArray($array) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Check if object has retrieve parameters. |
||
| 514 | * |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | 5 | public function hasRetrieveParams() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Check if attribute deletion. |
||
| 524 | * |
||
| 525 | * @param string $key |
||
| 526 | * @param mixed|null $value |
||
| 527 | * |
||
| 528 | * @throws InvalidArgumentException |
||
| 529 | */ |
||
| 530 | 255 | private function checkIfAttributeDeletion($key, $value) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Check metadata attribute. |
||
| 544 | * |
||
| 545 | * @param string $key |
||
| 546 | * @param mixed|null $value |
||
| 547 | * |
||
| 548 | * @throws InvalidArgumentException |
||
| 549 | */ |
||
| 550 | 255 | private function checkMetadataAttribute($key, $value) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Check permanent attributes. |
||
| 564 | * |
||
| 565 | * @param string $key |
||
| 566 | */ |
||
| 567 | 255 | private function checkPermanentAttributes($key) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Check unsaved attributes. |
||
| 576 | * |
||
| 577 | * @param array $supported |
||
| 578 | * |
||
| 579 | * @throws InvalidArgumentException |
||
| 580 | */ |
||
| 581 | 255 | private function checkUnsavedAttributes($supported) |
|
| 594 | |||
| 595 | /* ------------------------------------------------------------------------------------------------ |
||
| 596 | | Other Functions |
||
| 597 | | ------------------------------------------------------------------------------------------------ |
||
| 598 | */ |
||
| 599 | /** |
||
| 600 | * A recursive mapping of attributes to values for this object, |
||
| 601 | * including the proper value for deleted attributes. |
||
| 602 | * |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | 15 | protected function serializeParameters() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Serialize unsaved values. |
||
| 617 | * |
||
| 618 | * @param array $params |
||
| 619 | */ |
||
| 620 | 15 | private function serializeUnsavedValues(&$params) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Serialize nested updatable attributes. |
||
| 629 | * |
||
| 630 | * @param array $params |
||
| 631 | */ |
||
| 632 | 15 | private function serializeNestedUpdatableAttributes(&$params) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Show undefined property warning message. |
||
| 647 | * |
||
| 648 | * @param string $class |
||
| 649 | * @param string $key |
||
| 650 | */ |
||
| 651 | 10 | private function showUndefinedPropertyMsg($class, $key) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Show available attributes for undefined property warning message. |
||
| 669 | * |
||
| 670 | * @return string |
||
| 671 | */ |
||
| 672 | 5 | private function showUndefinedPropertyMsgAttributes() |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Check not found attributes exception. |
||
| 681 | * |
||
| 682 | * @param array $notFound |
||
| 683 | * |
||
| 684 | * @throws InvalidArgumentException |
||
| 685 | */ |
||
| 686 | private function checkNotFoundAttributesException($notFound) |
||
| 694 | } |
||
| 695 |