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 SoapTransport 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 SoapTransport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class SoapTransport extends BaseSOAPTransport implements MagentoTransportInterface, ServerTimeAwareInterface |
||
| 36 | { |
||
| 37 | const REQUIRED_EXTENSION_VERSION = '1.2.0'; |
||
| 38 | |||
| 39 | const ACTION_CUSTOMER_LIST = 'customerCustomerList'; |
||
| 40 | const ACTION_CUSTOMER_INFO = 'customerCustomerInfo'; |
||
| 41 | const ACTION_CUSTOMER_UPDATE = 'customerCustomerUpdate'; |
||
| 42 | const ACTION_CUSTOMER_DELETE = 'customerCustomerDelete'; |
||
| 43 | const ACTION_CUSTOMER_CREATE = 'customerCustomerCreate'; |
||
| 44 | const ACTION_CUSTOMER_ADDRESS_LIST = 'customerAddressList'; |
||
| 45 | const ACTION_CUSTOMER_ADDRESS_INFO = 'customerAddressInfo'; |
||
| 46 | const ACTION_CUSTOMER_ADDRESS_UPDATE = 'customerAddressUpdate'; |
||
| 47 | const ACTION_CUSTOMER_ADDRESS_DELETE = 'customerAddressDelete'; |
||
| 48 | const ACTION_CUSTOMER_ADDRESS_CREATE = 'customerAddressCreate'; |
||
| 49 | const ACTION_ADDRESS_LIST = 'customerAddressList'; |
||
| 50 | const ACTION_GROUP_LIST = 'customerGroupList'; |
||
| 51 | const ACTION_STORE_LIST = 'storeList'; |
||
| 52 | const ACTION_ORDER_LIST = 'salesOrderList'; |
||
| 53 | const ACTION_ORDER_INFO = 'salesOrderInfo'; |
||
| 54 | const ACTION_CART_INFO = 'shoppingCartInfo'; |
||
| 55 | const ACTION_COUNTRY_LIST = 'directoryCountryList'; |
||
| 56 | const ACTION_REGION_LIST = 'directoryRegionList'; |
||
| 57 | const ACTION_PING = 'oroPing'; |
||
| 58 | |||
| 59 | const ACTION_ORO_CART_LIST = 'oroQuoteList'; |
||
| 60 | const ACTION_ORO_ORDER_LIST = 'oroOrderList'; |
||
| 61 | const ACTION_ORO_ORDER_INFO = 'oroOrderInfo'; |
||
| 62 | const ACTION_ORO_CUSTOMER_LIST = 'oroCustomerList'; |
||
| 63 | const ACTION_ORO_CUSTOMER_INFO = 'oroCustomerInfo'; |
||
| 64 | const ACTION_ORO_CUSTOMER_ADDRESS_LIST = 'oroCustomerAddressList'; |
||
| 65 | const ACTION_ORO_CUSTOMER_ADDRESS_INFO = 'oroCustomerAddressInfo'; |
||
| 66 | const ACTION_ORO_CUSTOMER_CREATE = 'oroCustomerCreate'; |
||
| 67 | const ACTION_ORO_CUSTOMER_UPDATE = 'oroCustomerUpdate'; |
||
| 68 | const ACTION_ORO_NEWSLETTER_SUBSCRIBER_LIST = 'newsletterSubscriberList'; |
||
| 69 | const ACTION_ORO_NEWSLETTER_SUBSCRIBER_CREATE = 'newsletterSubscriberCreate'; |
||
| 70 | const ACTION_ORO_NEWSLETTER_SUBSCRIBER_UPDATE = 'newsletterSubscriberUpdate'; |
||
| 71 | const ACTION_ORO_WEBSITE_LIST = 'oroWebsiteList'; |
||
| 72 | |||
| 73 | const SOAP_FAULT_ADDRESS_DOES_NOT_EXIST = 102; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | protected $sessionId; |
||
| 77 | |||
| 78 | /** @var Mcrypt */ |
||
| 79 | protected $encoder; |
||
| 80 | |||
| 81 | /** @var bool */ |
||
| 82 | protected $isExtensionInstalled; |
||
| 83 | |||
| 84 | /** @var string */ |
||
| 85 | protected $magentoVersion; |
||
| 86 | |||
| 87 | /** @var string */ |
||
| 88 | protected $extensionVersion; |
||
| 89 | |||
| 90 | /** @var bool */ |
||
| 91 | protected $isWsiMode = false; |
||
| 92 | |||
| 93 | /** @var string */ |
||
| 94 | protected $adminUrl; |
||
| 95 | |||
| 96 | /** @var string */ |
||
| 97 | protected $serverTime; |
||
| 98 | |||
| 99 | /** @var array */ |
||
| 100 | protected $dependencies = []; |
||
| 101 | |||
| 102 | /** @var WsdlManager */ |
||
| 103 | protected $wsdlManager; |
||
| 104 | |||
| 105 | /** @var array */ |
||
| 106 | protected $auth = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $bundleConfig; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param Mcrypt $encoder |
||
| 115 | * @param WsdlManager $wsdlManager |
||
| 116 | * @param array $bundleConfig |
||
| 117 | */ |
||
| 118 | public function __construct(Mcrypt $encoder, WsdlManager $wsdlManager, array $bundleConfig = []) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function init(Transport $transportEntity) |
||
| 177 | |||
| 178 | protected function checkExtensionFunctions() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Disable wsdl caching by PHP. |
||
| 197 | * |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | protected function getSoapClient($wsdlUrl, array $options = []) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | public function call($action, $params = []) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function isExtensionInstalled() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function getMagentoVersion() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function getExtensionVersion() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function isSupportedExtensionVersion() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Pings magento and fill data related to Bridge Extension. |
||
| 303 | * |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | protected function pingMagento() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | */ |
||
| 336 | public function getAdminUrl() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function getServerTime() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | public function getOrders() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritdoc} |
||
| 369 | */ |
||
| 370 | public function getOrderInfo($incrementId) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * {@inheritdoc} |
||
| 383 | */ |
||
| 384 | public function getCarts() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * {@inheritdoc} |
||
| 395 | */ |
||
| 396 | public function getCustomers() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * {@inheritdoc} |
||
| 409 | */ |
||
| 410 | public function getCustomerGroups() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * {@inheritdoc} |
||
| 417 | */ |
||
| 418 | public function getStores() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * {@inheritdoc} |
||
| 425 | */ |
||
| 426 | public function getWebsites() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | public function getRegions() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * {@inheritdoc} |
||
| 441 | */ |
||
| 442 | public function getCustomerAddresses($originId) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * {@inheritdoc} |
||
| 458 | */ |
||
| 459 | public function createCustomer(array $customerData) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | */ |
||
| 473 | public function updateCustomer($customerId, array $customerData) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * {@inheritdoc} |
||
| 486 | */ |
||
| 487 | public function createCustomerAddress($customerId, array $item) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | public function updateCustomerAddress($customerAddressId, array $item) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * {@inheritdoc} |
||
| 508 | */ |
||
| 509 | public function getCustomerAddressInfo($customerAddressId) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * {@inheritdoc} |
||
| 522 | */ |
||
| 523 | public function getCustomerInfo($originId) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * {@inheritdoc} |
||
| 536 | */ |
||
| 537 | public function getNewsletterSubscribers() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * {@inheritdoc} |
||
| 548 | */ |
||
| 549 | View Code Duplication | public function createNewsletterSubscriber(array $subscriberData) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * {@inheritdoc} |
||
| 565 | */ |
||
| 566 | View Code Duplication | public function updateNewsletterSubscriber($subscriberId, array $subscriberData) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * {@inheritdoc} |
||
| 582 | */ |
||
| 583 | public function getErrorCode(\Exception $e) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * {@inheritdoc} |
||
| 595 | */ |
||
| 596 | public function getLabel() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * {@inheritdoc} |
||
| 603 | */ |
||
| 604 | public function getSettingsFormType() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * {@inheritdoc} |
||
| 611 | */ |
||
| 612 | public function getSettingsEntityFQCN() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Tries to fetch date from response headers |
||
| 619 | */ |
||
| 620 | protected function lookUpForServerTime() |
||
| 632 | } |
||
| 633 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.