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 |
||
| 8 | View Code Duplication | class ThemeConfiguration extends AbstractThemeConfiguration |
|
|
|
|||
| 9 | { |
||
| 10 | |||
| 11 | const THEME = 'Magium\Magento\Themes\Magento19\ThemeConfiguration'; |
||
| 12 | |||
| 13 | public $homeXpath = '//a[@class="logo"]'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string The Xpath string that finds the base of the navigation menu |
||
| 17 | */ |
||
| 18 | public $navigationBaseXPathSelector = '//nav[@id="nav"]'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string The Xpath string that can be used iteratively to find child navigation nodes |
||
| 22 | */ |
||
| 23 | |||
| 24 | public $navigationChildXPathSelector = 'a[concat(" ",normalize-space(.)," ") = " %s "]/..'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string A simple, default path to use for categories. |
||
| 28 | */ |
||
| 29 | |||
| 30 | public $navigationPathToSimpleProductCategory = '{{Accessories}}/{{Jewelry}}'; |
||
| 31 | public $navigationPathToConfigurableProductCategory = '{{Men}}/{{Shirts}}'; |
||
| 32 | |||
| 33 | public $productPagePriceXpath = '(//form[@id="product_addtocart_form"]/descendant::span[contains(concat(" ",normalize-space(@class)," ")," regular-price ")]/span[contains(concat(" ",normalize-space(@class)," ")," price ")])[1]'; |
||
| 34 | |||
| 35 | public $defaultSimpleProductName = '{{Blue Horizons Bracelets}}'; |
||
| 36 | public $defaultConfigurableProductName = '{{Plaid Cotton Shirt}}'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string Xpath to add a Simple product to the cart from the product's page |
||
| 40 | */ |
||
| 41 | |||
| 42 | public $addToCartXpath = '//button[@title="{{Add to Cart}}" and contains(concat(" ",normalize-space(@class)," ")," btn-cart ")]'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string Xpath to add a Simple product to the cart from the category page |
||
| 46 | */ |
||
| 47 | |||
| 48 | public $categoryAddToCartButtonXPathSelector = '//button[@title="{{Add to Cart}}" and @onclick]'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string Xpath to find a product's link on a category page. Used to navigate to the product from the category |
||
| 52 | */ |
||
| 53 | |||
| 54 | public $categoryProductPageXpath = '//h2[@class="product-name"]/descendant::a'; |
||
| 55 | |||
| 56 | public $categorySpecificProductPageXpath = '//h2[@class="product-name"]/descendant::a[.="%s"]'; |
||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string Xpath used after a product has been added to the cart to verify that the product has been added to the cart |
||
| 61 | */ |
||
| 62 | |||
| 63 | public $addToCartSuccessXpath = '//li[@class="success-msg" and contains(., "{{was added to your shopping cart}}")]'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string The base URL of the installation |
||
| 67 | */ |
||
| 68 | |||
| 69 | public $baseUrl = 'http://localhost/'; |
||
| 70 | |||
| 71 | public $myAccountTitle = 'My Account'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array Instructions in an Xpath array syntax to get to the login page. |
||
| 75 | */ |
||
| 76 | |||
| 77 | public $navigateToCustomerPageInstructions = [ |
||
| 78 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@class="account-cart-wrapper"]/descendant::span[.="{{Account}}"]'], |
||
| 79 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@id="header-account"]/descendant::a[@title="{{My Account}}"]'] |
||
| 80 | ]; |
||
| 81 | |||
| 82 | public $cartNavigationInstructions = [ |
||
| 83 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@class="account-cart-wrapper"]/descendant::span[.="{{Account}}"]'], |
||
| 84 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@id="header-account"]/descendant::a[contains(concat(" ",normalize-space(@class)," ")," top-link-cart ")]'] |
||
| 85 | ]; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array Instructions in an Xpath array syntax to get to the start of the checkout page |
||
| 89 | */ |
||
| 90 | |||
| 91 | public $checkoutNavigationInstructions = [ |
||
| 92 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@class="header-minicart"]/descendant::span[.="{{Cart}}"]'], |
||
| 93 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@class="minicart-actions"]/descendant::a[@title="{{Checkout}}"]'] |
||
| 94 | ]; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array Instructions in an Xpath array syntax to get to the customer registration page |
||
| 98 | */ |
||
| 99 | |||
| 100 | public $registrationNavigationInstructions = [ |
||
| 101 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@class="account-cart-wrapper"]/descendant::span[.="{{Account}}"]'], |
||
| 102 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@id="header-account"]/descendant::a[@title="{{Register}}"]'] |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var array Instructions in an Xpath array syntax to get to the customer registration page |
||
| 107 | */ |
||
| 108 | |||
| 109 | public $logoutNavigationInstructions = [ |
||
| 110 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@class="account-cart-wrapper"]/descendant::span[.="{{Account}}"]'], |
||
| 111 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//div[@id="header-account"]/descendant::a[@title="{{Log Out}}"]'] |
||
| 112 | ]; |
||
| 113 | |||
| 114 | public $registerFirstNameXpath = '//input[@id="firstname"]'; |
||
| 115 | public $registerLastNameXpath = '//input[@id="lastname"]'; |
||
| 116 | public $registerEmailXpath = '//input[@id="email_address"]'; |
||
| 117 | public $registerPasswordXpath = '//input[@id="password"]'; |
||
| 118 | public $registerConfirmPasswordXpath = '//input[@id="confirmation"]'; |
||
| 119 | public $registerNewsletterXpath = '//input[@id="is_subscribed"]'; |
||
| 120 | public $registerSubmitXpath = '//button[@type="submit" and @title="{{Register}}"]'; |
||
| 121 | |||
| 122 | public $logoutSuccessXpath = '//div[contains(concat(" ",normalize-space(@class)," ")," page-title ")]/descendant::h1[.="{{You are now logged out}}"]'; |
||
| 123 | |||
| 124 | public $layeredNavigationTestXpath = '//dl[@id="narrow-by-list"]'; |
||
| 125 | |||
| 126 | public $breadCrumbXpath = '//div[@class="breadcrumbs"]'; |
||
| 127 | |||
| 128 | public $productListBaseXpath = '//ol[contains(concat(" ",normalize-space(@class)," ")," products-list ")]/li[%d]'; |
||
| 129 | public $productListDescriptionXpath = '/descendant::div[contains(concat(" ",normalize-space(@class)," ")," desc ")]'; |
||
| 130 | public $productListTitleXpath = '/descendant::h2[@class="product-name"]/a'; |
||
| 131 | public $productListCompareLinkXpath = '/descendant::ul[@class="add-to-links"]/descendant::a[@class="link-compare"]'; |
||
| 132 | public $productListImageXpath = '/descendant::a[@class="product-image"]/img'; |
||
| 133 | public $productListLinkXpath = '/descendant::a[@class="product-image"]'; |
||
| 134 | public $productListOriginalPriceXpath = '/descendant::div[@class="price-box"]/descendant::p[@class="old-price"]/descendant::*[@class="price"]'; |
||
| 135 | public $productListPriceXpath = '/descendant::div[@class="price-box"]/descendant::*[@class="regular-price" or @class="special-price"]/descendant::span[@class="price"]'; |
||
| 136 | public $productListWishlistLinkXpath = '/descendant::ul[@class="add-to-links"]/descendant::a[@class="link-wishlist"]'; |
||
| 137 | public $productListAddToCartLinkXpath = '/descendant::p[@class="action"]/descendant::button[contains(concat(" ",normalize-space(@class)," ")," btn-cart ")]'; |
||
| 138 | |||
| 139 | public $productGridBaseXpath = '//ul[contains(concat(" ",normalize-space(@class)," ")," products-grid ")]/li[%d]'; |
||
| 140 | public $productGridDescriptionXpath = '/*[.="no description in the grid view"]'; |
||
| 141 | public $productGridTitleXpath = '/descendant::h2[@class="product-name"]/a'; |
||
| 142 | public $productGridCompareLinkXpath = '/descendant::ul[@class="add-to-links"]/descendant::a[@class="link-compare"]'; |
||
| 143 | public $productGridImageXpath = '/descendant::a[@class="product-image"]/img'; |
||
| 144 | public $productGridLinkXpath = '/descendant::a[@class="product-image"]'; |
||
| 145 | public $productGridOriginalPriceXpath = '/descendant::div[@class="price-box"]/descendant::p[@class="old-price"]/descendant::*[@class="price"]'; |
||
| 146 | public $productGridPriceXpath = '/descendant::div[@class="price-box"]/descendant::*[@class="regular-price" or @class="special-price"]/descendant::span[@class="price"]'; |
||
| 147 | public $productGridWishlistLinkXpath = '/descendant::ul[@class="add-to-links"]/descendant::a[@class="link-wishlist"]'; |
||
| 148 | public $productGridAddToCartLinkXpath = '/descendant::div[@class="actions"]/descendant::button[contains(concat(" ",normalize-space(@class)," ")," btn-cart ")]'; |
||
| 149 | |||
| 150 | public $productCollectionViewModeXpath = '//p[@class="view-mode"]/strong'; |
||
| 151 | public $productCollectionSortByXpath = '//div[@class="sort-by"]/descendant::option[@selected]'; // We select using the div, not the title because the title may be translated |
||
| 152 | public $productCollectionShowCountXpath = '//div[@class="limiter"]/descendant::option[@selected]'; // dittos |
||
| 153 | public $productCollectionShowCountOptionsXpath = '//div[@class="limiter"]/descendant::option'; |
||
| 154 | public $productCollectionProductCountXpath = '//div[contains(concat(" ",normalize-space(@class)," ")," pager ")]/descendant::p[contains(concat(" ",normalize-space(@class)," ")," amount ")]'; |
||
| 155 | |||
| 156 | public $layeredNavigationBaseXpath = '//div[contains(concat(" ",normalize-space(@class)," ")," block-layered-nav ")]'; |
||
| 157 | |||
| 158 | public $searchInputXpath = '//input[@id="search"]'; |
||
| 159 | public $searchSubmitXpath = '//form[@id="search_mini_form"]/descendant::button[@title="Search"]'; |
||
| 160 | |||
| 161 | public $searchSuggestionTextXpath = '//div[@id="search_autocomplete"]/descendant::li[@title][%d]'; |
||
| 162 | public $searchSuggestionCountXpath = '//div[@id="search_autocomplete"]/descendant::li[@title][%d]/span[@class="amount"]'; |
||
| 163 | |||
| 164 | public $simpleProductQtyXpath = '//input[@id="qty"]'; |
||
| 165 | |||
| 166 | public $configurableProductLabelXpath = '//div[@id="product-options-wrapper"]/descendant::label'; |
||
| 167 | public $configurableSwatchSelectorXpath = '(%s)[%d]/ancestor::dt/following-sibling::dd[1]/descendant::a[%d]'; |
||
| 168 | public $configurableSwatchImgXpath = '(%s)[%d]/ancestor::dt/following-sibling::dd[1]/descendant::a[%d]/descendant::img'; |
||
| 169 | public $configurableSwatchNotAvailableXpath = '(%s)[%d]/ancestor::dt/following-sibling::dd[1]/descendant::a[%d]/ancestor::li[contains(concat(" ",normalize-space(@class)," ")," not-available ")]'; |
||
| 170 | public $configurableProductOptionXpath = '(%s)[%d]/ancestor::dt/following-sibling::dd[1]/descendant::option[starts-with(., "%s")]'; |
||
| 171 | public $configurableSwatchOptionLabelAttributeName = 'title'; |
||
| 172 | |||
| 173 | public $viewModeAttributeName = 'class'; |
||
| 174 | |||
| 175 | public $breadCrumbMemberXpath = '/descendant::a[concat(" ",normalize-space(.)," ")=" {{%s}} "]'; |
||
| 176 | public $breadCrumbSelectorXpath = '/descendant::a[%d]'; |
||
| 177 | |||
| 178 | public $layeredNavigationFilterNameXpath = '//dl[@id="narrow-by-list"]/dt'; |
||
| 179 | |||
| 180 | public $layeredNavigationFilterTypesXpath = '//dt[.="%s"]/following-sibling::dd[1]/descendant::li'; |
||
| 181 | public $layeredNavigationFilterLinkXpath = '//dt[.="%s"]/following-sibling::dd[1]/descendant::li/descendant::a'; |
||
| 182 | public $layeredNavigationFilterNameElementXpath = '//dl[@id="narrow-by-list"]/dt[normalize-space(.) = "%s"]'; |
||
| 183 | public $layeredNavigationSwatchFilterTypesXpath = '//dt[.="%s"]/following-sibling::dd[1]/descendant::li'; |
||
| 184 | public $layeredNavigationSwatchAppliesXpath = '//dt[.="%s"]/following-sibling::dd[1]/descendant::ol[contains(concat(" ",normalize-space(@class)," ")," configurable-swatch-list ")]'; |
||
| 185 | public $layeredNavigationSwatchTitleAttribute = 'title'; |
||
| 186 | |||
| 187 | public $storeSwitcherInstructionsXpath = [ |
||
| 188 | [\Magium\WebDriver\WebDriver::INSTRUCTION_MOUSE_CLICK, '//select[@id="select-language"]/descendant::option[contains(@value,"___store=%s")]'], |
||
| 189 | ]; |
||
| 190 | |||
| 191 | public function getCustomerThemeClass() |
||
| 195 | |||
| 196 | public function getCheckoutThemeClass() |
||
| 200 | |||
| 201 | } |
||
| 202 |
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.