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 |
||
26 | class Image implements ImageInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var Filesystem |
||
30 | */ |
||
31 | protected $fileSystem; |
||
32 | |||
33 | /** |
||
34 | * @var UploaderFactory |
||
35 | */ |
||
36 | protected $uploaderFactory; |
||
37 | |||
38 | /** |
||
39 | * @var ResourceConnection |
||
40 | */ |
||
41 | protected $resourceConnection; |
||
42 | |||
43 | /** |
||
44 | * Image constructor. |
||
45 | * |
||
46 | * @param Filesystem $fileSystem |
||
47 | * @param UploaderFactory $uploaderFactory |
||
48 | * @param ResourceConnection $resourceConnection |
||
49 | */ |
||
50 | public function __construct( |
||
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | */ |
||
63 | public function loadImages(AbstractModel $object) |
||
80 | |||
81 | /** |
||
82 | * @param AbstractModel $object |
||
83 | * @return void |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | public function saveImages(AbstractModel $object) |
||
111 | |||
112 | /** |
||
113 | * @param AbstractModel $object |
||
114 | * @param string $file |
||
115 | * @param int $position |
||
116 | * @return void |
||
117 | */ |
||
118 | protected function saveImageRelation(AbstractModel $object, string $file, $position = 0) |
||
130 | |||
131 | /** |
||
132 | * @param AbstractModel $object |
||
133 | * @param string $imageId |
||
134 | * @return void |
||
135 | */ |
||
136 | View Code Duplication | protected function removeImageRelation(AbstractModel $object, string $imageId) |
|
147 | |||
148 | /** |
||
149 | * @param string $imageId |
||
150 | * @param string $position |
||
151 | * @return void |
||
152 | */ |
||
153 | View Code Duplication | protected function updateImagePosition(string $imageId, string $position) |
|
166 | |||
167 | /** |
||
168 | * @param string $input |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function uploadImage(string $input): string |
||
181 | |||
182 | /** |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function getAllowedExtensions(): array |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function getBaseDir(): string |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | protected function getSubDir(): string |
||
211 | } |
||
212 |
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.