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 |
||
24 | abstract class AssetFormFactory implements FormFactory |
||
25 | { |
||
26 | use Extensible; |
||
27 | use Injectable; |
||
28 | use Configurable; |
||
29 | |||
30 | /** |
||
31 | * Insert into HTML content area |
||
32 | */ |
||
33 | const TYPE_INSERT = 'insert'; |
||
34 | |||
35 | /** |
||
36 | * Select file by ID only |
||
37 | */ |
||
38 | const TYPE_SELECT = 'select'; |
||
39 | |||
40 | /** |
||
41 | * Edit form: Default |
||
42 | */ |
||
43 | const TYPE_ADMIN = 'admin'; |
||
44 | |||
45 | public function __construct() |
||
49 | |||
50 | public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = []) |
||
72 | |||
73 | /** |
||
74 | * Get form type from 'type' context |
||
75 | * |
||
76 | * @param array $context |
||
77 | * @return string |
||
78 | */ |
||
79 | protected function getFormType($context) |
||
83 | |||
84 | /** |
||
85 | * Gets the main tabs for the file edit form |
||
86 | * |
||
87 | * @param File $record |
||
88 | * @return TabSet |
||
89 | */ |
||
90 | protected function getFormFieldTabs($record, $context = []) |
||
96 | |||
97 | /** |
||
98 | * @param File $record |
||
99 | * @return FormAction |
||
100 | */ |
||
101 | View Code Duplication | protected function getSaveAction($record) |
|
109 | |||
110 | /** |
||
111 | * Get delete action, if this record is deletable |
||
112 | * |
||
113 | * @param File $record |
||
114 | * @return FormAction |
||
115 | */ |
||
116 | View Code Duplication | protected function getDeleteAction($record) |
|
126 | |||
127 | protected function getFormActions(Controller $controller, $name, $context = []) |
||
139 | |||
140 | protected function getFormFields(Controller $controller, $name, $context = []) |
||
161 | |||
162 | /** |
||
163 | * Build "details" formfield tab |
||
164 | * |
||
165 | * @param File $record |
||
166 | * @param array $context |
||
167 | * @return Tab |
||
168 | */ |
||
169 | protected function getFormFieldDetailsTab($record, $context = []) |
||
176 | |||
177 | public function getRequiredContext() |
||
181 | } |
||
182 |
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.