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 |
||
21 | class FileReferenceService |
||
22 | { |
||
23 | /** |
||
24 | * Return all references found in sys_file_reference. |
||
25 | * |
||
26 | * @param File|int $file |
||
27 | * @return array |
||
28 | */ |
||
29 | View Code Duplication | public function findFileReferences($file) |
|
42 | |||
43 | /** |
||
44 | * Return soft image references. |
||
45 | * |
||
46 | * @param File|int $file |
||
47 | * @return array |
||
48 | */ |
||
49 | View Code Duplication | public function findSoftImageReferences($file) |
|
65 | |||
66 | /** |
||
67 | * Return link image references. |
||
68 | * |
||
69 | * @param File|int $file |
||
70 | * @return array |
||
71 | */ |
||
72 | View Code Duplication | public function findSoftLinkReferences($file) |
|
88 | |||
89 | /** |
||
90 | * Count all references found in sys_file_reference. |
||
91 | * |
||
92 | * @param File|int $file |
||
93 | * @return int |
||
94 | */ |
||
95 | View Code Duplication | public function countFileReferences($file) |
|
107 | |||
108 | /** |
||
109 | * Count soft image references. |
||
110 | * |
||
111 | * @param File|int $file |
||
112 | * @return int |
||
113 | */ |
||
114 | View Code Duplication | public function countSoftImageReferences($file) |
|
128 | |||
129 | /** |
||
130 | * Count link image references. |
||
131 | * |
||
132 | * @param File|int $file |
||
133 | * @return int |
||
134 | */ |
||
135 | View Code Duplication | public function countSoftLinkReferences($file) |
|
149 | |||
150 | /** |
||
151 | * Count total reference. |
||
152 | * |
||
153 | * @param File|int $file |
||
154 | * @return int |
||
155 | */ |
||
156 | public function countTotalReferences($file) |
||
164 | |||
165 | /** |
||
166 | * @param string $tableName |
||
167 | * @return object|QueryBuilder |
||
168 | */ |
||
169 | protected function getQueryBuilder($tableName): QueryBuilder |
||
175 | |||
176 | /** |
||
177 | * @return object|DataService |
||
178 | */ |
||
179 | protected function getDataService(): DataService |
||
183 | } |
||
184 |
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.