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 |
||
12 | class CartSummary extends AbstractExtractor implements StepInterface |
||
13 | { |
||
14 | const EXTRACTOR = 'Checkout\CartSummary'; |
||
15 | /** |
||
16 | * Redefined here has a code completion helper |
||
17 | * |
||
18 | * @var \Magium\Magento\Themes\OnePageCheckout\AbstractThemeConfiguration |
||
19 | */ |
||
20 | |||
21 | protected $theme; |
||
22 | |||
23 | const VALUE_PRODUCTS = 'products'; |
||
24 | const VALUE_SUBTOTAL = 'subtotal'; |
||
25 | const VALUE_SnH = 'ship-handle'; |
||
26 | const VALUE_TAX = 'tax'; |
||
27 | const VALUE_GRAND_TOTAL = 'grand-total'; |
||
28 | |||
29 | public function __construct( |
||
37 | |||
38 | /** |
||
39 | * @return ProductIterator |
||
40 | */ |
||
41 | |||
42 | public function getProducts() |
||
46 | |||
47 | public function getSubTotal() |
||
51 | |||
52 | public function getShippingAndHandling() |
||
56 | |||
57 | public function getTax() |
||
61 | |||
62 | public function getGrandTotal() |
||
66 | |||
67 | public function extract() |
||
68 | { |
||
69 | $productIterator = new ProductIterator(); |
||
70 | $this->values[self::VALUE_PRODUCTS] = $productIterator; |
||
71 | $count = 1; |
||
72 | |||
73 | $testProductXpath = $this->theme->getCartSummaryCheckoutProductLoopNameXpath($count); |
||
74 | $this->webDriver->wait()->until(ExpectedCondition::elementExists($testProductXpath, WebDriver::BY_XPATH)); |
||
|
|||
75 | |||
76 | while ($this->webDriver->elementExists($testProductXpath, WebDriver::BY_XPATH)) { |
||
77 | |||
78 | $xpath = $this->theme->getCartSummaryCheckoutProductLoopNameXpath($count); |
||
79 | $nameElement = $this->webDriver->byXpath($xpath); |
||
80 | $name = trim($nameElement->getText()); |
||
81 | |||
82 | $xpath = $this->theme->getCartSummaryCheckoutProductLoopPriceXpath($count); |
||
83 | View Code Duplication | if ($this->webDriver->elementExists($xpath, WebDriver::BY_XPATH)) { |
|
84 | $priceElement = $this->webDriver->byXpath($xpath); |
||
85 | $price = trim($priceElement->getText()); // We do not extract the number value so currency checks can be done |
||
86 | } else { |
||
87 | $price = 0; |
||
88 | } |
||
89 | |||
90 | $xpath = $this->theme->getCartSummaryCheckoutProductLoopQtyXpath($count); |
||
91 | View Code Duplication | if ($this->webDriver->elementExists($xpath, WebDriver::BY_XPATH)) { |
|
92 | $qtyElement = $this->webDriver->byXpath($xpath); |
||
93 | $qty = trim($qtyElement->getText()); |
||
94 | } else { |
||
95 | $qty = 0; |
||
96 | } |
||
97 | |||
98 | $xpath = $this->theme->getCartSummaryCheckoutProductLoopSubtotalXpath($count); |
||
99 | View Code Duplication | if ($this->webDriver->elementExists($xpath, WebDriver::BY_XPATH)) { |
|
100 | $subtotalElement = $this->webDriver->byXpath($xpath); |
||
101 | $subtotal = trim($subtotalElement->getText()); |
||
102 | } else { |
||
103 | $subtotal = 0; |
||
104 | } |
||
105 | |||
106 | $product = new Product($name, $qty, $price, $subtotal); |
||
107 | $productIterator->addProduct($product); |
||
108 | $testProductXpath = $this->theme->getCartSummaryCheckoutProductLoopNameXpath(++$count); |
||
109 | } |
||
110 | |||
111 | // Tax and shipping may not be displayed |
||
112 | |||
113 | View Code Duplication | if ($this->webDriver->elementDisplayed($this->theme->getCartSummaryCheckoutTax(), WebDriver::BY_XPATH)) { |
|
114 | $this->values[self::VALUE_TAX] |
||
115 | = trim($this->webDriver->byXpath($this->theme->getCartSummaryCheckoutTax())->getText()); |
||
116 | } |
||
117 | View Code Duplication | if ($this->webDriver->elementDisplayed($this->theme->getCartSummaryCheckoutShippingTotal(), WebDriver::BY_XPATH)) { |
|
118 | $this->values[self::VALUE_SnH] |
||
119 | = trim($this->webDriver->byXpath($this->theme->getCartSummaryCheckoutShippingTotal())->getText()); |
||
120 | } |
||
121 | |||
122 | |||
123 | |||
124 | $this->values[self::VALUE_GRAND_TOTAL] |
||
125 | = trim($this->webDriver->byXpath($this->theme->getCartSummaryCheckoutGrandTotal())->getText()); |
||
126 | $this->values[self::VALUE_SUBTOTAL] |
||
127 | = trim($this->webDriver->byXpath($this->theme->getCartSummaryCheckoutSubTotal())->getText()); |
||
128 | |||
129 | } |
||
130 | |||
131 | public function execute() |
||
136 | |||
137 | public function nextAction() |
||
141 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: