Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class Images |
||
17 | { |
||
18 | /** |
||
19 | * @var LoggerInterface |
||
20 | */ |
||
21 | private $logger; |
||
22 | /** |
||
23 | * @var StockbaseClientFactory |
||
24 | */ |
||
25 | private $stockbaseClientFactory; |
||
26 | /** |
||
27 | * @var StockbaseConfiguration |
||
28 | */ |
||
29 | private $config; |
||
30 | /** |
||
31 | * @var ProductImageResource |
||
32 | */ |
||
33 | private $productImageResource; |
||
34 | /** |
||
35 | * @var ImagesHelper |
||
36 | */ |
||
37 | private $imagesHelper; |
||
38 | /** |
||
39 | * @var ProductCollection |
||
40 | */ |
||
41 | private $productCollection; |
||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $eans = array(); |
||
46 | |||
47 | /** |
||
48 | * Images constructor. |
||
49 | * @param LoggerInterface $logger |
||
50 | * @param StockbaseClientFactory $stockbaseClientFactory |
||
51 | * @param StockbaseConfiguration $config |
||
52 | * @param ProductCollection $productCollection |
||
53 | * @param ProductImageResource $productImageResource |
||
54 | * @param ImagesHelper $imagesHelper |
||
55 | */ |
||
56 | public function __construct( |
||
71 | |||
72 | /** |
||
73 | * Executes the job. |
||
74 | */ |
||
75 | public function execute() |
||
76 | { |
||
77 | // validate configuration: |
||
78 | if (!$this->config->isModuleEnabled() || !$this->config->isImagesSyncCronEnabled()) { |
||
79 | return; |
||
80 | } |
||
81 | // start process: |
||
82 | $this->logger->info('Synchronizing Stockbase images...'); |
||
83 | // get all the eans: |
||
84 | $eans = $this->getEansToProcess(); |
||
85 | try { |
||
86 | // if still need to process eans: |
||
87 | if (count($eans) > 0) { |
||
88 | $client = $this->stockbaseClientFactory->create(); |
||
89 | $images = $client->getImages($eans); |
||
90 | // validate returned images: |
||
91 | View Code Duplication | if (is_array($images->{'Items'}) && count($images->{'Items'}) > 0) { |
|
|
|||
92 | // download and save the images locally: |
||
93 | $newImagesCount = $this->imagesHelper->saveProductImages($images->{'Items'}, $client); |
||
94 | $this->logger->info('New synchronized images: '.$newImagesCount); |
||
95 | } |
||
96 | } |
||
97 | } catch (\Exception $e) { |
||
98 | $this->logger->info('Cron runImageImport error: '.$e->getMessage()); |
||
99 | |||
100 | return; |
||
101 | } |
||
102 | $this->logger->info('Stockbase images synchronization complete.'); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param Product $product |
||
107 | */ |
||
108 | public function getProductEan($product) |
||
120 | |||
121 | /** |
||
122 | * @return array |
||
123 | */ |
||
124 | private function getEansToProcess() |
||
162 | } |
||
163 |
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.