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 MissingFilesFinderTool extends AbstractTool |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Display the title of the tool on the welcome screen. |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | public function getTitle() |
||
33 | |||
34 | /** |
||
35 | * Display the description of the tool in the welcome screen. |
||
36 | * |
||
37 | * @return string |
||
38 | */ |
||
39 | View Code Duplication | public function getDescription() |
|
46 | |||
47 | /** |
||
48 | * Do the job: analyse Index. |
||
49 | * |
||
50 | * @param array $arguments |
||
51 | * @return string |
||
52 | */ |
||
53 | public function work(array $arguments = []) |
||
82 | |||
83 | /** |
||
84 | * Delete files given as parameter. |
||
85 | * This is a special case as we have a missing file in the file system |
||
86 | * As a result, we can't use $fileObject->delete(); which will |
||
87 | * raise exception "Error while fetching permissions". |
||
88 | * |
||
89 | * @param array $files |
||
90 | * @return void |
||
91 | */ |
||
92 | protected function deleteMissingFilesAction(array $files = []) |
||
111 | |||
112 | /** |
||
113 | * Return a pointer to the database. |
||
114 | * |
||
115 | * @return \Fab\Media\Index\IndexAnalyser|object |
||
116 | */ |
||
117 | protected function getIndexAnalyser() |
||
121 | |||
122 | /** |
||
123 | * @return StorageRepository|object |
||
124 | */ |
||
125 | protected function getStorageRepository() |
||
129 | |||
130 | /** |
||
131 | * Tell whether the tools should be displayed according to the context. |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function isShown() |
||
139 | |||
140 | /** |
||
141 | * @return object|DataService |
||
142 | */ |
||
143 | protected function getDataService(): DataService |
||
147 | |||
148 | } |
||
149 | |||
150 |
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.