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 |
||
23 | trait MediaFilesTrait |
||
24 | { |
||
25 | /** |
||
26 | * Upload components to delete files according with their different types. |
||
27 | * |
||
28 | * @var UploadComponentInterface[]|LocalUploadComponent[]|S3UploadComponent[] |
||
29 | */ |
||
30 | protected $tmpUploadComponents = []; |
||
31 | |||
32 | /** |
||
33 | * Delete mediafiles from owner. |
||
34 | * |
||
35 | * @param string $owner |
||
36 | * @param int $ownerId |
||
37 | * @param Module $module |
||
38 | * @param bool $protectMultiplied |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | protected function deleteMediafiles(string $owner, int $ownerId, Module $module, bool $protectMultiplied = true): void |
||
74 | |||
75 | /** |
||
76 | * Find the media model entry. |
||
77 | * |
||
78 | * @param int $id |
||
79 | * |
||
80 | * @throws UnknownMethodException |
||
81 | * @throws NotFoundHttpException |
||
82 | * |
||
83 | * @return Mediafile |
||
84 | */ |
||
85 | View Code Duplication | protected function findMediafileModel(int $id): Mediafile |
|
106 | |||
107 | /** |
||
108 | * Delete mediafile record with files. |
||
109 | * |
||
110 | * @param array|int|string $id |
||
111 | * @param Module $module |
||
112 | * |
||
113 | * @throws InvalidConfigException |
||
114 | * |
||
115 | * @return bool|int |
||
116 | */ |
||
117 | protected function deleteMediafileEntry($id, Module $module) |
||
155 | |||
156 | /** |
||
157 | * Set tmp upload component if not isset. |
||
158 | * |
||
159 | * @param string $storage |
||
160 | * @param string $componentName |
||
161 | * @param Module $module |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | private function setComponentIfNotIsset(string $storage, string $componentName, Module $module): void |
||
171 | } |
||
172 |
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.