Complex classes like FormViewHelperService 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 FormViewHelperService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class FormViewHelperService implements SingletonInterface |
||
45 | { |
||
46 | /** |
||
47 | * @var bool |
||
48 | */ |
||
49 | protected $formContext = false; |
||
50 | |||
51 | /** |
||
52 | * @var FormObject |
||
53 | */ |
||
54 | protected $formObject; |
||
55 | |||
56 | /** |
||
57 | * @var Request |
||
58 | */ |
||
59 | protected $request; |
||
60 | |||
61 | /** |
||
62 | * @var Result |
||
63 | */ |
||
64 | protected $result; |
||
65 | |||
66 | /** |
||
67 | * Reset every state that can be used by this service. |
||
68 | */ |
||
69 | public function resetState() |
||
75 | |||
76 | /** |
||
77 | * Will activate the form context, changing the result returned by the |
||
78 | * function `formContextExists()`. |
||
79 | * |
||
80 | * @return FormViewHelperService |
||
81 | * @throws DuplicateEntryException |
||
82 | */ |
||
83 | public function activateFormContext() |
||
94 | |||
95 | /** |
||
96 | * Returns `true` if the `FormViewHelper` context exists. |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function formContextExists() |
||
104 | |||
105 | /** |
||
106 | * Will loop on the submitted form fields and apply behaviours if their |
||
107 | * configuration contains. |
||
108 | */ |
||
109 | public function applyBehavioursOnSubmittedForm() |
||
133 | |||
134 | /** |
||
135 | * Takes care of injecting data for the form. |
||
136 | * |
||
137 | * If the form was generated using a content object, information about it |
||
138 | * are injected, to be retrieved later to be able for instance to fetch the |
||
139 | * object settings (TypoScript, FlexForm, ...). |
||
140 | */ |
||
141 | public function injectFormRequestData() |
||
164 | |||
165 | /** |
||
166 | * Fetches all data attributes that are bound to the form: fields values, |
||
167 | * validation result and others. |
||
168 | * |
||
169 | * @param DataAttributesAssetHandler $dataAttributesAssetHandler |
||
170 | * @return array |
||
171 | */ |
||
172 | public function getDataAttributes(DataAttributesAssetHandler $dataAttributesAssetHandler) |
||
204 | |||
205 | /** |
||
206 | * Checks the type of every data attribute and formats it if needed. |
||
207 | * |
||
208 | * @param array $dataAttributes |
||
209 | * @return array |
||
210 | */ |
||
211 | protected function formatDataAttributes(array $dataAttributes) |
||
226 | |||
227 | /** |
||
228 | * Checks if the form uses steps, in which case the current step is needed |
||
229 | * in order to display the form. If the step is not found, an exception is |
||
230 | * thrown. |
||
231 | */ |
||
232 | public function checkStepDefinition() |
||
240 | |||
241 | /** |
||
242 | * Will check all the fields that have been added below the form view |
||
243 | * helper: each field that is found in the form definition and is *not* |
||
244 | * supported by the current step will add an error and cancel the form |
||
245 | * rendering. |
||
246 | * |
||
247 | * @param ViewHelperVariableContainer $variableContainer |
||
248 | */ |
||
249 | public function checkStepFields(ViewHelperVariableContainer $variableContainer) |
||
282 | |||
283 | /** |
||
284 | * Returns the list of fields that have been added below the form view |
||
285 | * helper. |
||
286 | * |
||
287 | * @param ViewHelperVariableContainer $variableContainer |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getCurrentFormFieldNames(ViewHelperVariableContainer $variableContainer) |
||
312 | |||
313 | /** |
||
314 | * @return Step|null |
||
315 | */ |
||
316 | public function getCurrentStep() |
||
320 | |||
321 | /** |
||
322 | * @return FormObject |
||
323 | */ |
||
324 | public function getFormObject() |
||
328 | |||
329 | /** |
||
330 | * @param FormObject $formObject |
||
331 | */ |
||
332 | public function setFormObject(FormObject $formObject) |
||
336 | |||
337 | /** |
||
338 | * @param Request $request |
||
339 | */ |
||
340 | public function setRequest(Request $request) |
||
344 | |||
345 | /** |
||
346 | * @return Result |
||
347 | */ |
||
348 | public function getResult() |
||
352 | |||
353 | /** |
||
354 | * @return FormResult |
||
355 | */ |
||
356 | protected function getFormValidationResult() |
||
362 | |||
363 | /** |
||
364 | * @param string $formName |
||
365 | * @return AbstractFormValidator |
||
366 | */ |
||
367 | protected function getFormValidator($formName) |
||
380 | } |
||
381 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.