Complex classes like FieldAbstract 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 FieldAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class FieldAbstract implements FieldInterface |
||
19 | { |
||
20 | |||
21 | /** @var FormInterface[] $dynamicFormCollection */ |
||
22 | private $dynamicFormCollection; |
||
23 | |||
24 | /** @var FilterCollection $filterCollection */ |
||
25 | private $filterCollection; |
||
26 | |||
27 | /** @var ValidatorCollection $validatorCollection */ |
||
28 | private $validatorCollection; |
||
29 | |||
30 | /** @var ValidatorCollection $validatorCollection */ |
||
31 | private $transformer; |
||
32 | |||
33 | /** @var FieldRendererInterface $renderer */ |
||
34 | private $renderer; |
||
35 | |||
36 | /** @var array $errorMessages */ |
||
37 | private $errorMessages; |
||
38 | |||
39 | /** @var string $customErrorMessage */ |
||
40 | private $customErrorMessage; |
||
41 | |||
42 | /** @var string $label */ |
||
43 | private $label; |
||
44 | |||
45 | /** @var bool $required */ |
||
46 | private $required; |
||
47 | |||
48 | use HasAttributesTrait; |
||
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | abstract public function getTag(): string; |
||
54 | |||
55 | abstract public function init(); |
||
56 | |||
57 | 49 | public function __construct($name, $value = null) |
|
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | 37 | */ |
|
73 | public function getName(): string |
||
77 | |||
78 | /** |
||
79 | * @param string $name |
||
80 | */ |
||
81 | 49 | public function setName(string $name): void |
|
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getId(): ?string |
||
93 | |||
94 | /** |
||
95 | * @param string $id |
||
96 | */ |
||
97 | public function setId(string $id): void |
||
101 | 8 | ||
102 | 8 | /** |
|
103 | * @return string |
||
104 | */ |
||
105 | public function getClass(): string |
||
109 | |||
110 | 1 | /** |
|
111 | * @param string $class |
||
112 | * @return FieldAbstract |
||
113 | */ |
||
114 | public function setClass(string $class): void |
||
118 | |||
119 | 8 | /** |
|
120 | 8 | * @return mixed |
|
121 | */ |
||
122 | public function getValue() |
||
126 | 36 | ||
127 | /** |
||
128 | 36 | * @param string $value |
|
129 | */ |
||
130 | public function setValue($value): void |
||
135 | 29 | ||
136 | /** |
||
137 | 29 | * @param ValidatorInterface $validator |
|
138 | 29 | */ |
|
139 | 29 | public function addValidator(ValidatorInterface $validator): void |
|
143 | |||
144 | /** |
||
145 | * @return ValidatorCollection |
||
146 | 20 | */ |
|
147 | public function getValidators(): ValidatorCollection |
||
151 | |||
152 | /** |
||
153 | * @param FilterInterface $filter |
||
154 | */ |
||
155 | 2 | public function addFilter(FilterInterface $filter): void |
|
159 | |||
160 | /** |
||
161 | * @param FilterInterface $transformer |
||
162 | */ |
||
163 | public function setTransformer(TransformerInterface $transformer): void |
||
167 | 28 | ||
168 | /** |
||
169 | * @return TransformerInterface |
||
170 | */ |
||
171 | public function getTransformer(): TransformerInterface |
||
175 | 1 | ||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function hasTransformer(): bool |
||
183 | |||
184 | 31 | /** |
|
185 | * @return FilterCollection |
||
186 | 31 | */ |
|
187 | 31 | public function getFilters(): FilterCollection |
|
191 | |||
192 | 31 | /** |
|
193 | 31 | * Runs the checkForErrors method for each field, which adds to errorMessages if invalid |
|
194 | * |
||
195 | * @return bool |
||
196 | * @throws Exception If validation of $value is impossible |
||
197 | */ |
||
198 | public function isValid(): bool |
||
212 | 29 | ||
213 | 9 | /** |
|
214 | 9 | * @param ValidatorInterface $validator |
|
215 | */ |
||
216 | 29 | private function checkForErrors(ValidatorInterface $validator): void |
|
224 | |||
225 | 10 | /** |
|
226 | * @throws Exception |
||
227 | */ |
||
228 | private function filterValue(): void |
||
241 | |||
242 | 17 | /** |
|
243 | 17 | * @return array |
|
244 | */ |
||
245 | public function getMessages(): array |
||
249 | |||
250 | 5 | /** |
|
251 | * @return string |
||
252 | 5 | */ |
|
253 | 5 | public function getLabel(): ?string |
|
257 | |||
258 | /** |
||
259 | 4 | * @param string $label |
|
260 | */ |
||
261 | 4 | public function setLabel(string $label): void |
|
265 | |||
266 | /** |
||
267 | 2 | * @param string $message |
|
268 | */ |
||
269 | 2 | public function setCustomErrorMessage(string $message): void |
|
273 | |||
274 | /** |
||
275 | 29 | * @return bool |
|
276 | */ |
||
277 | 29 | public function hasCustomErrorMessage(): bool |
|
281 | |||
282 | /** |
||
283 | * @return string |
||
284 | 33 | */ |
|
285 | public function getCustomErrorMessage(): string |
||
289 | |||
290 | /** |
||
291 | * @return FieldRendererInterface |
||
292 | */ |
||
293 | public function getRenderer(): FieldRendererInterface |
||
297 | |||
298 | 35 | /** |
|
299 | * @param FieldRendererInterface $renderer |
||
300 | */ |
||
301 | public function setRenderer(FieldRendererInterface $renderer): void |
||
305 | 17 | ||
306 | /** |
||
307 | 17 | * If a field is required then it must have a value |
|
308 | 17 | * We add a not empty validator |
|
309 | 17 | * |
|
310 | * @return boolean |
||
311 | */ |
||
312 | 17 | public function isRequired(): bool |
|
316 | 17 | ||
317 | /** |
||
318 | 2 | * @param boolean $required |
|
319 | */ |
||
320 | 2 | public function setRequired(bool $required): void |
|
325 | 1 | ||
326 | 2 | /** |
|
327 | * adds not empty validator |
||
328 | 2 | */ |
|
329 | private function addNotEmptyValidator(): void |
||
334 | |||
335 | 3 | /** |
|
336 | * removes not empty validator |
||
337 | 3 | */ |
|
338 | 3 | private function removeNotEmptyValidator(): void |
|
350 | |||
351 | /** |
||
352 | * @param FormInterface $form |
||
353 | 4 | * @param string $triggerValue |
|
354 | */ |
||
355 | 4 | public function addDynamicForm(FormInterface $form, string $triggerValue): void |
|
359 | |||
360 | /** |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function hasDynamicForms(): bool |
||
367 | |||
368 | /** |
||
369 | * @return FormInterface[] |
||
370 | * @throws Exception |
||
371 | */ |
||
372 | public function getDynamicForms(): array |
||
379 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..