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 |
||
14 | class Sitewards_B2BProfessional_Helper_Data extends Sitewards_B2BProfessional_Helper_Core |
||
15 | { |
||
16 | /** |
||
17 | * Path for the config for extension active status |
||
18 | */ |
||
19 | const CONFIG_EXTENSION_ACTIVE = 'b2bprofessional/generalsettings/active'; |
||
20 | |||
21 | /** |
||
22 | * Path for the config for price block class names |
||
23 | */ |
||
24 | const CONFIG_EXTENSION_PRICE_BLOCKS = 'b2bprofessional/generalsettings/priceblocks'; |
||
25 | |||
26 | /** |
||
27 | * Path for the config for login message |
||
28 | */ |
||
29 | const CONFIG_EXTENSION_LOGIN_MESSAGE = 'b2bprofessional/generalsettings/login_message'; |
||
30 | |||
31 | /** |
||
32 | * Variable for if the extension is active |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | protected $bExtensionActive; |
||
37 | |||
38 | /** |
||
39 | * Variable for the login message |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $sLoginMessage; |
||
44 | |||
45 | /** |
||
46 | * Variable for if the extension is active by category |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $bExtensionActiveByCategory; |
||
51 | |||
52 | /** |
||
53 | * Variable for if the extension is active by customer group |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $bExtensionActiveByCustomerGroup; |
||
58 | |||
59 | /** |
||
60 | * Variable for the extension's price blocks |
||
61 | * |
||
62 | * @var string[] |
||
63 | */ |
||
64 | protected $aPriceBlockClassNames; |
||
65 | |||
66 | /** |
||
67 | * Variable for the add-to-cart blocks' layout names |
||
68 | * |
||
69 | * @var string[] |
||
70 | */ |
||
71 | protected $aAddToCartBlockLayoutNames = array( |
||
72 | 'product.info.addtocart' |
||
73 | ); |
||
74 | |||
75 | /** |
||
76 | * Check to see if the extension is active |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function isExtensionActive() |
||
84 | |||
85 | /** |
||
86 | * Return the login message to be displayed instead of the price block |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getLoginMessage() |
||
94 | |||
95 | /** |
||
96 | * Check to see if the extension is active by category |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | protected function isExtensionActiveByCategory() |
||
109 | |||
110 | /** |
||
111 | * Check to see if the extension is active by user group |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | protected function isExtensionActivatedByCustomerGroup() |
||
124 | |||
125 | /** |
||
126 | * Check to see if the block is a price block |
||
127 | * |
||
128 | * @param Mage_Core_Block_Template $oBlock |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function isBlockPriceBlock($oBlock) |
||
136 | |||
137 | /** |
||
138 | * Check to see if the block is an add-to-cart block |
||
139 | * |
||
140 | * @param Mage_Core_Block_Template $oBlock |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function isBlockAddToCartBlock($oBlock) |
||
148 | |||
149 | /** |
||
150 | * Check to see if it's the add-to-cart block should be hidden |
||
151 | * |
||
152 | * @param Mage_Core_Block_Template $oBlock |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function isAddToCartBlockAndHidden($oBlock) |
||
165 | |||
166 | /** |
||
167 | * Check to see if the given product is active |
||
168 | * - In this case active means product behaves as normal in a magento shop |
||
169 | * |
||
170 | * @param Mage_Catalog_Model_Product $oProduct |
||
171 | * @return bool |
||
172 | */ |
||
173 | View Code Duplication | public function isProductActive(Mage_Catalog_Model_Product $oProduct) |
|
206 | |||
207 | /** |
||
208 | * From an array of category ids check to see if any are enabled via the extension to hide prices |
||
209 | * |
||
210 | * @param int[] $aCategoryIds |
||
211 | * @return bool |
||
212 | */ |
||
213 | View Code Duplication | public function hasEnabledCategories($aCategoryIds) |
|
245 | |||
246 | /** |
||
247 | * Check if the customer is logged in |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | protected function isCustomerLoggedIn() |
||
255 | |||
256 | /** |
||
257 | * Get the price blocks as defined in the xml |
||
258 | * |
||
259 | * @return string[] |
||
260 | */ |
||
261 | protected function getPriceBlocks() |
||
268 | |||
269 | /** |
||
270 | * Get the add-to-cart blocks' layout names |
||
271 | * |
||
272 | * @return string[] |
||
273 | */ |
||
274 | protected function getAddToCartBlockLayoutNames() |
||
278 | } |
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.