Conditions | 34 |
Paths | > 20000 |
Total Lines | 227 |
Code Lines | 155 |
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 |
||
183 | public function getProducts(ShelfParameters $parameters) |
||
184 | { |
||
185 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
186 | $productsHandler = new Oledrion\ProductsHandler($db); |
||
187 | $vendorsHandler = new Oledrion\VendorsHandler($db); |
||
188 | $caddyHandler = new Oledrion\CaddyHandler($db); |
||
189 | |||
190 | $parametersValues = $parameters->getParameters(); |
||
191 | $productType = $parametersValues['productsType']; |
||
192 | $start = $parametersValues['start']; |
||
193 | $limit = $parametersValues['limit']; |
||
194 | $category = $parametersValues['category']; |
||
195 | $sort = $parametersValues['sort']; |
||
196 | $order = $parametersValues['order']; |
||
197 | $excluded = $parametersValues['excluded']; |
||
198 | $withXoopsUser = $parametersValues['withXoopsUser']; |
||
199 | $withRelatedProducts = $parametersValues['withRelatedProducts']; |
||
200 | $withQuantity = $parametersValues['withQuantity']; |
||
201 | $thisMonthOnly = $parametersValues['thisMonthOnly']; |
||
202 | $ret = $xoopsUsersIDs = $users = $relatedProducts = $productsManufacturers = $manufacturersPerProduct = $products = $productsIds = $categoriesIds = $vendorsIds = $manufacturersIds = $manufacturers = $categories = $vendors = []; |
||
203 | // On commence par récupérer la liste des produits |
||
204 | switch (mb_strtolower($productType)) { |
||
205 | case 'recent': |
||
206 | |||
207 | $products = $productsHandler->getRecentProducts(new Oledrion\Parameters([ |
||
208 | 'start' => $start, |
||
209 | 'limit' => $limit, |
||
210 | 'category' => $category, |
||
211 | 'sort' => $sort, |
||
212 | 'order' => $order, |
||
213 | 'excluded' => $excluded, |
||
214 | 'thisMonthOnly' => $thisMonthOnly, |
||
215 | ])); |
||
216 | |||
217 | break; |
||
218 | case 'mostsold': |
||
219 | |||
220 | $tempProductsIds = []; |
||
221 | $tempProductsIds = $caddyHandler->getMostSoldProducts($start, $limit, $category, $withQuantity); |
||
222 | if (count($tempProductsIds) > 0) { |
||
223 | $products = $productsHandler->getProductsFromIDs(array_keys($tempProductsIds)); |
||
224 | } |
||
225 | |||
226 | break; |
||
227 | case 'recentlysold': |
||
228 | |||
229 | $tempProductsIds = []; |
||
230 | $tempProductsIds = $caddyHandler->getRecentlySoldProducts($start, $limit); |
||
231 | if (count($tempProductsIds) > 0) { |
||
232 | $tempProductsIds = array_unique($tempProductsIds); |
||
233 | } |
||
234 | if (count($tempProductsIds) > 0) { |
||
235 | $products = $productsHandler->getProductsFromIDs(array_keys($tempProductsIds)); |
||
236 | } |
||
237 | |||
238 | break; |
||
239 | case 'mostviewed': |
||
240 | |||
241 | $products = $productsHandler->getMostViewedProducts(new Oledrion\Parameters([ |
||
242 | 'start' => $start, |
||
243 | 'limit' => $limit, |
||
244 | 'category' => $category, |
||
245 | 'sort' => $sort, |
||
246 | 'order' => $order, |
||
247 | ])); |
||
248 | |||
249 | break; |
||
250 | case 'bestrated': |
||
251 | |||
252 | $products = $productsHandler->getBestRatedProducts(new Oledrion\Parameters([ |
||
253 | 'start' => $start, |
||
254 | 'limit' => $limit, |
||
255 | 'category' => $category, |
||
256 | 'sort' => $sort, |
||
257 | 'order' => $order, |
||
258 | ])); |
||
259 | |||
260 | break; |
||
261 | case 'recommended': |
||
262 | |||
263 | $products = $productsHandler->getRecentRecommended(new Oledrion\Parameters([ |
||
264 | 'start' => $start, |
||
265 | 'limit' => $limit, |
||
266 | 'category' => $category, |
||
267 | 'sort' => $sort, |
||
268 | 'order' => $order, |
||
269 | ])); |
||
270 | |||
271 | break; |
||
272 | case 'promotional': |
||
273 | |||
274 | $products = $productsHandler->getPromotionalProducts(new Oledrion\Parameters([ |
||
275 | 'start' => $start, |
||
276 | 'limit' => $limit, |
||
277 | 'category' => $category, |
||
278 | 'sort' => $sort, |
||
279 | 'order' => $order, |
||
280 | ])); |
||
281 | |||
282 | break; |
||
283 | case 'random': |
||
284 | |||
285 | $products = $productsHandler->getRandomProducts(new Oledrion\Parameters([ |
||
286 | 'start' => $start, |
||
287 | 'limit' => $limit, |
||
288 | 'category' => $category, |
||
289 | 'sort' => $sort, |
||
290 | 'order' => $order, |
||
291 | 'thisMonthOnly' => $thisMonthOnly, |
||
292 | ])); |
||
293 | } |
||
294 | |||
295 | if (count($products) > 0) { |
||
296 | $productsIds = array_keys($products); |
||
297 | } else { |
||
298 | return $ret; |
||
299 | } |
||
300 | |||
301 | // Recherche des Id des catégories et des vendeurs |
||
302 | foreach ($products as $product) { |
||
303 | $categoriesIds[] = $product->getVar('product_cid'); |
||
304 | $vendorsIds[] = $product->getVar('product_vendor_id'); |
||
305 | if ($withXoopsUser) { |
||
306 | $xoopsUsersIDs[] = $product->getVar('product_submitter'); |
||
307 | } |
||
308 | } |
||
309 | |||
310 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
311 | $productsmanuHandler = new Oledrion\ProductsmanuHandler($db); |
||
312 | $categoryHandler = new Oledrion\CategoryHandler($db); |
||
313 | $productsManufacturers = $productsmanuHandler->getFromProductsIds($productsIds); |
||
314 | $vendorsHandler = new Oledrion\VendorsHandler($db); |
||
315 | $manufacturerHandler = new Oledrion\ManufacturerHandler($db); |
||
316 | // Regroupement des fabricants par produit |
||
317 | foreach ($productsManufacturers as $item) { |
||
318 | $manufacturersIds[] = $item->getVar('pm_manu_id'); |
||
319 | $manufacturersPerProduct[$item->getVar('pm_product_id')][] = $item; |
||
320 | } |
||
321 | // On récupère la liste des personnes qui ont soumis les produits |
||
322 | if ($withXoopsUser) { |
||
323 | $users = Oledrion\Utility::getUsersFromIds($xoopsUsersIDs); |
||
324 | } |
||
325 | |||
326 | // Il faut récupérer la liste des produits relatifs |
||
327 | if ($withRelatedProducts) { |
||
328 | $relatedProducts = $this->getRelatedProductsFromProductsIds($productsIds); |
||
329 | } |
||
330 | |||
331 | $categoriesIds = array_unique($categoriesIds); |
||
332 | sort($categoriesIds); |
||
333 | |||
334 | $vendorsIds = array_unique($vendorsIds); |
||
335 | sort($vendorsIds); |
||
336 | |||
337 | $manufacturersIds = array_unique($manufacturersIds); |
||
338 | sort($manufacturersIds); |
||
339 | |||
340 | // Récupération des fabricants, des vendeurs et des catégories |
||
341 | if (count($manufacturersIds) > 0) { |
||
342 | $manufacturers = $manufacturerHandler->getManufacturersFromIds($manufacturersIds); |
||
343 | } |
||
344 | if (count($categoriesIds) > 0) { |
||
345 | //mb $categories = $this->handlers->h_oledrion_cat->getCategoriesFromIds($categoriesIds); |
||
346 | $categories = $categoryHandler->getCategoriesFromIds($categoriesIds); |
||
347 | } |
||
348 | if (count($vendorsIds) > 0) { |
||
349 | $vendors = $vendorsHandler->getVendorsFromIds($vendorsIds); |
||
350 | } |
||
351 | |||
352 | $count = 1; |
||
353 | $lastTitle = ''; |
||
354 | foreach ($products as $product) { |
||
355 | $tmp = []; |
||
356 | $tmp = $product->toArray(); |
||
357 | $lastTitle = $product->getVar('product_title'); |
||
358 | // Le vendeur |
||
359 | if (isset($vendors[$product->getVar('product_vendor_id')])) { |
||
360 | $tmp['product_vendor'] = $vendors[$product->getVar('product_vendor_id')]->toArray(); |
||
361 | } |
||
362 | // La catégorie |
||
363 | if (isset($categories[$product->getVar('product_cid')])) { |
||
364 | $tmp['product_category'] = $categories[$product->getVar('product_cid')]->toArray(); |
||
365 | } |
||
366 | // Les produits relatifs |
||
367 | if ($withRelatedProducts) { |
||
368 | if (isset($relatedProducts[$product->getVar('product_id')])) { |
||
369 | $productsRelatedToThisOne = $relatedProducts[$product->getVar('product_id')]; |
||
370 | foreach ($productsRelatedToThisOne as $oneRelatedProdut) { |
||
371 | $tmp['product_related_products'][] = $oneRelatedProdut->toArray(); |
||
372 | } |
||
373 | } |
||
374 | } |
||
375 | // Les fabricants du produit |
||
376 | if (isset($manufacturersPerProduct[$product->getVar('product_id')])) { |
||
377 | $productManufacturers = $manufacturersPerProduct[$product->getVar('product_id')]; |
||
378 | $tmpManufacturersList = []; |
||
379 | foreach ($productManufacturers as $productManufacturer) { |
||
380 | if (isset($manufacturers[$productManufacturer->getVar('pm_manu_id')])) { |
||
381 | $manufacturer = $manufacturers[$productManufacturer->getVar('pm_manu_id')]; |
||
382 | $tmp['product_manufacturers'][] = $manufacturer->toArray(); |
||
383 | $tmpManufacturersList[] = $manufacturer->getVar('manu_commercialname') . ' ' . $manufacturer->getVar('manu_name'); |
||
384 | } |
||
385 | } |
||
386 | if (count($tmpManufacturersList) > 0) { |
||
387 | $tmp['product_joined_manufacturers'] = implode(OLEDRION_STRING_TO_JOIN_MANUFACTURERS, $tmpManufacturersList); |
||
388 | } |
||
389 | } |
||
390 | |||
391 | // L'utilisateur Xoops (éventuellement) |
||
392 | if ($withXoopsUser && isset($users[$product->getVar('product_submitter')])) { |
||
393 | $thisUser = $users[$product->getVar('product_submitter')]; |
||
394 | if ('' !== xoops_trim($thisUser->getVar('name'))) { |
||
395 | $name = $thisUser->getVar('name'); |
||
396 | } else { |
||
397 | $name = $thisUser->getVar('uname'); |
||
398 | } |
||
399 | $tmp['product_submiter_name'] = $name; |
||
400 | $userLink = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $thisUser->getVar('uid') . '">' . $name . '</a>'; |
||
401 | $tmp['product_submiter_link'] = $userLink; |
||
402 | } |
||
403 | $tmp['product_count'] = $count; // Compteur pour les templates (pour gérer les colonnes) |
||
404 | $ret[] = $tmp; |
||
405 | ++$count; |
||
406 | } |
||
407 | $ret['lastTitle'] = $lastTitle; |
||
408 | |||
409 | return $ret; |
||
410 | } |
||
412 |
This check looks for private methods that have been defined, but are not used inside the class.