Complex classes like PayPalGetExpressCheckoutReply 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 PayPalGetExpressCheckoutReply, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class PayPalGetExpressCheckoutReply implements IPayPalGetExpressCheckoutReply |
||
| 28 | { |
||
| 29 | use TTopLevelPayload, TOrderId, TBillingAddress, TShippingAddress; |
||
| 30 | |||
| 31 | const ADDRESS_INTERFACE = '\eBayEnterprise\RetailOrderManagement\Payload\Payment\IPayPalAddress'; |
||
| 32 | const SUCCESS = 'Success'; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $responseCode; |
||
| 36 | /** @var string */ |
||
| 37 | protected $payerEmail; |
||
| 38 | /** @var string */ |
||
| 39 | protected $payerId; |
||
| 40 | /** @var string */ |
||
| 41 | protected $payerStatus; |
||
| 42 | /** @var string */ |
||
| 43 | protected $payerNameHonorific; |
||
| 44 | /** @var string */ |
||
| 45 | protected $payerLastName; |
||
| 46 | /** @var string */ |
||
| 47 | protected $payerMiddleName; |
||
| 48 | /** @var string */ |
||
| 49 | protected $payerFirstName; |
||
| 50 | /** @var string */ |
||
| 51 | protected $payerCountry; |
||
| 52 | /** @var string */ |
||
| 53 | protected $payerPhone; |
||
| 54 | /** @var string */ |
||
| 55 | protected $billingAddressStatus; |
||
| 56 | /** @var string */ |
||
| 57 | protected $shippingAddressStatus; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param IValidatorIterator |
||
| 61 | * @param ISchemaValidator |
||
| 62 | * @param IPayloadMap |
||
| 63 | * @param LoggerInterface |
||
| 64 | * @param IPayload |
||
| 65 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 68 | IValidatorIterator $validators, |
||
| 69 | ISchemaValidator $schemaValidator, |
||
| 70 | IPayloadMap $payloadMap, |
||
| 71 | LoggerInterface $logger, |
||
| 72 | IPayload $parentPayload = null |
||
| 73 | ) { |
||
| 74 | $this->logger = $logger; |
||
|
|
|||
| 75 | $this->validators = $validators; |
||
| 76 | $this->schemaValidator = $schemaValidator; |
||
| 77 | $this->payloadMap = $payloadMap; |
||
| 78 | $this->parentPayload = $parentPayload; |
||
| 79 | $this->payloadFactory = new PayloadFactory(); |
||
| 80 | |||
| 81 | $this->extractionPaths = [ |
||
| 82 | 'orderId' => 'string(x:OrderId)', |
||
| 83 | 'responseCode' => 'string(x:ResponseCode)', |
||
| 84 | 'payerEmail' => 'string(x:PayerEmail)', |
||
| 85 | 'payerId' => 'string(x:PayerId)', |
||
| 86 | 'payerStatus' => 'string(x:PayerStatus)', |
||
| 87 | 'payerNameHonorific' => 'string(x:PayerName/x:Honorific)', |
||
| 88 | 'payerLastName' => 'string(x:PayerName/x:LastName)', |
||
| 89 | 'payerMiddleName' => 'string(x:PayerName/x:MiddleName)', |
||
| 90 | 'payerFirstName' => 'string(x:PayerName/x:FirstName)', |
||
| 91 | 'payerCountry' => 'string(x:PayerCountry)', |
||
| 92 | 'payerPhone' => 'string(x:PayerPhone)', |
||
| 93 | 'billingCity' => 'string(x:BillingAddress/x:City)', |
||
| 94 | 'billingCountryCode' => 'string(x:BillingAddress/x:CountryCode)', |
||
| 95 | 'shipToCity' => 'string(x:ShippingAddress/x:City)', |
||
| 96 | 'shipToCountryCode' => 'string(x:ShippingAddress/x:CountryCode)', |
||
| 97 | ]; |
||
| 98 | $this->optionalExtractionPaths = [ |
||
| 99 | 'billingMainDivision' => 'x:BillingAddress/x:MainDivision', |
||
| 100 | 'billingPostalCode' => 'x:BillingAddress/x:PostalCode', |
||
| 101 | 'shipToMainDivision' => 'x:ShippingAddress/x:MainDivision', |
||
| 102 | 'shipToPostalCode' => 'x:ShippingAddress/x:PostalCode', |
||
| 103 | 'billingAddressStatus' => 'x:BillingAddress/x:AddressStatus', |
||
| 104 | 'shippingAddressStatus' => 'x:ShippingAddress/x:AddressStatus', |
||
| 105 | ]; |
||
| 106 | $this->addressLinesExtractionMap = [ |
||
| 107 | [ |
||
| 108 | 'property' => 'billingLines', |
||
| 109 | 'xPath' => "x:BillingAddress/*[starts-with(name(), 'Line')]" |
||
| 110 | ], |
||
| 111 | [ |
||
| 112 | 'property' => 'shipToLines', |
||
| 113 | 'xPath' => "x:ShippingAddress/*[starts-with(name(), 'Line')]" |
||
| 114 | ] |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Should downstream systems consider this reply a success? |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function isSuccess() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Response code like Success, Failure etc |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getResponseCode() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | public function setResponseCode($code) |
||
| 143 | { |
||
| 144 | $this->responseCode = $code; |
||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Serialize a complete reply |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function serializeContents() |
||
| 153 | { |
||
| 154 | return $this->serializeOrderId() |
||
| 155 | . $this->serializeResponseCode() |
||
| 156 | . $this->serializePayerEmail() |
||
| 157 | . $this->serializePayerId() |
||
| 158 | . $this->serializePayerStatus() |
||
| 159 | . $this->serializePayerName() |
||
| 160 | . $this->serializePayerCountry() |
||
| 161 | . $this->serializeBillingAddressWithStatus() |
||
| 162 | . $this->serializePayerPhone() |
||
| 163 | . $this->serializeShippingAddressWithStatus(); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Serialize a ResponseCode |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | protected function serializeResponseCode() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Serialize a PayerEmail |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | protected function serializePayerEmail() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Email address of the payer. Character length and limitations: 127 single-byte characters |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getPayerEmail() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string |
||
| 196 | * @return self |
||
| 197 | */ |
||
| 198 | public function setPayerEmail($email) |
||
| 199 | { |
||
| 200 | $this->payerEmail = $email; |
||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Serialize a PayerId |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | protected function serializePayerId() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Unique identifier of the customer's PayPal account. Character length and limitations: 17 single-byte characters |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getPayerId() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string |
||
| 225 | * @return self |
||
| 226 | */ |
||
| 227 | public function setPayerId($id) |
||
| 228 | { |
||
| 229 | $this->payerId = $id; |
||
| 230 | return $this; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Serialize a PayerStatus |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | protected function serializePayerStatus() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Status of payer's email address. |
||
| 244 | * "verified" or "unverified" |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getPayerStatus() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string |
||
| 255 | * @return self |
||
| 256 | */ |
||
| 257 | public function setPayerStatus($payerStatus) |
||
| 258 | { |
||
| 259 | $this->payerStatus = $payerStatus; |
||
| 260 | return $this; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Billing address status to be sent to the Order Management System |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getBillingAddressStatus() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | public function setBillingAddressStatus($status) |
||
| 278 | { |
||
| 279 | $this->billingAddressStatus = $status; |
||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Shipping address status to be sent to the Order Management System |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getShippingAddressStatus() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | public function setShippingAddressStatus($status) |
||
| 298 | { |
||
| 299 | $this->shippingAddressStatus = $status; |
||
| 300 | return $this; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Serialize Payer Name Details |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | protected function serializePayerName() |
||
| 308 | { |
||
| 309 | return "<PayerName>" |
||
| 310 | . "<Honorific>{$this->xmlEncode($this->getPayerNameHonorific())}</Honorific>" |
||
| 311 | . "<LastName>{$this->xmlEncode($this->getPayerLastName())}</LastName>" |
||
| 312 | . "<MiddleName>{$this->xmlEncode($this->getPayerMiddleName())}</MiddleName>" |
||
| 313 | . "<FirstName>{$this->xmlEncode($this->getPayerFirstName())}</FirstName>" |
||
| 314 | . "</PayerName>"; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * A title you can assign to the payer. Typically "Dr.", "Mr.", "Ms." etc. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getPayerNameHonorific() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string |
||
| 329 | * @return self |
||
| 330 | */ |
||
| 331 | public function setPayerNameHonorific($hon) |
||
| 332 | { |
||
| 333 | $this->payerNameHonorific = $hon; |
||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * The surname of the payer. |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getPayerLastName() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string |
||
| 349 | * @return self |
||
| 350 | */ |
||
| 351 | public function setPayerLastName($name) |
||
| 352 | { |
||
| 353 | $this->payerLastName = $name; |
||
| 354 | return $this; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * The payer's middle name. |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getPayerMiddleName() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string |
||
| 369 | * @return self |
||
| 370 | */ |
||
| 371 | public function setPayerMiddleName($name) |
||
| 372 | { |
||
| 373 | $this->payerMiddleName = $name; |
||
| 374 | return $this; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * The payer's first name. |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getPayerFirstName() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string |
||
| 389 | * @return self |
||
| 390 | */ |
||
| 391 | public function setPayerFirstName($name) |
||
| 392 | { |
||
| 393 | $this->payerFirstName = $name; |
||
| 394 | return $this; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Serialize Payer Country |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | protected function serializePayerCountry() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Payment sender's country of residence using standard two-character ISO 3166 country codes. |
||
| 408 | * Character length and limitations: Two single-byte characters. |
||
| 409 | * |
||
| 410 | * @link http://countrycode.org/ |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getPayerCountry() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param string |
||
| 420 | * @return self |
||
| 421 | */ |
||
| 422 | public function setPayerCountry($country) |
||
| 423 | { |
||
| 424 | $this->payerCountry = $country; |
||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Serialize Payer Phone |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | protected function serializePayerPhone() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Payer's phone on file with PayPal. |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getPayerPhone() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string |
||
| 449 | * @return self |
||
| 450 | */ |
||
| 451 | public function setPayerPhone($phone) |
||
| 452 | { |
||
| 453 | $this->payerPhone = $phone; |
||
| 454 | return $this; |
||
| 455 | } |
||
| 456 | |||
| 457 | protected function getSchemaFile() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * The XML namespace for the payload. |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | protected function getXmlNamespace() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Return the name of the xml root node. |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | protected function getRootNodeName() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * add the address status element to a serialized IBillingAddress or |
||
| 484 | * IShippingAddress |
||
| 485 | * @param string $status |
||
| 486 | * @param string $serializedAddress |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | protected function injectAddressStatus($status, $serializedAddress) |
||
| 490 | { |
||
| 491 | if ($status) { |
||
| 492 | $closeTagPos = strrpos($serializedAddress, '</'); |
||
| 493 | $serializedAddress = substr_replace( |
||
| 494 | $serializedAddress, |
||
| 495 | "<AddressStatus>{$this->xmlEncode($status)}</AddressStatus>", |
||
| 496 | $closeTagPos, |
||
| 497 | 0 |
||
| 498 | ); |
||
| 499 | } |
||
| 500 | return $serializedAddress; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * serialize the billing address along with the address |
||
| 505 | * status |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | protected function serializeBillingAddressWithStatus() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * serialize the shipping address along with the address |
||
| 515 | * status |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | protected function serializeShippingAddressWithStatus() |
||
| 522 | } |
||
| 523 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..