Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Bpost 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 Bpost, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Bpost |
||
| 28 | { |
||
| 29 | |||
| 30 | const LABEL_FORMAT_A4 = 'A4'; |
||
| 31 | const LABEL_FORMAT_A6 = 'A6'; |
||
| 32 | |||
| 33 | // URL for the api |
||
| 34 | const API_URL = 'https://api-parcel.bpost.be/services/shm'; |
||
| 35 | |||
| 36 | // current version |
||
| 37 | const VERSION = '3.3.0'; |
||
| 38 | |||
| 39 | /** Min weight, in grams, for a shipping */ |
||
| 40 | const MIN_WEIGHT = 0; |
||
| 41 | |||
| 42 | /** Max weight, in grams, for a shipping */ |
||
| 43 | const MAX_WEIGHT = 30000; |
||
| 44 | |||
| 45 | /** @var ApiCaller */ |
||
| 46 | private $apiCaller; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The account id |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $accountId; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * A cURL instance |
||
| 57 | * |
||
| 58 | * @var resource |
||
| 59 | */ |
||
| 60 | private $curl; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The passPhrase |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $passPhrase; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The port to use. |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | private $port; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The timeout |
||
| 78 | * |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | private $timeOut = 30; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The user agent |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private $userAgent; |
||
| 89 | |||
| 90 | private $apiUrl; |
||
| 91 | |||
| 92 | /** @var Logger */ |
||
| 93 | private $logger; |
||
| 94 | |||
| 95 | // class methods |
||
| 96 | /** |
||
| 97 | * Create Bpost instance |
||
| 98 | * |
||
| 99 | * @param string $accountId |
||
| 100 | * @param string $passPhrase |
||
| 101 | * @param string $apiUrl |
||
| 102 | */ |
||
| 103 | public function __construct($accountId, $passPhrase, $apiUrl = self::API_URL) |
||
| 104 | { |
||
| 105 | $this->accountId = (string)$accountId; |
||
| 106 | $this->passPhrase = (string)$passPhrase; |
||
| 107 | $this->apiUrl = (string)$apiUrl; |
||
| 108 | $this->logger = new Logger(); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return ApiCaller |
||
| 113 | */ |
||
| 114 | public function getApiCaller() |
||
| 115 | { |
||
| 116 | if ($this->apiCaller === null) { |
||
| 117 | $this->apiCaller = new ApiCaller($this->logger); |
||
| 118 | } |
||
| 119 | return $this->apiCaller; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param ApiCaller $apiCaller |
||
| 124 | */ |
||
| 125 | public function setApiCaller(ApiCaller $apiCaller) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Destructor |
||
| 132 | */ |
||
| 133 | public function __destruct() |
||
| 134 | { |
||
| 135 | if ($this->curl !== null) { |
||
| 136 | curl_close($this->curl); |
||
| 137 | $this->curl = null; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Decode the response |
||
| 143 | * |
||
| 144 | * @param \SimpleXMLElement $item The item to decode. |
||
| 145 | * @param array $return Just a placeholder. |
||
|
|
|||
| 146 | * @param int $i A internal counter. |
||
| 147 | * @return array |
||
| 148 | * @throws BpostXmlInvalidItemException |
||
| 149 | */ |
||
| 150 | private static function decodeResponse($item, $return = null, $i = 0) |
||
| 151 | { |
||
| 152 | if (!$item instanceof \SimpleXMLElement) { |
||
| 153 | throw new BpostXmlInvalidItemException(); |
||
| 154 | } |
||
| 155 | |||
| 156 | $arrayKeys = array( |
||
| 157 | 'barcode', |
||
| 158 | 'orderLine', |
||
| 159 | Insurance::INSURANCE_TYPE_ADDITIONAL_INSURANCE, |
||
| 160 | Box\Option\Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, |
||
| 161 | 'infoPugo' |
||
| 162 | ); |
||
| 163 | $integerKeys = array('totalPrice'); |
||
| 164 | |||
| 165 | /** @var \SimpleXMLElement $value */ |
||
| 166 | foreach ($item as $key => $value) { |
||
| 167 | $attributes = (array)$value->attributes(); |
||
| 168 | |||
| 169 | if (!empty($attributes) && isset($attributes['@attributes'])) { |
||
| 170 | $return[$key]['@attributes'] = $attributes['@attributes']; |
||
| 171 | } |
||
| 172 | |||
| 173 | // empty |
||
| 174 | if (isset($value['nil']) && (string)$value['nil'] === 'true') { |
||
| 175 | $return[$key] = null; |
||
| 176 | } // empty |
||
| 177 | elseif (isset($value[0]) && (string)$value == '') { |
||
| 178 | if (in_array($key, $arrayKeys)) { |
||
| 179 | $return[$key][] = self::decodeResponse($value); |
||
| 180 | } else { |
||
| 181 | $return[$key] = self::decodeResponse($value, null, 1); |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | // arrays |
||
| 185 | if (in_array($key, $arrayKeys)) { |
||
| 186 | $return[$key][] = (string)$value; |
||
| 187 | } // booleans |
||
| 188 | elseif ((string)$value == 'true') { |
||
| 189 | $return[$key] = true; |
||
| 190 | } elseif ((string)$value == 'false') { |
||
| 191 | $return[$key] = false; |
||
| 192 | } // integers |
||
| 193 | elseif (in_array($key, $integerKeys)) { |
||
| 194 | $return[$key] = (int)$value; |
||
| 195 | } // fallback to string |
||
| 196 | else { |
||
| 197 | $return[$key] = (string)$value; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | return $return; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Make the call |
||
| 207 | * |
||
| 208 | * @param string $url The URL to call. |
||
| 209 | * @param string $body The data to pass. |
||
| 210 | * @param array $headers The headers to pass. |
||
| 211 | * @param string $method The HTTP-method to use. |
||
| 212 | * @param bool $expectXML Do we expect XML? |
||
| 213 | * @return mixed |
||
| 214 | * @throws BpostCurlException |
||
| 215 | * @throws BpostInvalidResponseException |
||
| 216 | * @throws BpostInvalidSelectionException |
||
| 217 | */ |
||
| 218 | private function doCall($url, $body = null, $headers = array(), $method = 'GET', $expectXML = true) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get the account id |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getAccountId() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Generate the secret string for the Authorization header |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | private function getAuthorizationHeader() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the passPhrase |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getPassPhrase() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the port |
||
| 316 | * |
||
| 317 | * @return int |
||
| 318 | */ |
||
| 319 | public function getPort() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the timeout that will be used |
||
| 326 | * |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | public function getTimeOut() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the useragent that will be used. |
||
| 336 | * Our version will be prepended to yours. |
||
| 337 | * It will look like: "PHP Bpost/<version> <your-user-agent>" |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getUserAgent() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Set the timeout |
||
| 348 | * After this time the request will stop. You should handle any errors triggered by this. |
||
| 349 | * |
||
| 350 | * @param int $seconds The timeout in seconds. |
||
| 351 | */ |
||
| 352 | public function setTimeOut($seconds) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set the user-agent for you application |
||
| 359 | * It will be appended to ours, the result will look like: "PHP Bpost/<version> <your-user-agent>" |
||
| 360 | * |
||
| 361 | * @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>. |
||
| 362 | */ |
||
| 363 | public function setUserAgent($userAgent) |
||
| 367 | |||
| 368 | // webservice methods |
||
| 369 | // orders |
||
| 370 | /** |
||
| 371 | * Creates a new order. If an order with the same orderReference already exists |
||
| 372 | * |
||
| 373 | * @param Order $order |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | * @throws BpostCurlException |
||
| 377 | * @throws BpostInvalidResponseException |
||
| 378 | * @throws BpostInvalidSelectionException |
||
| 379 | */ |
||
| 380 | public function createOrReplaceOrder(Order $order) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Fetch an order |
||
| 412 | * |
||
| 413 | * @param $reference |
||
| 414 | * |
||
| 415 | * @return Order |
||
| 416 | * @throws BpostCurlException |
||
| 417 | * @throws BpostInvalidResponseException |
||
| 418 | * @throws BpostInvalidSelectionException |
||
| 419 | * @throws Exception\XmlException\BpostXmlNoReferenceFoundException |
||
| 420 | */ |
||
| 421 | public function fetchOrder($reference) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get the products configuration |
||
| 439 | * |
||
| 440 | * @return ProductConfiguration |
||
| 441 | * @throws BpostCurlException |
||
| 442 | * @throws BpostInvalidResponseException |
||
| 443 | * @throws BpostInvalidSelectionException |
||
| 444 | */ |
||
| 445 | public function fetchProductConfig() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Modify the status for an order. |
||
| 464 | * |
||
| 465 | * @param string $reference The reference for an order |
||
| 466 | * @param string $status The new status, allowed values are: OPEN, PENDING, CANCELLED, COMPLETED, ON-HOLD or PRINTED |
||
| 467 | * |
||
| 468 | * @return bool |
||
| 469 | * @throws BpostCurlException |
||
| 470 | * @throws BpostInvalidResponseException |
||
| 471 | * @throws BpostInvalidSelectionException |
||
| 472 | * @throws BpostInvalidValueException |
||
| 473 | */ |
||
| 474 | public function modifyOrderStatus($reference, $status) |
||
| 509 | |||
| 510 | // labels |
||
| 511 | /** |
||
| 512 | * Get the possible label formats |
||
| 513 | * |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | public static function getPossibleLabelFormatValues() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Generic method to centralize handling of labels |
||
| 526 | * |
||
| 527 | * @param string $url |
||
| 528 | * @param string $format |
||
| 529 | * @param bool $withReturnLabels |
||
| 530 | * @param bool $asPdf |
||
| 531 | * |
||
| 532 | * @return Bpost\Label[] |
||
| 533 | * @throws BpostCurlException |
||
| 534 | * @throws BpostInvalidResponseException |
||
| 535 | * @throws BpostInvalidSelectionException |
||
| 536 | * @throws BpostInvalidValueException |
||
| 537 | */ |
||
| 538 | protected function getLabel($url, $format = self::LABEL_FORMAT_A6, $withReturnLabels = false, $asPdf = false) |
||
| 539 | { |
||
| 540 | $format = strtoupper($format); |
||
| 541 | if (!in_array($format, self::getPossibleLabelFormatValues())) { |
||
| 542 | throw new BpostInvalidValueException('format', $format, self::getPossibleLabelFormatValues()); |
||
| 543 | } |
||
| 544 | |||
| 545 | $url .= '/labels/' . $format; |
||
| 546 | if ($withReturnLabels) { |
||
| 547 | $url .= '/withReturnLabels'; |
||
| 548 | } |
||
| 549 | |||
| 550 | if ($asPdf) { |
||
| 551 | $headers = array( |
||
| 552 | 'Accept: application/vnd.bpost.shm-label-pdf-v3.4+XML' |
||
| 553 | ); |
||
| 554 | } else { |
||
| 555 | $headers = array( |
||
| 556 | 'Accept: application/vnd.bpost.shm-label-image-v3.4+XML', |
||
| 557 | ); |
||
| 558 | } |
||
| 559 | |||
| 560 | $xml = $this->doCall( |
||
| 561 | $url, |
||
| 562 | null, |
||
| 563 | $headers |
||
| 564 | ); |
||
| 565 | |||
| 566 | return Labels::createFromXML($xml); |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Create the labels for all unprinted boxes in an order. |
||
| 571 | * The service will return labels for all unprinted boxes for that order. |
||
| 572 | * Boxes that were unprinted will get the status PRINTED, the boxes that |
||
| 573 | * had already been printed will remain the same. |
||
| 574 | * |
||
| 575 | * @param string $reference The reference for an order |
||
| 576 | * @param string $format The desired format, allowed values are: A4, A6 |
||
| 577 | * @param bool $withReturnLabels Should return labels be returned? |
||
| 578 | * @param bool $asPdf Should we retrieve the PDF-version instead of PNG |
||
| 579 | * |
||
| 580 | * @return Bpost\Label[] |
||
| 581 | * @throws BpostInvalidValueException |
||
| 582 | */ |
||
| 583 | public function createLabelForOrder( |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Create a label for a known barcode. |
||
| 596 | * |
||
| 597 | * @param string $barcode The barcode of the parcel |
||
| 598 | * @param string $format The desired format, allowed values are: A4, A6 |
||
| 599 | * @param bool $withReturnLabels Should return labels be returned? |
||
| 600 | * @param bool $asPdf Should we retrieve the PDF-version instead of PNG |
||
| 601 | * |
||
| 602 | * @return Bpost\Label[] |
||
| 603 | * @throws BpostInvalidValueException |
||
| 604 | */ |
||
| 605 | public function createLabelForBox( |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Create labels in bulk, according to the list of order references and the |
||
| 618 | * list of barcodes. When there is an order reference specified in the |
||
| 619 | * request, the service will return a label of every box of that order. If |
||
| 620 | * a certain box was not yet printed, it will have the status PRINTED |
||
| 621 | * |
||
| 622 | * @param array $references The references for the order |
||
| 623 | * @param string $format The desired format, allowed values are: A4, A6 |
||
| 624 | * @param bool $withReturnLabels Should return labels be returned? |
||
| 625 | * @param bool $asPdf Should we retrieve the PDF-version instead of PNG |
||
| 626 | * @param bool $forcePrinting Reprint a already printed label |
||
| 627 | * |
||
| 628 | * @return Bpost\Label[] |
||
| 629 | * @throws BpostCurlException |
||
| 630 | * @throws BpostInvalidResponseException |
||
| 631 | * @throws BpostInvalidSelectionException |
||
| 632 | * @throws BpostInvalidValueException |
||
| 633 | */ |
||
| 634 | public function createLabelInBulkForOrders( |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Set a logger to permit to the plugin to log events |
||
| 655 | * |
||
| 656 | * @param LoggerInterface $logger |
||
| 657 | */ |
||
| 658 | public function setLogger(LoggerInterface $logger) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param int $weight in grams |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | public function isValidWeight($weight) |
||
| 671 | } |
||
| 672 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.