| Conditions | 9 |
| Paths | 92 |
| Total Lines | 51 |
| 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 |
||
| 51 | public function handle() |
||
| 52 | { |
||
| 53 | $ids = $this->option('id'); |
||
| 54 | $debug = $this->getOutput()->isDebug(); |
||
| 55 | |||
| 56 | $this->info('Importing products'); |
||
| 57 | |||
| 58 | $products = count($ids) ? $ids : $this->getProducts(); |
||
| 59 | $bar = $this->output->createProgressBar(count($products)); |
||
| 60 | |||
| 61 | $skippedProducts = new Collection(); |
||
| 62 | |||
| 63 | foreach ($products as $productId) { |
||
|
|
|||
| 64 | try { |
||
| 65 | $rawProduct = $this->getProduct($productId); |
||
| 66 | $product = $this->importProduct($rawProduct); |
||
| 67 | |||
| 68 | if (!$product) { |
||
| 69 | $skippedProducts->add(new Collection([ |
||
| 70 | 'id' => (string) $rawProduct->id, |
||
| 71 | 'reference' => (string) $rawProduct->reference, |
||
| 72 | ])); |
||
| 73 | |||
| 74 | if ($debug) { |
||
| 75 | $this->info("\nPrestashop: Skipped product [{$rawProduct->id}] $rawProduct->reference"); |
||
| 76 | } |
||
| 77 | } else { |
||
| 78 | if ($debug) { |
||
| 79 | $this->info("\nPrestashop: Imported product [{$rawProduct->id}] {$rawProduct->reference}"); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $bar->advance(); |
||
| 84 | } catch (\Exception $e) { |
||
| 85 | if ($debug) { |
||
| 86 | $this->info($e->getMessage()); |
||
| 87 | } |
||
| 88 | $this->error("\nPrestashop: Failed to request product " . (string) $productId); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $bar->finish(); |
||
| 93 | |||
| 94 | $skippedProductsCount = count($skippedProducts); |
||
| 95 | if ($skippedProductsCount) { |
||
| 96 | $this->info("\nPrestashop: Skipped {$skippedProductsCount} products"); |
||
| 97 | $this->table(['id', 'reference'], $skippedProducts->toArray()); |
||
| 98 | } |
||
| 99 | |||
| 100 | return true; |
||
| 101 | } |
||
| 102 | |||
| 150 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.