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 EbayEnterprise_Catalog_Helper_Pim 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 EbayEnterprise_Catalog_Helper_Pim, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class EbayEnterprise_Catalog_Helper_Pim |
||
17 | { |
||
18 | const DEFAULT_OPERATION_TYPE = 'Change'; |
||
19 | const NEW_PRODUCT_OPERATION_TYPE = 'Add'; |
||
20 | const MAX_SKU_LENGTH = 15; |
||
21 | const STRING_LIMIT = 4000; |
||
22 | // Config XML path for gift wrapping availability flag |
||
23 | const XML_PATH_ALLOW_GIFT_WRAPPING = 'sales/gift_options/wrapping_allow_items'; |
||
24 | /** |
||
25 | * return a cdata node from a given string value. |
||
26 | * @param string $attrValue |
||
27 | * @param string $attribute |
||
28 | * @param Mage_Catalog_Model_Product $product |
||
29 | * @param DOMDocument $doc |
||
30 | * @return DOMNode|null |
||
|
|||
31 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
32 | */ |
||
33 | public function getValueAsDefault($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
34 | { |
||
35 | if ($attrValue) { |
||
36 | $frag = $doc->createDocumentFragment(); |
||
37 | $frag->appendChild($doc->createElement('Value')) |
||
38 | ->appendChild($this->passString($attrValue, $attribute, $product, $doc)); |
||
39 | return $frag; |
||
40 | } |
||
41 | return null; |
||
42 | } |
||
43 | /** |
||
44 | * call self::createStringNode passing it string truncate to on self::STRING_LIMIT and pass the given DOMDocument |
||
45 | * which will either return DOMNode object or a null value. |
||
46 | * @param string $attrValue |
||
47 | * @param string $attribute |
||
48 | * @param Mage_Catalog_Model_Product $product |
||
49 | * @param DOMDocument $doc |
||
50 | * @return DOMNode|null |
||
51 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
52 | */ |
||
53 | public function passString($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
57 | /** |
||
58 | * De-normalized a given sku by calling EbayEnterprise_Catalog_Helper_Data::denormalizeSku method and then calling |
||
59 | * the self::createStringNode method given the de-normalize sku and the given DOMDocument object in which |
||
60 | * will return a DOMNode object |
||
61 | * @param string $attrValue |
||
62 | * @param string $attribute |
||
63 | * @param Mage_Catalog_Model_Product $product |
||
64 | * @param DOMDocument $doc |
||
65 | * @throws EbayEnterprise_Catalog_Model_Pim_Product_Validation_Exception |
||
66 | * @return DOMNode|null |
||
67 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
68 | */ |
||
69 | public function passSKU($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
70 | { |
||
71 | $catalogId = Mage::helper('eb2ccore')->getConfigModel($product->getStoreId())->catalogId; |
||
72 | $sku = Mage::helper('ebayenterprise_catalog')->denormalizeSku($attrValue, $catalogId); |
||
73 | if (strlen($sku) > self::MAX_SKU_LENGTH) { |
||
74 | throw new EbayEnterprise_Catalog_Model_Pim_Product_Validation_Exception( |
||
75 | sprintf('%s SKU \'%s\' Exceeds max length.', __FUNCTION__, $sku) |
||
76 | ); |
||
77 | } |
||
78 | return $this->createStringNode($sku, $doc); |
||
79 | } |
||
80 | /** |
||
81 | * Pass the string IF it has a value. |
||
82 | * which will either return DOMNode object or a null value. |
||
83 | * @param string $attrValue |
||
84 | * @param string $attribute |
||
85 | * @param Mage_Catalog_Model_Product $product |
||
86 | * @param DOMDocument $doc |
||
87 | * @return DOMNode|null |
||
88 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
89 | */ |
||
90 | public function passStringIf($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
91 | { |
||
92 | if (!empty($attrValue)) { |
||
93 | return $this->passString($attrValue, $attribute, $product, $doc); |
||
94 | } |
||
95 | return null; |
||
96 | } |
||
97 | /** |
||
98 | * round the attrValue to two decimal point by calling the method Mage_Core_Model_Store::roundPrice given the attrValue |
||
99 | * which will return a rounded attrValue, than pass this attrValue to the method self::createTextNode as first parameter |
||
100 | * and the given DOMDocument object as second parameter which will return a DOMNode object |
||
101 | * @param string $attrValue |
||
102 | * @param string $attribute |
||
103 | * @param Mage_Catalog_Model_Product $product |
||
104 | * @param DOMDocument $doc |
||
105 | * @return DOMNode|null |
||
106 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
107 | */ |
||
108 | public function passPrice($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
112 | /** |
||
113 | * Call the Self::createDecimal method given the attrValue which will return a decimal value if the attriValue is numeric |
||
114 | * otherwise will return null if it null pass it to self::createTextNode will also return node but if return an actual |
||
115 | * decimal value a DOMNode object will be returned |
||
116 | * @param string $attrValue |
||
117 | * @param string $attribute |
||
118 | * @param Mage_Catalog_Model_Product $product |
||
119 | * @param DOMDocument $doc |
||
120 | * @return DOMNode|null |
||
121 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
122 | */ |
||
123 | public function passDecimal($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
127 | /** |
||
128 | * the self::createDateTime method is called given the attrValue if it return a valid date time value then the method |
||
129 | * self::createTextNode will return a DOMNode object when invoked with the attrValue and DOMDocument object, however |
||
130 | * if self::createDateTime method return null than the self::createTextNode will return null |
||
131 | * @param string $attrValue |
||
132 | * @param string $attribute |
||
133 | * @param Mage_Catalog_Model_Product $product |
||
134 | * @param DOMDocument $doc |
||
135 | * @return DOMNode|null |
||
136 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
137 | */ |
||
138 | public function passDate($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
142 | /** |
||
143 | * the method self::createInteger will be called given an attrValue it will return an integer value if the attrValue string |
||
144 | * is numeric other null. when it return an integer value this value is then pass to the method self::createTextNode method |
||
145 | * along with the given DOMDocument object in which will return a DOMNode object, but if a null is given to the method |
||
146 | * self:createTextNode it will return null |
||
147 | * @param string $attrValue |
||
148 | * @param string $attribute |
||
149 | * @param Mage_Catalog_Model_Product $product |
||
150 | * @param DOMDocument $doc |
||
151 | * @return DOMNode|null |
||
152 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
153 | */ |
||
154 | public function passInteger($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
158 | /** |
||
159 | * The Yes/No selector control passes 1 or 0 to represent Yes or No respectively. |
||
160 | * the method self::createBool will be invoked in this method given attrValue if the attribute value is the value |
||
161 | * '1' the return value will be 'true' otherwise the return value will be 'false', this value will then passed to |
||
162 | * the method self::createTextNode then return a DOMNode object given the attrValue and DOMDocument object |
||
163 | * @param string $attrValue |
||
164 | * @param string $attribute |
||
165 | * @param Mage_Catalog_Model_Product $product |
||
166 | * @param DOMDocument $doc |
||
167 | * @return DOMNode|null |
||
168 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
169 | */ |
||
170 | public function passYesNoToBool($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
174 | /** |
||
175 | * return a DOMAttr object containing the client id value |
||
176 | * @param string $attrValue |
||
177 | * @param string $attribute |
||
178 | * @param Mage_Catalog_Model_Product $product |
||
179 | * @param DOMDocument $doc |
||
180 | * @return DOMAttr |
||
181 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
182 | */ |
||
183 | public function passGsiClientId($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
184 | { |
||
185 | $domAttribute = $this->_getDomAttr($doc, $attribute); |
||
186 | $domAttribute->value = Mage::helper('ebayenterprise_catalog/feed')->getClientId(); |
||
187 | return $domAttribute; |
||
188 | } |
||
189 | /** |
||
190 | * return DOMAttr object containing the operation type for the product. |
||
191 | * Any products created after the last export run should use the new product |
||
192 | * operation type ("Add"). All other products being exported should use |
||
193 | * the default operation type ("Change"). |
||
194 | * @param string $attrValue |
||
195 | * @param string $attribute |
||
196 | * @param Mage_Catalog_Model_Product $product |
||
197 | * @param DOMDocument $doc |
||
198 | * @return DOMAttr |
||
199 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
200 | */ |
||
201 | public function passOperationType($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
202 | { |
||
203 | $domAttribute = $this->_getDomAttr($doc, $attribute); |
||
204 | $lastRunTime = Mage::helper('ebayenterprise_catalog')->getConfigModel()->pimExportFeedCutoffDate; |
||
205 | $domAttribute->value = strtotime($product->getCreatedAt()) > strtotime($lastRunTime) ? |
||
206 | static::NEW_PRODUCT_OPERATION_TYPE : |
||
207 | static::DEFAULT_OPERATION_TYPE; |
||
208 | return $domAttribute; |
||
209 | } |
||
210 | /** |
||
211 | * return a DOMAttr object containing catalog id value |
||
212 | * @param string $attrValue |
||
213 | * @param string $attribute |
||
214 | * @param Mage_Catalog_Model_Product $product |
||
215 | * @param DOMDocument $doc |
||
216 | * @return DOMAttr |
||
217 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
218 | */ |
||
219 | public function passCatalogId($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
220 | { |
||
221 | $domAttribute = $this->_getDomAttr($doc, $attribute); |
||
222 | $domAttribute->value = Mage::helper('ebayenterprise_catalog/feed')->getCatalogId(); |
||
223 | return $domAttribute; |
||
224 | } |
||
225 | /** |
||
226 | * return a DOMAttr object containing store id value |
||
227 | * @param string $attrValue |
||
228 | * @param string $attribute |
||
229 | * @param Mage_Catalog_Model_Product $product |
||
230 | * @param DOMDocument $doc |
||
231 | * @return DOMAttr |
||
232 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
233 | */ |
||
234 | public function passStoreId($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
235 | { |
||
236 | $domAttribute = $this->_getDomAttr($doc, $attribute); |
||
237 | // get the rom store id based on the product's store view. |
||
238 | $domAttribute->value = Mage::helper('ebayenterprise_catalog/feed')->getStoreId($product->getStoreId()); |
||
239 | return $domAttribute; |
||
240 | } |
||
241 | /** |
||
242 | * given a DOMDocument and attribute name normalize the attribute create a DONAttr |
||
243 | * @param DOMDocument $doc |
||
244 | * @param string $nodeAttribute |
||
245 | * @return DOMAttr |
||
246 | */ |
||
247 | protected function _getDomAttr(DOMDocument $doc, $nodeAttribute) |
||
251 | |||
252 | /** |
||
253 | * given a value if it is null return null otherwise a DOMNode |
||
254 | * |
||
255 | * @param string $value |
||
256 | * @param DOMDocument $doc |
||
257 | * @return DOMNode | null |
||
258 | */ |
||
259 | public function createStringNode($value, DOMDocument $doc) |
||
263 | |||
264 | /** |
||
265 | * given a value if it is null return null otherwise a DOMNode |
||
266 | * @param string $value |
||
267 | * @param DOMDocument $doc |
||
268 | * @return DOMNode | null |
||
269 | */ |
||
270 | public function createTextNode($value, DOMDocument $doc) |
||
274 | /** |
||
275 | * given a string representing date time if the string is not is not empty return and integer date time |
||
276 | * @param string $value |
||
277 | * @return string | null |
||
278 | */ |
||
279 | public function createDateTime($value) |
||
283 | /** |
||
284 | * given a string representing integer value if the string is a numeric value cast it as integer otherwise return null |
||
285 | * @param string $value |
||
286 | * @return int | null |
||
287 | */ |
||
288 | public function createInteger($value) |
||
292 | /** |
||
293 | * given a string representing decimal value if the string is a numeric value cast it as float otherwise return null |
||
294 | * @param string $value |
||
295 | * @return int | null |
||
296 | */ |
||
297 | public function createDecimal($value) |
||
301 | /** |
||
302 | * given a string if it is '1' return 'true' otherwise 'false' |
||
303 | * @param string $value |
||
304 | * @return string |
||
305 | */ |
||
306 | public function createBool($value) |
||
310 | /** |
||
311 | * For a given product, look for a configurable product using that product. |
||
312 | * If a product is used by multiple configurable products, only the first |
||
313 | * configurable product will be returned. |
||
314 | * @param Mage_Catalog_Model_Product $product |
||
315 | * @return Mage_Catalog_Model_Product|null Parent configurable product or null of no product is found |
||
316 | */ |
||
317 | protected function _getParentConfigurableProduct(Mage_Catalog_Model_Product $product) |
||
318 | { |
||
319 | $parentIds = Mage::getResourceSingleton('catalog/product_type_configurable') |
||
320 | ->getParentIdsByChild($product->getId()); |
||
321 | return isset($parentIds[0]) ? Mage::getModel('catalog/product')->load($parentIds[0]) : null; |
||
322 | } |
||
323 | /** |
||
324 | * Get the product to use as the source for style data. The selection of the |
||
325 | * source product will follow these rules: |
||
326 | * - Products used by a configurable product that exists will use the |
||
327 | * configurable product. |
||
328 | * - Products used by a configurable product that does not exist, will have |
||
329 | * no source - return null. This scenario is almost guaranteed to never |
||
330 | * occur by the DB schema and the way the parent product lookup is |
||
331 | * implemented. As it is still technically possible, however, for the |
||
332 | * product to have not been loaded (customization on the load events or |
||
333 | * similar), failing to cover the scenario would cause a catastrophic |
||
334 | * failure of the export and there is minimal logic to cover the scenario, |
||
335 | * handling is included. |
||
336 | * - All other products will get the data from itself. |
||
337 | * @param Mage_Catalog_Model_Product $product Product being exported |
||
338 | * @return Mage_Catalog_Model_Product|null Product to use as source for style data, null of no such product exists |
||
339 | */ |
||
340 | protected function _getStyleSourceProduct(Mage_Catalog_Model_Product $product) |
||
354 | /** |
||
355 | * if $product is configurable return the result of passSKU |
||
356 | * @param string $attrValue |
||
357 | * @param string $attribute |
||
358 | * @param Mage_Catalog_Model_Product $product |
||
359 | * @param DOMDocument $doc |
||
360 | * @return mixed |
||
361 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
362 | */ |
||
363 | public function passStyleId($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
368 | /** |
||
369 | * if $product is a giftcard, return fragment with the child nodes |
||
370 | * of the GiftCard Element |
||
371 | * @param string $attrValue |
||
372 | * @param string $attribute |
||
373 | * @param Mage_Catalog_Model_Product $product |
||
374 | * @param DOMDocument $doc |
||
375 | * @return mixed |
||
376 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
377 | */ |
||
378 | public function passGiftCard($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
398 | /** |
||
399 | * return a fragment containing a product link element for each |
||
400 | * linked product. |
||
401 | * @param string $attrValue |
||
402 | * @param string $attribute |
||
403 | * @param Mage_Catalog_Model_Product $product |
||
404 | * @param DOMDocument $doc |
||
405 | * @return mixed |
||
406 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
407 | */ |
||
408 | public function passProductLinks($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
431 | |||
432 | /** |
||
433 | * build out a product link subtree |
||
434 | * |
||
435 | * @param DOMDocumentFragment $frag |
||
436 | * @param string $type |
||
437 | * @param int $position |
||
438 | * @param DOMNode $value |
||
439 | * @return DOMDocumentFragment |
||
440 | */ |
||
441 | protected function _addProductLink(DOMDocumentFragment $frag, $type, $position, DOMNode $value) |
||
453 | /** |
||
454 | * return a fragment containing the nodes for the product's category links |
||
455 | * or null if there are none. |
||
456 | * @param string $attrValue |
||
457 | * @param string $attribute |
||
458 | * @param Mage_Catalog_Model_Product $product |
||
459 | * @param DOMDocument $doc |
||
460 | * @return mixed |
||
461 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
462 | */ |
||
463 | public function passCategoryLinks($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
484 | /** |
||
485 | * return Y/N when the value evaluates to true/false respectively. |
||
486 | * @param string $attrValue |
||
487 | * @param string $attribute |
||
488 | * @param Mage_Catalog_Model_Product $product |
||
489 | * @param DOMDocument $doc |
||
490 | * @return mixed |
||
491 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
492 | */ |
||
493 | public function passGiftWrap($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
501 | /** |
||
502 | * Pass string as an ISO Country code otherwise null. |
||
503 | * @param string $attrValue |
||
504 | * @param string $attribute |
||
505 | * @param Mage_Catalog_Model_Product $product |
||
506 | * @param DOMDocument $doc |
||
507 | * @return DOMNode|null |
||
508 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
509 | */ |
||
510 | public function passIsoCountryCode($attrValue, $attribute, Mage_Catalog_Model_Product $product, DOMDocument $doc) |
||
515 | } |
||
516 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.