Complex classes like AbstractAuthorizationContainer 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 AbstractAuthorizationContainer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class AbstractAuthorizationContainer extends AbstractRequestContainer implements AuthorizationContainerInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Sub account ID |
||
| 19 | * |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $aid; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $clearingtype; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $clearingsubtype; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Merchant reference number for the payment process. (Permitted symbols: 0-9, a-z, A-Z, .,-,_,/) |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $reference; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Total amount (in smallest currency unit! e.g. cent) |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $amount; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Currency (ISO-4217) |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $currency; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Individual parameter |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $param; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * dynamic text for debit and creditcard payments |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $narrative_text; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer |
||
| 71 | */ |
||
| 72 | protected $personalData; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ShippingContainer |
||
| 76 | */ |
||
| 77 | protected $shippingData; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PaymentMethod\AbstractPaymentMethodContainer |
||
| 81 | */ |
||
| 82 | protected $paymentMethod; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ThreeDSecureContainer |
||
| 86 | */ |
||
| 87 | protected $_3dsecure; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\Invoicing\TransactionContainer |
||
| 91 | */ |
||
| 92 | protected $invoicing; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $onlinebanktransfertype; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $bankcountry; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $businessrelation; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string|null |
||
| 111 | */ |
||
| 112 | public function getBusinessrelation(): ?string |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $businessrelation |
||
| 119 | */ |
||
| 120 | public function setBusinessrelation(string $businessrelation): void |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $it; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | protected $id; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | protected $pr; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | protected $no; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $de; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | protected $va; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | protected $email; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param int $aid |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | public function setAid($aid) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | public function getAid() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param int $amount |
||
| 180 | * |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | public function setAmount($amount) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function getAmount() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $clearingType |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function setClearingType($clearingType) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getClearingType() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $currency |
||
| 216 | * |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function setCurrency($currency) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getCurrency() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $narrative_text |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | public function setNarrativeText($narrative_text) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function getNarrativeText() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $param |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | public function setParam($param) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getParam() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $reference |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public function setReference($reference) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getReference() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer $personalData |
||
| 288 | * |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | public function setPersonalData(PersonalContainer $personalData) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\PersonalContainer |
||
| 298 | */ |
||
| 299 | public function getPersonalData() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ShippingContainer $delivery |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function setShippingData(ShippingContainer $delivery) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ShippingContainer |
||
| 316 | */ |
||
| 317 | public function getShippingData() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param PaymentMethod\AbstractPaymentMethodContainer $paymentMethod |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | public function setPaymentMethod(AbstractPaymentMethodContainer $paymentMethod) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return PaymentMethod\AbstractPaymentMethodContainer |
||
| 334 | */ |
||
| 335 | public function getPaymentMethod() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ThreeDSecureContainer $secure |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | public function set3dsecure(ThreeDSecureContainer $secure) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Authorization\ThreeDSecureContainer |
||
| 352 | */ |
||
| 353 | public function get3dsecure() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\Invoicing\TransactionContainer $invoicing |
||
| 360 | * |
||
| 361 | * @return void |
||
| 362 | */ |
||
| 363 | public function setInvoicing(TransactionContainer $invoicing) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\Invoicing\TransactionContainer |
||
| 370 | */ |
||
| 371 | public function getInvoicing() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function getOnlinebanktransfertype() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $onlinebanktransfertype |
||
| 386 | * |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | public function setOnlinebanktransfertype($onlinebanktransfertype) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function getClearingsubtype(): string |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param string $clearingsubtype |
||
| 404 | * |
||
| 405 | * @return void |
||
| 406 | */ |
||
| 407 | public function setClearingsubtype(string $clearingsubtype): void |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | public function getIt(): array |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param array $it |
||
| 422 | */ |
||
| 423 | public function setIt(array $it): void |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | public function getId(): array |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param array $id |
||
| 438 | */ |
||
| 439 | public function setId(array $id): void |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getPr(): array |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param array $pr |
||
| 454 | */ |
||
| 455 | public function setPr(array $pr): void |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @return array |
||
| 462 | */ |
||
| 463 | public function getNo(): array |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param array $no |
||
| 470 | */ |
||
| 471 | public function setNo(array $no): void |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function getDe(): array |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param array $de |
||
| 486 | */ |
||
| 487 | public function setDe(array $de): void |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function getVa(): array |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param array $va |
||
| 502 | */ |
||
| 503 | public function setVa(array $va): void |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function getEmail(): string |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $email |
||
| 518 | */ |
||
| 519 | public function setEmail(string $email): void |
||
| 523 | } |
||
| 524 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.