| Conditions | 19 |
| Paths | 9475 |
| Total Lines | 116 |
| 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 154 | public function computeCart(&$cartForTemplate, &$emptyCart, &$shippingAmount, &$commandAmount, &$vatAmount, &$goOn, &$commandAmountTTC, &$discountsDescription) |
||
| 155 | { |
||
| 156 | global $h_bookshop_authors, $h_bookshop_books, $h_bookshop_booksauthors, $h_bookshop_cat, $h_bookshop_vat, $h_bookshop_discounts; |
||
| 157 | if ($this->isCartEmpty()) { // Pas de caddie |
||
| 158 | $emptyCart = true; |
||
| 159 | } else { |
||
| 160 | $emptyCart = false; |
||
| 161 | $tblCaddie = array(); |
||
| 162 | $tblCaddie = isset($_SESSION[CADDY_NAME]) ? $_SESSION[CADDY_NAME] : array(); |
||
| 163 | $caddyCount = count($tblCaddie); |
||
| 164 | if ($caddyCount > 0) { |
||
| 165 | $cpt = 0; |
||
| 166 | $fraisPort = 0; |
||
| 167 | $totalBooksQuantity = 0; |
||
| 168 | $tblVat = array(); |
||
| 169 | if (!is_object($h_bookshop_vat)) { |
||
| 170 | $h_bookshop_vat = xoops_getModuleHandler('bookshop_vat', BOOKSHOP_DIRNAME); |
||
| 171 | } |
||
| 172 | if (!is_object($h_bookshop_books)) { |
||
| 173 | $h_bookshop_books = xoops_getModuleHandler('bookshop_books', BOOKSHOP_DIRNAME); |
||
| 174 | } |
||
| 175 | if (!is_object($h_bookshop_cat)) { |
||
| 176 | $h_bookshop_cat = xoops_getModuleHandler('bookshop_cat', BOOKSHOP_DIRNAME); |
||
| 177 | } |
||
| 178 | if (!is_object($h_bookshop_booksauthors)) { |
||
| 179 | $h_bookshop_booksauthors = xoops_getModuleHandler('bookshop_booksauthors', BOOKSHOP_DIRNAME); |
||
| 180 | } |
||
| 181 | if (!is_object($h_bookshop_authors)) { |
||
| 182 | $h_bookshop_authors = xoops_getModuleHandler('bookshop_authors', BOOKSHOP_DIRNAME); |
||
| 183 | } |
||
| 184 | if (!is_object($h_bookshop_discounts)) { |
||
| 185 | $h_bookshop_discounts = xoops_getModuleHandler('bookshop_discounts', BOOKSHOP_DIRNAME); |
||
| 186 | } |
||
| 187 | $tblVat = $h_bookshop_vat->GetAllVats(); |
||
| 188 | foreach ($tblCaddie as $produit) { |
||
| 189 | $datas = array(); |
||
| 190 | $book_id = $produit['id']; |
||
| 191 | $book_number = $produit['number']; |
||
| 192 | $book_qte = $produit['qty']; |
||
| 193 | $book = null; |
||
| 194 | $book = $h_bookshop_books->get($book_id); |
||
| 195 | $totalBooksQuantity += $produit['qty']; |
||
| 196 | if (!is_object($book)) { |
||
| 197 | exit(_BOOKSHOP_ERROR9); |
||
| 198 | } |
||
| 199 | ++$cpt; |
||
| 200 | if ($cpt == $caddyCount) { // On arrive sur le dernier livre |
||
| 201 | $category = null; |
||
| 202 | $category = $h_bookshop_cat->get($book->getVar('book_cid')); |
||
| 203 | if (is_object($category)) { |
||
| 204 | $goOn = $h_bookshop_cat->GetCategoryLink($category->getVar('cat_cid'), $category->getVar('cat_title')); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | $datas = $book->toArray(); |
||
| 208 | // Recherche des auteurs |
||
| 209 | $tblTmp = $tblTmp2 = $tblAuteurs = $tblJoin = array(); |
||
| 210 | $tblTmp = $h_bookshop_booksauthors->getObjects(new Criteria('ba_book_id', $book_id, '='), true); |
||
| 211 | foreach ($tblTmp as $item) { |
||
| 212 | if ($item->getVar('ba_type') == 1) { |
||
| 213 | $tblTmp2[] = $item->getVar('ba_auth_id'); // Que les auteurs |
||
| 214 | } |
||
| 215 | } |
||
| 216 | $tblAuteurs = $h_bookshop_authors->getObjects(new Criteria('auth_id', '(' . implode(',', $tblTmp2) . ')', 'IN'), true); |
||
| 217 | foreach ($tblAuteurs as $item) { |
||
| 218 | $tblJoin[] = $item->getVar('auth_firstname') . ' ' . $item->getVar('auth_name'); |
||
| 219 | } |
||
| 220 | if (count($tblJoin) > 0) { |
||
| 221 | $datas['book_joined_authors'] = implode(', ', $tblJoin); |
||
| 222 | } |
||
| 223 | $datas['book_number'] = $book_number; |
||
| 224 | |||
| 225 | // Calculs "financiers" *************************************************************************** |
||
| 226 | if ($book->getVar('book_discount_price') > 0) { |
||
| 227 | $ht = (float)$book->getVar('book_discount_price'); |
||
| 228 | } else { |
||
| 229 | $ht = (float)$book->getVar('book_price'); |
||
| 230 | } |
||
| 231 | $htDiscounted = $ht; |
||
| 232 | $fraisPortLivre = (float)$book->getVar('book_shipping_price'); |
||
| 233 | $h_bookshop_discounts->applyDiscountOnEachBook($book_id, $htDiscounted, $discountsDescription, $book_qte, $fraisPortLivre); |
||
| 234 | $prixReelHT = $htDiscounted * $book_qte; |
||
| 235 | $vatRate = $tblVat[$book->getVar('book_vat_id')]->getVar('vat_rate'); |
||
| 236 | $montantTVA = bookshop_getVAT($prixReelHT, $vatRate); |
||
| 237 | $fraisPort = $fraisPortLivre * $book_qte; |
||
| 238 | $totalTTC = $prixReelHT + $montantTVA + $fraisPort; |
||
| 239 | |||
| 240 | $datas['book_price_normal_ht'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $ht); |
||
| 241 | $datas['book_price_discounted_ht'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $htDiscounted); |
||
| 242 | $datas['book_qty'] = $book_qte; |
||
| 243 | $datas['book_price_ht'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $prixReelHT); |
||
| 244 | $datas['book_vat_rate'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $vatRate); |
||
| 245 | $datas['book_vat_amount'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $montantTVA); |
||
| 246 | $datas['book_shipping_amount'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $fraisPort); |
||
| 247 | $datas['book_price_ttc'] = sprintf('%0.' . bookshop_getmoduleoption('decimals_count') . 'f', $totalTTC); |
||
| 248 | $cartForTemplate[] = $datas; |
||
| 249 | |||
| 250 | // Les cumuls (dans les variables "globales") |
||
| 251 | $shippingAmount += $fraisPort; |
||
| 252 | $commandAmount += $prixReelHT; |
||
| 253 | $vatAmount += $montantTVA; |
||
| 254 | } |
||
| 255 | // Règles sur TOUS les livres |
||
| 256 | $h_bookshop_discounts->applyDiscountOnAllBooks($commandAmount, $discountsDescription, $totalBooksQuantity); |
||
| 257 | |||
| 258 | // Règles sur les frais de port |
||
| 259 | $h_bookshop_discounts->applyDiscountOnShipping($shippingAmount, $discountsDescription, $totalBooksQuantity); |
||
| 260 | |||
| 261 | // Règles (n°2) sur les frais de port |
||
| 262 | $h_bookshop_discounts->applyDiscountOnShipping2($shippingAmount, $commandAmount, $discountsDescription, $totalBooksQuantity); |
||
| 263 | |||
| 264 | // Règles sur le montant global de la commande |
||
| 265 | $commandAmountTTC = $shippingAmount + $commandAmount + $vatAmount; |
||
| 266 | $h_bookshop_discounts->applyDiscountOnCommand($commandAmountTTC, $discountsDescription); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 422 |
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.