Complex classes like Field 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 Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class Field { |
||
9 | |||
10 | /** |
||
11 | * The parent form. |
||
12 | * |
||
13 | * @var \Helmut\Forms\Form |
||
14 | */ |
||
15 | protected $form; |
||
16 | |||
17 | /** |
||
18 | * The type of the field. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | public $type; |
||
23 | |||
24 | /** |
||
25 | * The name of the field. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | public $name; |
||
30 | |||
31 | /** |
||
32 | * The unique id of the field. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | public $id; |
||
37 | |||
38 | /** |
||
39 | * The label property of the field. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $label; |
||
44 | |||
45 | /** |
||
46 | * The default value of the field. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $default; |
||
51 | |||
52 | /** |
||
53 | * Field is required. |
||
54 | * |
||
55 | * @var boolean |
||
56 | */ |
||
57 | protected $required = false; |
||
58 | |||
59 | /** |
||
60 | * An array of validation methods. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $validations = []; |
||
65 | |||
66 | /** |
||
67 | * An array of validation errors. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $errors = []; |
||
72 | |||
73 | /** |
||
74 | * Create a field instance. |
||
75 | * |
||
76 | * @param \Helmut\Forms\Form $form |
||
77 | 118 | * @param string $type |
|
78 | * @param string $name |
||
79 | 118 | */ |
|
80 | 118 | public function __construct(\Helmut\Forms\Form $form, $type, $name) |
|
88 | |||
89 | /** |
||
90 | * Return an the value of the field. For fields |
||
91 | * with multiple values return an array of values |
||
92 | * using associative key names. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | abstract public function getValue(); |
||
97 | |||
98 | /** |
||
99 | * Provide the key names of any buttons. Multiple |
||
100 | * buttons may be returned using an array. |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | abstract public function getButtonName(); |
||
105 | |||
106 | /** |
||
107 | * Return array of properties for renderering. |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | abstract public function renderWith(); |
||
112 | |||
113 | /** |
||
114 | * Set field value using provided default. |
||
115 | * |
||
116 | * @param mixed $default |
||
|
|||
117 | * @return void |
||
118 | */ |
||
119 | abstract public function setValueFromDefault(); |
||
120 | |||
121 | /** |
||
122 | * Set value using a model. |
||
123 | * |
||
124 | * @param object $model |
||
125 | * @return void |
||
126 | */ |
||
127 | abstract public function setValueFromModel($model); |
||
128 | |||
129 | /** |
||
130 | * Set value from the request. |
||
131 | * |
||
132 | * @param \Helmut\Forms\Request $request |
||
133 | * @return void |
||
134 | */ |
||
135 | abstract public function setValueFromRequest($request); |
||
136 | |||
137 | /** |
||
138 | * Fill a model with field values. |
||
139 | * |
||
140 | * @param object $model |
||
141 | * @return void |
||
142 | */ |
||
143 | abstract public function fillModelWithValue($model); |
||
144 | |||
145 | /** |
||
146 | * Return if field passes required validation. |
||
147 | * |
||
148 | * @return boolean |
||
149 | */ |
||
150 | abstract public function validateRequired(); |
||
151 | 118 | ||
152 | /** |
||
153 | 118 | * Set a unique id. |
|
154 | 118 | * |
|
155 | 118 | * @return void |
|
156 | */ |
||
157 | public function setId() |
||
162 | 46 | ||
163 | /** |
||
164 | 46 | * Return an array of keys. |
|
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function keys() |
||
172 | 72 | ||
173 | /** |
||
174 | 72 | * Return an array of values. |
|
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function values() |
||
188 | |||
189 | /** |
||
190 | * Return an array of properties to be passed |
||
191 | * to the template during rendering. |
||
192 | 118 | * |
|
193 | * @return array |
||
194 | 118 | */ |
|
195 | public function properties() |
||
203 | |||
204 | 111 | /** |
|
205 | 111 | * Return an array of button keys. |
|
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | public function buttons() |
||
219 | |||
220 | /** |
||
221 | * Set the field value using default. |
||
222 | * |
||
223 | * @return void |
||
224 | 9 | */ |
|
225 | public function setFromDefault() |
||
229 | |||
230 | /** |
||
231 | * Set the field value using request. |
||
232 | * |
||
233 | * @param \Helmut\Forms\Request $request |
||
234 | * @return void |
||
235 | 13 | */ |
|
236 | public function setFromRequest($request) |
||
240 | |||
241 | /** |
||
242 | * Set the field values using a model. |
||
243 | * |
||
244 | * @param object $model |
||
245 | * @return void |
||
246 | */ |
||
247 | public function setFromModel($model) |
||
251 | |||
252 | /** |
||
253 | * Fill a model with field value; |
||
254 | * |
||
255 | * @param object $model |
||
256 | * @return void |
||
257 | */ |
||
258 | public function fillModel($model) |
||
262 | |||
263 | /** |
||
264 | * Return the name. |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | public function getName() |
||
272 | |||
273 | 27 | /** |
|
274 | * Set the label property. |
||
275 | * |
||
276 | * @param string $label |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function setLabel($label) |
||
285 | |||
286 | 45 | /** |
|
287 | * Set required status. |
||
288 | * |
||
289 | * @param boolean $status |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function setRequired($status = true) |
||
298 | |||
299 | /** |
||
300 | * Set the default value. |
||
301 | * |
||
302 | * @param mixed $value |
||
303 | * @return $this |
||
304 | 72 | */ |
|
305 | public function setDefault($value) |
||
311 | |||
312 | /** |
||
313 | * Check if valid. |
||
314 | 28 | * |
|
315 | * @return boolean |
||
316 | 28 | */ |
|
317 | 24 | public function isValid() |
|
321 | |||
322 | 22 | /** |
|
323 | * Check if invalid. |
||
324 | 22 | * |
|
325 | 9 | * @return boolean |
|
326 | */ |
||
327 | public function isInvalid() |
||
331 | |||
332 | /** |
||
333 | 28 | * Perform all validations. |
|
334 | * |
||
335 | * @return void |
||
336 | */ |
||
337 | public function runValidations() |
||
357 | |||
358 | /** |
||
359 | 45 | * Call a validation method. |
|
360 | * |
||
361 | 45 | * @return boolean |
|
362 | */ |
||
363 | public function callValidationMethod($validation, $parameters = []) |
||
377 | |||
378 | /** |
||
379 | 1 | * Check if field is required. |
|
380 | * |
||
381 | 1 | * @return boolean |
|
382 | */ |
||
383 | public function isRequired() |
||
387 | |||
388 | /** |
||
389 | * Check if field has been filled. |
||
390 | * |
||
391 | * @return boolean |
||
392 | */ |
||
393 | public function isNotEmpty() |
||
397 | |||
398 | /** |
||
399 | * Return validation errors. |
||
400 | * |
||
401 | * @return array |
||
402 | 25 | */ |
|
403 | public function errors() |
||
407 | |||
408 | 23 | /** |
|
409 | 17 | * Add a user defined validation error. |
|
410 | * |
||
411 | * @return array |
||
412 | 23 | */ |
|
413 | public function error($message) |
||
419 | |||
420 | /** |
||
421 | * Add an internal validation error. |
||
422 | * |
||
423 | * @param string $method |
||
424 | * @param array $parameters |
||
425 | * @return void |
||
426 | */ |
||
427 | 75 | protected function addValidationError($method, $parameters) |
|
431 | |||
432 | /** |
||
433 | * Remove all internal validation error for method. |
||
434 | * |
||
435 | * @param string $method |
||
436 | * @return void |
||
437 | */ |
||
438 | protected function clearValidationError($method) |
||
442 | |||
443 | /** |
||
444 | * Get the value of a field. |
||
445 | * |
||
446 | * @param string $key |
||
447 | * @return mixed |
||
448 | */ |
||
449 | public function value($key = null) |
||
468 | |||
469 | 14 | /** |
|
470 | * Get the parent form. |
||
471 | * |
||
472 | 14 | * @return \Helmut\Forms\Form |
|
473 | 14 | */ |
|
474 | 10 | public function getForm() |
|
478 | |||
479 | 14 | /** |
|
480 | * Gives this class the ability to set and get any of the |
||
481 | 14 | * properties of the instances that inherit from this class |
|
482 | * using shorthand notation. Call label() instead of |
||
483 | * setLabel() for example. |
||
484 | * |
||
485 | * Also it allows you to specify validations in the same dynamic |
||
486 | * manner like required() or between(1, 10). |
||
487 | * |
||
488 | * @param string $method |
||
489 | * @param array $parameters |
||
490 | * @return mixed |
||
491 | */ |
||
492 | public function __call($method, $parameters) |
||
514 | |||
515 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.