| Conditions | 3 |
| Paths | 4 |
| Total Lines | 70 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 114 | public function addTransactionLogEntry(Context $oContext, Order $oOrder = null) |
||
| 115 | { |
||
| 116 | $this->oContext = $oContext; |
||
| 117 | $aRequest = $oContext->getRequest()->getPostValue(); |
||
|
|
|||
| 118 | $sRawStatus = serialize($aRequest); |
||
| 119 | if (!$this->toolkitHelper->isUTF8($sRawStatus)) { |
||
| 120 | $sRawStatus = utf8_encode($sRawStatus); // needed for serializing the array |
||
| 121 | } |
||
| 122 | $sOrderId = $oOrder !== null ? $oOrder->getIncrementId() : ''; |
||
| 123 | $this->getConnection()->insert( |
||
| 124 | $this->getMainTable(), |
||
| 125 | [ |
||
| 126 | 'order_id' => $sOrderId, |
||
| 127 | 'store_id' => $this->storeManager->getStore()->getId(), |
||
| 128 | 'reference' => $this->getParam('reference'), |
||
| 129 | 'txid' => $this->getParam('txid'), |
||
| 130 | 'txaction' => $this->getParam('txaction'), |
||
| 131 | 'sequencenumber' => $this->getParam('sequencenumber'), |
||
| 132 | 'clearingtype' => $this->getParam('clearingtype'), |
||
| 133 | 'txtime' => date('Y-m-d H:i:s', $this->getParam('txtime')), |
||
| 134 | 'price' => $this->getParam('price'), |
||
| 135 | 'balance' => $this->getParam('balance'), |
||
| 136 | 'receivable' => $this->getParam('receivable'), |
||
| 137 | 'currency' => $this->getParam('currency'), |
||
| 138 | 'aid' => $this->getParam('aid'), |
||
| 139 | 'portalid' => $this->getParam('portalid'), |
||
| 140 | 'key' => $this->getParam('key'), |
||
| 141 | 'mode' => $this->getParam('mode'), |
||
| 142 | 'userid' => $this->getParam('userid'), |
||
| 143 | 'customerid' => $this->getParam('customerid'), |
||
| 144 | 'company' => $this->getParam('company'), |
||
| 145 | 'firstname' => $this->getParam('firstname'), |
||
| 146 | 'lastname' => $this->getParam('lastname'), |
||
| 147 | 'street' => $this->getParam('street'), |
||
| 148 | 'zip' => $this->getParam('zip'), |
||
| 149 | 'city' => $this->getParam('city'), |
||
| 150 | 'email' => $this->getParam('email'), |
||
| 151 | 'country' => $this->getParam('country'), |
||
| 152 | 'shipping_company' => $this->getParam('shipping_company'), |
||
| 153 | 'shipping_firstname' => $this->getParam('shipping_firstname'), |
||
| 154 | 'shipping_lastname' => $this->getParam('shipping_lastname'), |
||
| 155 | 'shipping_street' => $this->getParam('shipping_street'), |
||
| 156 | 'shipping_zip' => $this->getParam('shipping_zip'), |
||
| 157 | 'shipping_city' => $this->getParam('shipping_city'), |
||
| 158 | 'shipping_country' => $this->getParam('shipping_country'), |
||
| 159 | 'param' => $this->getParam('param'), |
||
| 160 | 'accessname' => $this->getParam('accessname'), |
||
| 161 | 'accesscode' => $this->getParam('accesscode'), |
||
| 162 | 'bankcountry' => $this->getParam('bankcountry'), |
||
| 163 | 'bankaccount' => $this->getParam('bankaccount'), |
||
| 164 | 'bankcode' => $this->getParam('bankcode'), |
||
| 165 | 'bankaccountholder' => $this->getParam('bankaccountholder'), |
||
| 166 | 'cardexpiredate' => $this->getParam('cardexpiredate'), |
||
| 167 | 'cardtype' => $this->getParam('cardtype'), |
||
| 168 | 'cardpan' => $this->getParam('cardpan'), |
||
| 169 | 'clearing_bankaccountholder' => $this->getParam('clearing_bankaccountholder'), |
||
| 170 | 'clearing_bankaccount' => $this->getParam('clearing_bankaccount'), |
||
| 171 | 'clearing_bankcode' => $this->getParam('clearing_bankcode'), |
||
| 172 | 'clearing_bankname' => $this->getParam('clearing_bankname'), |
||
| 173 | 'clearing_bankbic' => $this->getParam('clearing_bankbic'), |
||
| 174 | 'clearing_bankiban' => $this->getParam('clearing_bankiban'), |
||
| 175 | 'clearing_legalnote' => $this->getParam('clearing_legalnote'), |
||
| 176 | 'clearing_duedate' => $this->getParam('clearing_duedate'), |
||
| 177 | 'clearing_reference' => $this->getParam('clearing_reference'), |
||
| 178 | 'clearing_instructionnote' => $this->getParam('clearing_instructionnote'), |
||
| 179 | 'raw_status' => $sRawStatus, |
||
| 180 | ] |
||
| 181 | ); |
||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 202 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: