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 as a media object |
||
37 | */ |
||
38 | const TYPE_INSERT_MEDIA = 'insert-media'; |
||
39 | |||
40 | /** |
||
41 | * Insert into HTML content area as a link |
||
42 | */ |
||
43 | const TYPE_INSERT_LINK = 'insert-link'; |
||
44 | |||
45 | /** |
||
46 | * Select file by ID only |
||
47 | */ |
||
48 | const TYPE_SELECT = 'select'; |
||
49 | |||
50 | /** |
||
51 | * Edit form: Default |
||
52 | */ |
||
53 | const TYPE_ADMIN = 'admin'; |
||
54 | |||
55 | public function __construct() |
||
59 | |||
60 | /** |
||
61 | * @param RequestHandler $controller |
||
62 | * @param string $name |
||
63 | * @param array $context |
||
64 | * @return Form |
||
65 | */ |
||
66 | public function getForm(RequestHandler $controller = null, $name = FormFactory::DEFAULT_NAME, $context = []) |
||
98 | |||
99 | /** |
||
100 | * Get the validator for the form to be built |
||
101 | * |
||
102 | * @param RequestHandler $controller |
||
103 | * @param $formName |
||
104 | * @param $context |
||
105 | * @return RequiredFields |
||
106 | */ |
||
107 | protected function getValidator(RequestHandler $controller = null, $formName, $context = []) |
||
113 | |||
114 | /** |
||
115 | * Get form type from 'type' context |
||
116 | * |
||
117 | * @param array $context |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function getFormType($context) |
||
124 | |||
125 | /** |
||
126 | * Gets the main tabs for the file edit form |
||
127 | * |
||
128 | * @param File $record |
||
129 | * @param array $context |
||
130 | * @return TabSet |
||
131 | */ |
||
132 | protected function getFormFieldTabs($record, $context = []) |
||
142 | |||
143 | /** |
||
144 | * @param File $record |
||
145 | * @return FormAction |
||
146 | */ |
||
147 | View Code Duplication | protected function getSaveAction($record) |
|
155 | |||
156 | /** |
||
157 | * Get delete action, if this record is deletable |
||
158 | * |
||
159 | * @param File $record |
||
160 | * @return FormAction |
||
161 | */ |
||
162 | View Code Duplication | protected function getDeleteAction($record) |
|
172 | |||
173 | /** |
||
174 | * @param RequestHandler $controller |
||
175 | * @param $formName |
||
176 | * @param array $context |
||
177 | * @return FieldList |
||
178 | */ |
||
179 | protected function getFormActions(RequestHandler $controller = null, $formName, $context = []) |
||
195 | |||
196 | /** |
||
197 | * Get fields for this form |
||
198 | * |
||
199 | * @param RequestHandler $controller |
||
200 | * @param string $formName |
||
201 | * @param array $context |
||
202 | * @return FieldList |
||
203 | */ |
||
204 | protected function getFormFields(RequestHandler $controller = null, $formName, $context = []) |
||
228 | |||
229 | /** |
||
230 | * Build popup menu |
||
231 | * |
||
232 | * @param File $record |
||
233 | * @return PopoverField |
||
234 | */ |
||
235 | protected function getPopoverMenu($record) |
||
250 | |||
251 | /** |
||
252 | * Get actions that go into the Popover menu |
||
253 | * |
||
254 | * @param File $record |
||
255 | * @return array |
||
256 | */ |
||
257 | protected function getPopoverActions($record) |
||
266 | |||
267 | /** |
||
268 | * Build "details" formfield tab |
||
269 | * |
||
270 | * @param File $record |
||
271 | * @param array $context |
||
272 | * @return Tab |
||
273 | */ |
||
274 | protected function getFormFieldDetailsTab($record, $context = []) |
||
291 | |||
292 | /** |
||
293 | * Get user-visible "Path" for this record |
||
294 | * |
||
295 | * @param File $record |
||
296 | * @param array $context |
||
297 | * @return string |
||
298 | */ |
||
299 | protected function getPath($record, $context = []) |
||
319 | |||
320 | /** |
||
321 | * Build security tab |
||
322 | * |
||
323 | * @param File $record |
||
324 | * @param array $context |
||
325 | * @return Tab |
||
326 | */ |
||
327 | protected function getFormFieldSecurityTab($record, $context = []) |
||
366 | |||
367 | public function getRequiredContext() |
||
371 | } |
||
372 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.