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 |
||
34 | class SoapTransport extends BaseSOAPTransport implements MagentoTransportInterface, ServerTimeAwareInterface |
||
35 | { |
||
36 | const REQUIRED_EXTENSION_VERSION = '1.2.0'; |
||
37 | |||
38 | const ACTION_CUSTOMER_LIST = 'customerCustomerList'; |
||
39 | const ACTION_CUSTOMER_INFO = 'customerCustomerInfo'; |
||
40 | const ACTION_CUSTOMER_UPDATE = 'customerCustomerUpdate'; |
||
41 | const ACTION_CUSTOMER_DELETE = 'customerCustomerDelete'; |
||
42 | const ACTION_CUSTOMER_CREATE = 'customerCustomerCreate'; |
||
43 | const ACTION_CUSTOMER_ADDRESS_LIST = 'customerAddressList'; |
||
44 | const ACTION_CUSTOMER_ADDRESS_INFO = 'customerAddressInfo'; |
||
45 | const ACTION_CUSTOMER_ADDRESS_UPDATE = 'customerAddressUpdate'; |
||
46 | const ACTION_CUSTOMER_ADDRESS_DELETE = 'customerAddressDelete'; |
||
47 | const ACTION_CUSTOMER_ADDRESS_CREATE = 'customerAddressCreate'; |
||
48 | const ACTION_ADDRESS_LIST = 'customerAddressList'; |
||
49 | const ACTION_GROUP_LIST = 'customerGroupList'; |
||
50 | const ACTION_STORE_LIST = 'storeList'; |
||
51 | const ACTION_ORDER_LIST = 'salesOrderList'; |
||
52 | const ACTION_ORDER_INFO = 'salesOrderInfo'; |
||
53 | const ACTION_CART_INFO = 'shoppingCartInfo'; |
||
54 | const ACTION_COUNTRY_LIST = 'directoryCountryList'; |
||
55 | const ACTION_REGION_LIST = 'directoryRegionList'; |
||
56 | const ACTION_PING = 'oroPing'; |
||
57 | |||
58 | const ACTION_ORO_CART_LIST = 'oroQuoteList'; |
||
59 | const ACTION_ORO_ORDER_LIST = 'oroOrderList'; |
||
60 | const ACTION_ORO_ORDER_INFO = 'oroOrderInfo'; |
||
61 | const ACTION_ORO_CUSTOMER_LIST = 'oroCustomerList'; |
||
62 | const ACTION_ORO_CUSTOMER_INFO = 'oroCustomerInfo'; |
||
63 | const ACTION_ORO_CUSTOMER_ADDRESS_LIST = 'oroCustomerAddressList'; |
||
64 | const ACTION_ORO_CUSTOMER_ADDRESS_INFO = 'oroCustomerAddressInfo'; |
||
65 | const ACTION_ORO_CUSTOMER_CREATE = 'oroCustomerCreate'; |
||
66 | const ACTION_ORO_CUSTOMER_UPDATE = 'oroCustomerUpdate'; |
||
67 | const ACTION_ORO_NEWSLETTER_SUBSCRIBER_LIST = 'newsletterSubscriberList'; |
||
68 | const ACTION_ORO_NEWSLETTER_SUBSCRIBER_CREATE = 'newsletterSubscriberCreate'; |
||
69 | const ACTION_ORO_NEWSLETTER_SUBSCRIBER_UPDATE = 'newsletterSubscriberUpdate'; |
||
70 | const ACTION_ORO_WEBSITE_LIST = 'oroWebsiteList'; |
||
71 | |||
72 | const SOAP_FAULT_ADDRESS_DOES_NOT_EXIST = 102; |
||
73 | |||
74 | /** @var string */ |
||
75 | protected $sessionId; |
||
76 | |||
77 | /** @var Mcrypt */ |
||
78 | protected $encoder; |
||
79 | |||
80 | /** @var bool */ |
||
81 | protected $isExtensionInstalled; |
||
82 | |||
83 | /** @var string */ |
||
84 | protected $magentoVersion; |
||
85 | |||
86 | /** @var string */ |
||
87 | protected $extensionVersion; |
||
88 | |||
89 | /** @var bool */ |
||
90 | protected $isWsiMode = false; |
||
91 | |||
92 | /** @var string */ |
||
93 | protected $adminUrl; |
||
94 | |||
95 | /** @var string */ |
||
96 | protected $serverTime; |
||
97 | |||
98 | /** @var array */ |
||
99 | protected $dependencies = []; |
||
100 | |||
101 | /** @var WsdlManager */ |
||
102 | protected $wsdlManager; |
||
103 | |||
104 | /** @var array */ |
||
105 | protected $auth = []; |
||
106 | |||
107 | /** |
||
108 | * @var array |
||
109 | */ |
||
110 | protected $bundleConfig; |
||
111 | |||
112 | /** |
||
113 | * @param Mcrypt $encoder |
||
114 | * @param WsdlManager $wsdlManager |
||
115 | * @param array $bundleConfig |
||
116 | */ |
||
117 | public function __construct(Mcrypt $encoder, WsdlManager $wsdlManager, array $bundleConfig = []) |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | public function init(Transport $transportEntity) |
||
176 | |||
177 | protected function checkExtensionFunctions() |
||
193 | |||
194 | /** |
||
195 | * Disable wsdl caching by PHP. |
||
196 | * |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | protected function getSoapClient($wsdlUrl, array $options = []) |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function call($action, $params = []) |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | protected function makeNewAttempt($action, $params) |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function isExtensionInstalled() |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function getMagentoVersion() |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function getExtensionVersion() |
||
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | public function isSupportedExtensionVersion() |
||
316 | |||
317 | /** |
||
318 | * Pings magento and fill data related to Bridge Extension. |
||
319 | * |
||
320 | * @return $this |
||
321 | */ |
||
322 | protected function pingMagento() |
||
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | public function getAdminUrl() |
||
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | */ |
||
364 | public function getServerTime() |
||
368 | |||
369 | /** |
||
370 | * {@inheritdoc} |
||
371 | */ |
||
372 | public function getOrders() |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | */ |
||
386 | public function getOrderInfo($incrementId) |
||
396 | |||
397 | /** |
||
398 | * {@inheritdoc} |
||
399 | */ |
||
400 | public function getCarts() |
||
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | */ |
||
412 | public function getCustomers() |
||
422 | |||
423 | /** |
||
424 | * {@inheritdoc} |
||
425 | */ |
||
426 | public function getCustomerGroups() |
||
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | */ |
||
434 | public function getStores() |
||
438 | |||
439 | /** |
||
440 | * {@inheritdoc} |
||
441 | */ |
||
442 | public function getWebsites() |
||
446 | |||
447 | /** |
||
448 | * {@inheritdoc} |
||
449 | */ |
||
450 | public function getRegions() |
||
454 | |||
455 | /** |
||
456 | * {@inheritdoc} |
||
457 | */ |
||
458 | public function getCustomerAddresses($originId) |
||
471 | |||
472 | /** |
||
473 | * {@inheritdoc} |
||
474 | */ |
||
475 | public function createCustomer(array $customerData) |
||
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | public function updateCustomer($customerId, array $customerData) |
||
499 | |||
500 | /** |
||
501 | * {@inheritdoc} |
||
502 | */ |
||
503 | public function createCustomerAddress($customerId, array $item) |
||
510 | |||
511 | /** |
||
512 | * {@inheritdoc} |
||
513 | */ |
||
514 | public function updateCustomerAddress($customerAddressId, array $item) |
||
521 | |||
522 | /** |
||
523 | * {@inheritdoc} |
||
524 | */ |
||
525 | public function getCustomerAddressInfo($customerAddressId) |
||
535 | |||
536 | /** |
||
537 | * {@inheritdoc} |
||
538 | */ |
||
539 | public function getCustomerInfo($originId) |
||
549 | |||
550 | /** |
||
551 | * {@inheritdoc} |
||
552 | */ |
||
553 | public function getNewsletterSubscribers() |
||
561 | |||
562 | /** |
||
563 | * {@inheritdoc} |
||
564 | */ |
||
565 | View Code Duplication | public function createNewsletterSubscriber(array $subscriberData) |
|
578 | |||
579 | /** |
||
580 | * {@inheritdoc} |
||
581 | */ |
||
582 | View Code Duplication | public function updateNewsletterSubscriber($subscriberId, array $subscriberData) |
|
595 | |||
596 | /** |
||
597 | * {@inheritdoc} |
||
598 | */ |
||
599 | public function getErrorCode(\Exception $e) |
||
608 | |||
609 | /** |
||
610 | * {@inheritdoc} |
||
611 | */ |
||
612 | public function getLabel() |
||
616 | |||
617 | /** |
||
618 | * {@inheritdoc} |
||
619 | */ |
||
620 | public function getSettingsFormType() |
||
624 | |||
625 | /** |
||
626 | * {@inheritdoc} |
||
627 | */ |
||
628 | public function getSettingsEntityFQCN() |
||
632 | |||
633 | /** |
||
634 | * Tries to fetch date from response headers |
||
635 | */ |
||
636 | protected function lookUpForServerTime() |
||
648 | } |
||
649 |
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.