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 |
||
16 | class EbayEnterprise_Inventory_Model_Observer |
||
17 | { |
||
18 | /** @var EbayEnterprise_Inventory_Model_Quantity_Service */ |
||
19 | protected $quantityService; |
||
20 | /** @var EbayEnterprise_Inventory_Model_Details_Service */ |
||
21 | protected $detailsService; |
||
22 | /** @var EbayEnterprise_Inventory_Model_Allocation_Service */ |
||
23 | protected $allocationService; |
||
24 | /** @var EbayEnterprise_MageLog_Helper_Data */ |
||
25 | protected $logger; |
||
26 | /** @var EbayEnterprise_MageLog_Helper_Context */ |
||
27 | protected $logContext; |
||
28 | /** @var Mage_Core_Model_App */ |
||
29 | protected $app; |
||
30 | /** @var Mage_Adminhtml_Model_Session_Quote */ |
||
31 | protected $adminQuoteSession; |
||
32 | |||
33 | /** |
||
34 | * @param array $args May contain: |
||
35 | * - quantity_service => EbayEnterprise_Inventory_Model_Quantity_Service |
||
36 | * - details_service => EbayEnterprise_Inventory_Model_Details_Service |
||
37 | * - logger => EbayEnterprise_MageLog_Helper_Data |
||
38 | * - log_context => EbayEnterprise_MageLog_Helper_Context |
||
39 | */ |
||
40 | View Code Duplication | public function __construct(array $args = []) |
|
|
|||
41 | { |
||
42 | list( |
||
43 | $this->quantityService, |
||
44 | $this->detailsService, |
||
45 | $this->allocationService, |
||
46 | $this->logger, |
||
47 | $this->logContext, |
||
48 | $this->app |
||
49 | ) = $this->checkTypes( |
||
50 | $this->nullCoalesce( |
||
51 | $args, |
||
52 | 'quantity_service', |
||
53 | Mage::getModel('ebayenterprise_inventory/quantity_service') |
||
54 | ), |
||
55 | $this->nullCoalesce($args, 'details_service', Mage::getModel('ebayenterprise_inventory/details_service')), |
||
56 | $this->nullCoalesce( |
||
57 | $args, |
||
58 | 'allocation_service', |
||
59 | Mage::getModel('ebayenterprise_inventory/allocation_service') |
||
60 | ), |
||
61 | $this->nullCoalesce($args, 'logger', Mage::helper('ebayenterprise_magelog')), |
||
62 | $this->nullCoalesce($args, 'log_context', Mage::helper('ebayenterprise_magelog/context')), |
||
63 | Mage::app() |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Enforce type checks on constructor init params. |
||
69 | * |
||
70 | * @param EbayEnterprise_Inventory_Model_Quantity_Service |
||
71 | * @param EbayEnterprise_Inventory_Model_Details_Service |
||
72 | * @param EbayEnterprise_Inventory_Model_Allocation_Service |
||
73 | * @param EbayEnterprise_MageLog_Helper_Data |
||
74 | * @param EbayEnterprise_MageLog_Helper_Context |
||
75 | * @param Mage_Core_Model_App |
||
76 | * @return array |
||
77 | */ |
||
78 | protected function checkTypes( |
||
79 | EbayEnterprise_Inventory_Model_Quantity_Service $quantityService, |
||
80 | EbayEnterprise_Inventory_Model_Details_Service $detailsService, |
||
81 | EbayEnterprise_Inventory_Model_Allocation_Service $allocationService, |
||
82 | EbayEnterprise_MageLog_Helper_Data $logger, |
||
83 | EbayEnterprise_MageLog_Helper_Context $logContext, |
||
84 | Mage_Core_Model_App $app |
||
85 | ) { |
||
86 | return func_get_args(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Fill in default values. |
||
91 | * |
||
92 | * @param string |
||
93 | * @param array |
||
94 | * @param mixed |
||
95 | * @return mixed |
||
96 | */ |
||
97 | protected function nullCoalesce(array $arr, $key, $default) |
||
101 | |||
102 | /** |
||
103 | * Before collecting item totals, check that all items |
||
104 | * in the quote are available to be fulfilled. |
||
105 | * |
||
106 | * @param Varien_Event_Observer |
||
107 | * @return self |
||
108 | */ |
||
109 | public function handleBeforeCollectTotals(Varien_Event_Observer $observer) |
||
139 | |||
140 | /** |
||
141 | * Handle admin out of stock Exception thrown from ROM Inventory Service. |
||
142 | * |
||
143 | * @param EbayEnterprise_Inventory_Exception_Quantity_Unavailable_Exception |
||
144 | * @param Mage_Sales_Model_Quote |
||
145 | * @return self |
||
146 | */ |
||
147 | protected function handleAdminOrderException( |
||
160 | |||
161 | /** |
||
162 | * Stash the adminhtml quote session to a class property. |
||
163 | * @return Mage_Adminhtml_Model_Session_Quote |
||
164 | */ |
||
165 | protected function getAdminQuoteSession() |
||
172 | |||
173 | /** |
||
174 | * add estimated shipping information to the item payload |
||
175 | * @param Varien_Event_Observer $observer |
||
176 | * @return self |
||
177 | */ |
||
178 | public function handleEbayEnterpriseOrderCreateItem(Varien_Event_Observer $observer) |
||
189 | |||
190 | /** |
||
191 | * get the correct order item |
||
192 | * @param Mage_Sales_Model_Order_Item |
||
193 | * @return Mage_Sales_Model_Order_Item |
||
194 | */ |
||
195 | protected function getItemForLookup(Mage_Sales_Model_Order_Item $item) |
||
202 | |||
203 | /** |
||
204 | * Inject the ship from address into the tax item payload |
||
205 | * |
||
206 | * @param Varien_Event_Observer |
||
207 | * @return self |
||
208 | */ |
||
209 | public function handleEbayEnterpriseTaxItemShipOrigin(Varien_Event_Observer $observer) |
||
217 | |||
218 | /** |
||
219 | * trigger inventory allocation for the quote |
||
220 | * |
||
221 | * @param Varien_Event_Observer |
||
222 | * @return self |
||
223 | */ |
||
224 | public function handleSalesOrderPlaceBefore(Varien_Event_Observer $observer) |
||
231 | |||
232 | /** |
||
233 | * trigger a rollback if the order fails |
||
234 | * |
||
235 | * @return self |
||
236 | */ |
||
237 | public function handleSalesModelServiceQuoteSubmitFailure() |
||
242 | |||
243 | /** |
||
244 | * Clean-up quantity session after an order was successfully created. |
||
245 | * |
||
246 | * @return self |
||
247 | */ |
||
248 | public function handleEbayEnterpriseOrderCreateSuccessful() |
||
255 | } |
||
256 |
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.