Conditions | 34 |
Paths | > 20000 |
Total Lines | 210 |
Code Lines | 148 |
Lines | 18 |
Ratio | 8.57 % |
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 |
||
149 | public function getProducts(Oledrion_shelf_parameters $parameters) |
||
150 | { |
||
151 | global $vatArray; |
||
152 | $parametersValues = $parameters->getParameters(); |
||
153 | $productType = $parametersValues['productsType']; |
||
154 | $start = $parametersValues['start']; |
||
155 | $limit = $parametersValues['limit']; |
||
156 | $category = $parametersValues['category']; |
||
157 | $sort = $parametersValues['sort']; |
||
158 | $order = $parametersValues['order']; |
||
159 | $excluded = $parametersValues['excluded']; |
||
160 | $withXoopsUser = $parametersValues['withXoopsUser']; |
||
161 | $withRelatedProducts = $parametersValues['withRelatedProducts']; |
||
162 | $withQuantity = $parametersValues['withQuantity']; |
||
163 | $thisMonthOnly = $parametersValues['thisMonthOnly']; |
||
164 | $ret = $xoopsUsersIDs = $users = $relatedProducts = $productsManufacturers = $manufacturersPerProduct = $products = $productsIds = $categoriesIds = $vendorsIds = $manufacturersIds = $manufacturers = $categories = $vendors = array(); |
||
165 | // On commence par récupérer la liste des produits |
||
166 | switch (strtolower($productType)) { |
||
167 | case 'recent': |
||
168 | $products = $this->handlers->h_oledrion_products->getRecentProducts(new Oledrion_parameters(array( |
||
169 | 'start' => $start, |
||
170 | 'limit' => $limit, |
||
171 | 'category' => $category, |
||
172 | 'sort' => $sort, |
||
173 | 'order' => $order, |
||
174 | 'excluded' => $excluded, |
||
175 | 'thisMonthOnly' => $thisMonthOnly |
||
176 | ))); |
||
177 | break; |
||
178 | |||
179 | case 'mostsold': |
||
180 | $tempProductsIds = array(); |
||
181 | $tempProductsIds = $this->handlers->h_oledrion_caddy->getMostSoldProducts($start, $limit, $category, $withQuantity); |
||
182 | if (count($tempProductsIds) > 0) { |
||
183 | $products = $this->handlers->h_oledrion_products->getProductsFromIDs(array_keys($tempProductsIds)); |
||
184 | } |
||
185 | break; |
||
186 | |||
187 | case 'recentlysold': |
||
188 | $tempProductsIds = array(); |
||
189 | $tempProductsIds = $this->handlers->h_oledrion_caddy->getRecentlySoldProducts($start, $limit); |
||
190 | if (count($tempProductsIds) > 0) { |
||
191 | $tempProductsIds = array_unique($tempProductsIds); |
||
192 | } |
||
193 | if (count($tempProductsIds) > 0) { |
||
194 | $products = $this->handlers->h_oledrion_products->getProductsFromIDs(array_keys($tempProductsIds)); |
||
195 | } |
||
196 | break; |
||
197 | |||
198 | View Code Duplication | case 'mostviewed': |
|
199 | $products = $this->handlers->h_oledrion_products->getMostViewedProducts(new Oledrion_parameters(array( |
||
200 | 'start' => $start, |
||
201 | 'limit' => $limit, |
||
202 | 'category' => $category, |
||
203 | 'sort' => $sort, |
||
204 | 'order' => $order |
||
205 | ))); |
||
206 | break; |
||
207 | |||
208 | View Code Duplication | case 'bestrated': |
|
209 | $products = $this->handlers->h_oledrion_products->getBestRatedProducts(new Oledrion_parameters(array( |
||
210 | 'start' => $start, |
||
211 | 'limit' => $limit, |
||
212 | 'category' => $category, |
||
213 | 'sort' => $sort, |
||
214 | 'order' => $order |
||
215 | ))); |
||
216 | break; |
||
217 | |||
218 | View Code Duplication | case 'recommended': |
|
219 | $products = $this->handlers->h_oledrion_products->getRecentRecommended(new Oledrion_parameters(array( |
||
220 | 'start' => $start, |
||
221 | 'limit' => $limit, |
||
222 | 'category' => $category, |
||
223 | 'sort' => $sort, |
||
224 | 'order' => $order |
||
225 | ))); |
||
226 | break; |
||
227 | |||
228 | View Code Duplication | case 'promotional': |
|
229 | $products = $this->handlers->h_oledrion_products->getPromotionalProducts(new Oledrion_parameters(array( |
||
230 | 'start' => $start, |
||
231 | 'limit' => $limit, |
||
232 | 'category' => $category, |
||
233 | 'sort' => $sort, |
||
234 | 'order' => $order |
||
235 | ))); |
||
236 | break; |
||
237 | |||
238 | View Code Duplication | case 'random': |
|
239 | $products = $this->handlers->h_oledrion_products->getRandomProducts(new Oledrion_parameters(array( |
||
240 | 'start' => $start, |
||
241 | 'limit' => $limit, |
||
242 | 'category' => $category, |
||
243 | 'sort' => $sort, |
||
244 | 'order' => $order, |
||
245 | 'thisMonthOnly' => $thisMonthOnly |
||
246 | ))); |
||
247 | } |
||
248 | |||
249 | if (count($products) > 0) { |
||
250 | $productsIds = array_keys($products); |
||
251 | } else { |
||
252 | return $ret; |
||
253 | } |
||
254 | |||
255 | // Recherche des Id des catégories et des vendeurs |
||
256 | foreach ($products as $product) { |
||
257 | $categoriesIds[] = $product->getVar('product_cid'); |
||
258 | $vendorsIds[] = $product->getVar('product_vendor_id'); |
||
259 | if ($withXoopsUser) { |
||
260 | $xoopsUsersIDs[] = $product->getVar('product_submitter'); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | $productsManufacturers = $this->handlers->h_oledrion_productsmanu->getFromProductsIds($productsIds); |
||
265 | // Regroupement des fabricants par produit |
||
266 | View Code Duplication | foreach ($productsManufacturers as $item) { |
|
267 | $manufacturersIds[] = $item->getVar('pm_manu_id'); |
||
268 | $manufacturersPerProduct[$item->getVar('pm_product_id')][] = $item; |
||
269 | } |
||
270 | // On récupère la liste des personnes qui ont soumis les produits |
||
271 | if ($withXoopsUser) { |
||
272 | $users = Oledrion_utils::getUsersFromIds($xoopsUsersIDs); |
||
273 | } |
||
274 | |||
275 | // Il faut récupérer la liste des produits relatifs |
||
276 | if ($withRelatedProducts) { |
||
277 | $relatedProducts = $this->getRelatedProductsFromProductsIds($productsIds); |
||
278 | } |
||
279 | |||
280 | $categoriesIds = array_unique($categoriesIds); |
||
281 | sort($categoriesIds); |
||
282 | |||
283 | $vendorsIds = array_unique($vendorsIds); |
||
284 | sort($vendorsIds); |
||
285 | |||
286 | $manufacturersIds = array_unique($manufacturersIds); |
||
287 | sort($manufacturersIds); |
||
288 | |||
289 | // Récupération des fabricants, des vendeurs et des catégories |
||
290 | if (count($manufacturersIds) > 0) { |
||
291 | $manufacturers = $this->handlers->h_oledrion_manufacturer->getManufacturersFromIds($manufacturersIds); |
||
292 | } |
||
293 | if (count($categoriesIds) > 0) { |
||
294 | $categories = $this->handlers->h_oledrion_cat->getCategoriesFromIds($categoriesIds); |
||
295 | } |
||
296 | if (count($vendorsIds) > 0) { |
||
297 | $vendors = $this->handlers->h_oledrion_vendors->getVendorsFromIds($vendorsIds); |
||
298 | } |
||
299 | |||
300 | $count = 1; |
||
301 | $lastTitle = ''; |
||
302 | foreach ($products as $product) { |
||
303 | $tmp = array(); |
||
304 | $tmp = $product->toArray(); |
||
305 | $lastTitle = $product->getVar('product_title'); |
||
306 | // Le vendeur |
||
307 | if (isset($vendors[$product->getVar('product_vendor_id')])) { |
||
308 | $tmp['product_vendor'] = $vendors[$product->getVar('product_vendor_id')]->toArray(); |
||
309 | } |
||
310 | // La catégorie |
||
311 | if (isset($categories[$product->getVar('product_cid')])) { |
||
312 | $tmp['product_category'] = $categories[$product->getVar('product_cid')]->toArray(); |
||
313 | } |
||
314 | // Les produits relatifs |
||
315 | if ($withRelatedProducts) { |
||
316 | if (isset($relatedProducts[$product->getVar('product_id')])) { |
||
317 | $productsRelatedToThisOne = $relatedProducts[$product->getVar('product_id')]; |
||
318 | foreach ($productsRelatedToThisOne as $oneRelatedProdut) { |
||
319 | $tmp['product_related_products'][] = $oneRelatedProdut->toArray(); |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | // Les fabricants du produit |
||
324 | if (isset($manufacturersPerProduct[$product->getVar('product_id')])) { |
||
325 | $productManufacturers = $manufacturersPerProduct[$product->getVar('product_id')]; |
||
326 | $tmpManufacturersList = array(); |
||
327 | foreach ($productManufacturers as $productManufacturer) { |
||
328 | if (isset($manufacturers[$productManufacturer->getVar('pm_manu_id')])) { |
||
329 | $manufacturer = $manufacturers[$productManufacturer->getVar('pm_manu_id')]; |
||
330 | $tmp['product_manufacturers'][] = $manufacturer->toArray(); |
||
331 | $tmpManufacturersList[] = $manufacturer->getVar('manu_commercialname') . ' ' . $manufacturer->getVar('manu_name'); |
||
332 | } |
||
333 | } |
||
334 | if (count($tmpManufacturersList) > 0) { |
||
335 | $tmp['product_joined_manufacturers'] = implode(OLEDRION_STRING_TO_JOIN_MANUFACTURERS, $tmpManufacturersList); |
||
336 | } |
||
337 | } |
||
338 | |||
339 | // L'utilisateur Xoops (éventuellement) |
||
340 | if ($withXoopsUser && isset($users[$product->getVar('product_submitter')])) { |
||
341 | $thisUser = $users[$product->getVar('product_submitter')]; |
||
342 | if (xoops_trim($thisUser->getVar('name')) != '') { |
||
343 | $name = $thisUser->getVar('name'); |
||
344 | } else { |
||
345 | $name = $thisUser->getVar('uname'); |
||
346 | } |
||
347 | $tmp['product_submiter_name'] = $name; |
||
348 | $userLink = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $thisUser->getVar('uid') . '">' . $name . '</a>'; |
||
349 | $tmp['product_submiter_link'] = $userLink; |
||
350 | } |
||
351 | $tmp['product_count'] = $count; // Compteur pour les templates (pour gérer les colonnes) |
||
352 | $ret[] = $tmp; |
||
353 | ++$count; |
||
354 | } |
||
355 | $ret['lastTitle'] = $lastTitle; |
||
356 | |||
357 | return $ret; |
||
358 | } |
||
359 | } |
||
360 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.