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:
Complex classes like AssetFormFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AssetFormFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | abstract class AssetFormFactory implements FormFactory |
||
30 | { |
||
31 | use Extensible; |
||
32 | use Injectable; |
||
33 | use Configurable; |
||
34 | |||
35 | /** |
||
36 | * Insert into HTML content area |
||
37 | */ |
||
38 | const TYPE_INSERT = 'insert'; |
||
39 | |||
40 | /** |
||
41 | * Select file by ID only |
||
42 | */ |
||
43 | const TYPE_SELECT = 'select'; |
||
44 | |||
45 | /** |
||
46 | * Edit form: Default |
||
47 | */ |
||
48 | const TYPE_ADMIN = 'admin'; |
||
49 | |||
50 | public function __construct() |
||
54 | |||
55 | /** |
||
56 | * @param RequestHandler $controller |
||
57 | * @param string $name |
||
58 | * @param array $context |
||
59 | * @return Form |
||
60 | */ |
||
61 | public function getForm(RequestHandler $controller = null, $name = FormFactory::DEFAULT_NAME, $context = []) |
||
93 | |||
94 | /** |
||
95 | * Get the validator for the form to be built |
||
96 | * |
||
97 | * @param RequestHandler $controller |
||
98 | * @param $formName |
||
99 | * @param $context |
||
100 | * @return RequiredFields |
||
101 | */ |
||
102 | protected function getValidator(RequestHandler $controller = null, $formName, $context = []) |
||
108 | |||
109 | /** |
||
110 | * Get form type from 'type' context |
||
111 | * |
||
112 | * @param array $context |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function getFormType($context) |
||
119 | |||
120 | /** |
||
121 | * Gets the main tabs for the file edit form |
||
122 | * |
||
123 | * @param File $record |
||
124 | * @param array $context |
||
125 | * @return TabSet |
||
126 | */ |
||
127 | protected function getFormFieldTabs($record, $context = []) |
||
137 | |||
138 | /** |
||
139 | * @param File $record |
||
140 | * @return FormAction |
||
141 | */ |
||
142 | View Code Duplication | protected function getSaveAction($record) |
|
150 | |||
151 | /** |
||
152 | * Get delete action, if this record is deletable |
||
153 | * |
||
154 | * @param File $record |
||
155 | * @return FormAction |
||
156 | */ |
||
157 | View Code Duplication | protected function getDeleteAction($record) |
|
167 | |||
168 | /** |
||
169 | * @param RequestHandler $controller |
||
170 | * @param $formName |
||
171 | * @param array $context |
||
172 | * @return FieldList |
||
173 | */ |
||
174 | protected function getFormActions(RequestHandler $controller = null, $formName, $context = []) |
||
190 | |||
191 | /** |
||
192 | * Get fields for this form |
||
193 | * |
||
194 | * @param RequestHandler $controller |
||
195 | * @param string $formName |
||
196 | * @param array $context |
||
197 | * @return FieldList |
||
198 | */ |
||
199 | protected function getFormFields(RequestHandler $controller = null, $formName, $context = []) |
||
223 | |||
224 | /** |
||
225 | * Build popup menu |
||
226 | * |
||
227 | * @param File $record |
||
228 | * @return PopoverField |
||
229 | */ |
||
230 | protected function getPopoverMenu($record) |
||
245 | |||
246 | /** |
||
247 | * Get actions that go into the Popover menu |
||
248 | * |
||
249 | * @param File $record |
||
250 | * @return array |
||
251 | */ |
||
252 | protected function getPopoverActions($record) |
||
261 | |||
262 | /** |
||
263 | * Build "details" formfield tab |
||
264 | * |
||
265 | * @param File $record |
||
266 | * @param array $context |
||
267 | * @return Tab |
||
268 | */ |
||
269 | protected function getFormFieldDetailsTab($record, $context = []) |
||
286 | |||
287 | /** |
||
288 | * Get user-visible "Path" for this record |
||
289 | * |
||
290 | * @param File $record |
||
291 | * @return string |
||
292 | */ |
||
293 | protected function getPath($record, $context = []) |
||
313 | |||
314 | /** |
||
315 | * Build security tab |
||
316 | * |
||
317 | * @param File $record |
||
318 | * @param array $context |
||
319 | * @return Tab |
||
320 | */ |
||
321 | protected function getFormFieldSecurityTab($record, $context = []) |
||
360 | |||
361 | public function getRequiredContext() |
||
365 | } |
||
366 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.