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:
1 | <?php |
||
11 | class BillingAddress implements StepInterface |
||
12 | { |
||
13 | const ACTION = 'Checkout\Steps\BillingAddress'; |
||
14 | |||
15 | protected $webdriver; |
||
16 | protected $theme; |
||
17 | protected $customerIdentity; |
||
18 | protected $testCase; |
||
19 | protected $shipToDifferentAddress; |
||
20 | |||
21 | protected $enterNewAddress; |
||
22 | protected $bypass = []; |
||
23 | |||
24 | public function __construct( |
||
35 | |||
36 | /** |
||
37 | * Allows you to bypass arbitrary element assertions and entry. Currently only supports email address. |
||
38 | * |
||
39 | * @see Magium\Magento\Actions\Checkout\Steps\CustomerBillingAddress |
||
40 | * |
||
41 | * @param $element The name of the element |
||
42 | */ |
||
43 | |||
44 | public function bypassElement($element) |
||
48 | |||
49 | public function shipToDifferentAddress($ship = true) |
||
53 | |||
54 | public function enterNewAddress($newAddress = true) |
||
58 | |||
59 | protected function preExecute() |
||
60 | { |
||
61 | if ($this->enterNewAddress & $this->webdriver->elementDisplayed($this->theme->getBillingNewAddressXpath(), WebDriver::BY_XPATH)) { |
||
62 | $this->webdriver->byXpath($this->theme->getBillingNewAddressXpath())->click(); |
||
63 | } |
||
64 | |||
65 | if ($this->shipToDifferentAddress) { |
||
66 | View Code Duplication | if ($this->webdriver->elementExists($this->theme->getDoNotUseBillingAddressForShipping(), WebDriver::BY_XPATH)) { |
|
|
|||
67 | $this->webdriver->byXpath($this->theme->getDoNotUseBillingAddressForShipping())->click(); |
||
68 | } |
||
69 | View Code Duplication | } else { |
|
70 | if ($this->webdriver->elementExists($this->theme->getUseBillingAddressForShipping(), WebDriver::BY_XPATH)) { |
||
71 | $this->webdriver->byXpath($this->theme->getUseBillingAddressForShipping())->click(); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | if (!$this->enterNewAddress && $this->webdriver->elementDisplayed($this->theme->getBillingAddressDropdownXpath(), WebDriver::BY_XPATH)) { |
||
76 | // We're logged in and we have an address. |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | protected function shouldProcess($xpath) |
||
90 | |||
91 | protected function sendData($xpath, $data) |
||
98 | |||
99 | public function execute() |
||
129 | |||
130 | |||
131 | View Code Duplication | public function nextAction() |
|
138 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.