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