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 | protected function makeNewAttempt($action, $params) |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function isExtensionInstalled() |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function getMagentoVersion() |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function getExtensionVersion() |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function isSupportedExtensionVersion() |
||
317 | |||
318 | /** |
||
319 | * Pings magento and fill data related to Bridge Extension. |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | protected function pingMagento() |
||
349 | |||
350 | /** |
||
351 | * {@inheritdoc} |
||
352 | */ |
||
353 | public function getAdminUrl() |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function getServerTime() |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function getOrders() |
||
383 | |||
384 | /** |
||
385 | * {@inheritdoc} |
||
386 | */ |
||
387 | public function getOrderInfo($incrementId) |
||
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | public function getCarts() |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function getCustomers() |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function getCustomerGroups() |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | public function getStores() |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function getWebsites() |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | public function getRegions() |
||
455 | |||
456 | /** |
||
457 | * {@inheritdoc} |
||
458 | */ |
||
459 | public function getCustomerAddresses($originId) |
||
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | public function createCustomer(array $customerData) |
||
486 | |||
487 | /** |
||
488 | * {@inheritdoc} |
||
489 | */ |
||
490 | public function updateCustomer($customerId, array $customerData) |
||
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | public function createCustomerAddress($customerId, array $item) |
||
511 | |||
512 | /** |
||
513 | * {@inheritdoc} |
||
514 | */ |
||
515 | public function updateCustomerAddress($customerAddressId, array $item) |
||
522 | |||
523 | /** |
||
524 | * {@inheritdoc} |
||
525 | */ |
||
526 | public function getCustomerAddressInfo($customerAddressId) |
||
536 | |||
537 | /** |
||
538 | * {@inheritdoc} |
||
539 | */ |
||
540 | public function getCustomerInfo($originId) |
||
550 | |||
551 | /** |
||
552 | * {@inheritdoc} |
||
553 | */ |
||
554 | public function getNewsletterSubscribers() |
||
562 | |||
563 | /** |
||
564 | * {@inheritdoc} |
||
565 | */ |
||
566 | View Code Duplication | public function createNewsletterSubscriber(array $subscriberData) |
|
579 | |||
580 | /** |
||
581 | * {@inheritdoc} |
||
582 | */ |
||
583 | View Code Duplication | public function updateNewsletterSubscriber($subscriberId, array $subscriberData) |
|
596 | |||
597 | /** |
||
598 | * {@inheritdoc} |
||
599 | */ |
||
600 | public function getErrorCode(\Exception $e) |
||
609 | |||
610 | /** |
||
611 | * {@inheritdoc} |
||
612 | */ |
||
613 | public function getLabel() |
||
617 | |||
618 | /** |
||
619 | * {@inheritdoc} |
||
620 | */ |
||
621 | public function getSettingsFormType() |
||
625 | |||
626 | /** |
||
627 | * {@inheritdoc} |
||
628 | */ |
||
629 | public function getSettingsEntityFQCN() |
||
633 | |||
634 | /** |
||
635 | * Tries to fetch date from response headers |
||
636 | */ |
||
637 | protected function lookUpForServerTime() |
||
649 | } |
||
650 |
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.