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 |
||
| 10 | class Mygento_Payture_Helper_Data extends Mage_Core_Helper_Abstract |
||
| 11 | { |
||
| 12 | public function addLog($text) |
||
| 13 | { |
||
| 14 | if (Mage::getStoreConfig('payment/payture/debug')) { |
||
| 15 | Mage::log($text, null, 'payture.log', true); |
||
| 16 | } |
||
| 17 | } |
||
| 18 | |||
| 19 | public function getKey() |
||
| 20 | { |
||
| 21 | return Mage::helper('core')->decrypt(Mage::getStoreConfig('payment/payture/key')); |
||
| 22 | } |
||
| 23 | |||
| 24 | public function getPassword() |
||
| 25 | { |
||
| 26 | return Mage::helper('core')->decrypt(Mage::getStoreConfig('payment/payture/password')); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function sendEmailByOrder($order) |
||
| 30 | { |
||
| 31 | try { |
||
| 32 | $order->sendNewOrderEmail(); |
||
| 33 | } catch (Exception $e) { |
||
| 34 | $this->addLog($e->getMessage()); |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getLink($order_id) |
||
| 39 | { |
||
| 40 | $collection = Mage::getModel('payture/keys')->getCollection(); |
||
| 41 | $collection->addFieldToFilter('orderid', $order_id); |
||
| 42 | if (count($collection) == 0) { |
||
| 43 | $model = Mage::getModel('payture/keys'); |
||
| 44 | $key = strtr(base64_encode(microtime() . $order_id . rand(1, 1048576)), '+/=', '-_,'); |
||
| 45 | $model->setHkey($key); |
||
| 46 | $model->setOrderid($order_id); |
||
| 47 | $model->setSessionid(null); |
||
| 48 | $model->setDate(null); |
||
| 49 | $model->save(); |
||
| 50 | return Mage::getUrl('payture/payment/paynow/', array('_secure' => true, 'order' => $key)); |
||
| 51 | } else { |
||
| 52 | $item = $collection->getFirstItem(); |
||
| 53 | return Mage::getUrl('payture/payment/paynow/', |
||
| 54 | array('_secure' => true, 'order' => $item->getHkey())); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | View Code Duplication | public function decodeid($link) |
|
| 68 | |||
| 69 | public function addtransaction($order) |
||
| 70 | { |
||
| 71 | $orders = Mage::getModel('sales/order_invoice')->getCollection() |
||
| 72 | ->addAttributeToFilter('order_id', array('eq' => $order->getId())); |
||
| 73 | $orders->getSelect()->limit(1); |
||
| 113 | |||
| 114 | private function sendCustomComment($order, $toemail, $comment) |
||
| 139 | |||
| 140 | public function checkTicket($_ticket) |
||
| 149 | |||
| 150 | public function getHost() |
||
| 157 | |||
| 158 | public function getData($url) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * |
||
| 175 | * @param type string |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | public function getConfig($param) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * |
||
| 185 | * @param $entity Mage_Sales_Model_Order | Mage_Sales_Model_Order_Invoice | Mage_Sales_Model_Order_Creditmemo |
||
| 186 | * @return type |
||
| 187 | */ |
||
| 188 | public function getOrderItemsJson($entity) |
||
| 218 | |||
| 219 | } |
||
| 220 |
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.