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 validates. |
||
147 | * |
||
148 | * @return boolean |
||
149 | */ |
||
150 | abstract public function validate(); |
||
151 | 118 | ||
152 | /** |
||
153 | 118 | * Return if field validates required. |
|
154 | 118 | * |
|
155 | 118 | * @return boolean |
|
156 | */ |
||
157 | abstract public function validateRequired(); |
||
158 | |||
159 | /** |
||
160 | * Set a unique id. |
||
161 | * |
||
162 | 46 | * @return void |
|
163 | */ |
||
164 | 46 | public function setId() |
|
169 | |||
170 | /** |
||
171 | * Return an array of keys. |
||
172 | 72 | * |
|
173 | * @return array |
||
174 | 72 | */ |
|
175 | public function keys() |
||
179 | |||
180 | /** |
||
181 | * Return an array of values. |
||
182 | 45 | * |
|
183 | * @return array |
||
184 | 45 | */ |
|
185 | public function values() |
||
195 | |||
196 | /** |
||
197 | * Return an array of properties to be passed |
||
198 | * to the template during rendering. |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 111 | public function properties() |
|
210 | |||
211 | /** |
||
212 | * Return an array of button keys. |
||
213 | 7 | * |
|
214 | * @return array |
||
215 | 7 | */ |
|
216 | 7 | public function buttons() |
|
226 | 9 | ||
227 | 9 | /** |
|
228 | * Set the field value using default. |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | public function setFromDefault() |
||
236 | |||
237 | 13 | /** |
|
238 | 13 | * Set the field value using request. |
|
239 | * |
||
240 | * @param \Helmut\Forms\Request $request |
||
241 | * @return void |
||
242 | */ |
||
243 | public function setFromRequest($request) |
||
247 | |||
248 | /** |
||
249 | * Set the field values using a model. |
||
250 | * |
||
251 | * @param object $model |
||
252 | * @return void |
||
253 | */ |
||
254 | public function setFromModel($model) |
||
258 | |||
259 | /** |
||
260 | * Fill a model with field value; |
||
261 | * |
||
262 | * @param object $model |
||
263 | * @return void |
||
264 | */ |
||
265 | public function fillModel($model) |
||
269 | 27 | ||
270 | /** |
||
271 | 27 | * Return the name. |
|
272 | * |
||
273 | 27 | * @return string |
|
274 | */ |
||
275 | public function getName() |
||
279 | |||
280 | /** |
||
281 | * Set the label property. |
||
282 | 45 | * |
|
283 | * @param string $label |
||
284 | 45 | * @return $this |
|
285 | */ |
||
286 | 45 | public function setLabel($label) |
|
292 | |||
293 | /** |
||
294 | 72 | * Set required status. |
|
295 | * |
||
296 | 72 | * @param boolean $status |
|
297 | * @return $this |
||
298 | */ |
||
299 | public function setRequired($status = true) |
||
305 | |||
306 | 72 | /** |
|
307 | * Set the default value. |
||
308 | * |
||
309 | * @param mixed $value |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setDefault($value) |
||
318 | |||
319 | /** |
||
320 | 28 | * Check if valid. |
|
321 | * |
||
322 | 22 | * @return boolean |
|
323 | */ |
||
324 | 22 | public function isValid() |
|
328 | 22 | ||
329 | 17 | /** |
|
330 | * Check if invalid. |
||
331 | * |
||
332 | * @return boolean |
||
333 | 28 | */ |
|
334 | public function isInvalid() |
||
338 | |||
339 | /** |
||
340 | 27 | * Perform all validations. |
|
341 | * |
||
342 | 27 | * @return void |
|
343 | */ |
||
344 | 27 | public function runValidations() |
|
364 | |||
365 | /** |
||
366 | * Call a validation method. |
||
367 | * |
||
368 | * @return boolean |
||
369 | 28 | */ |
|
370 | public function callValidationMethod($validation, $parameters = []) |
||
384 | |||
385 | /** |
||
386 | * Check if field is required. |
||
387 | * |
||
388 | * @return boolean |
||
389 | */ |
||
390 | public function isRequired() |
||
394 | |||
395 | /** |
||
396 | * Check if field has been filled. |
||
397 | * |
||
398 | * @return boolean |
||
399 | */ |
||
400 | public function isNotEmpty() |
||
404 | 25 | ||
405 | /** |
||
406 | 25 | * Return validation errors. |
|
407 | * |
||
408 | 23 | * @return array |
|
409 | 17 | */ |
|
410 | public function errors() |
||
414 | |||
415 | 2 | /** |
|
416 | * Add a user defined validation error. |
||
417 | 2 | * |
|
418 | 2 | * @return array |
|
419 | */ |
||
420 | public function error($message) |
||
426 | |||
427 | 75 | /** |
|
428 | * Add an internal validation error. |
||
429 | 75 | * |
|
430 | * @param string $method |
||
431 | * @param array $parameters |
||
432 | * @return void |
||
433 | */ |
||
434 | protected function addValidationError($method, $parameters) |
||
438 | |||
439 | /** |
||
440 | * Remove all internal validation error for method. |
||
441 | * |
||
442 | * @param string $method |
||
443 | * @return void |
||
444 | */ |
||
445 | protected function clearValidationError($method) |
||
449 | |||
450 | /** |
||
451 | * Get the value of a field. |
||
452 | * |
||
453 | * @param string $key |
||
454 | * @return mixed |
||
455 | 74 | */ |
|
456 | public function value($key = null) |
||
475 | |||
476 | /** |
||
477 | 14 | * Translate a specific key. |
|
478 | * |
||
479 | 14 | * @param string $key |
|
480 | * @return string |
||
481 | 14 | */ |
|
482 | public function translate($key) |
||
486 | |||
487 | /** |
||
488 | * Get the parent form. |
||
489 | * |
||
490 | * @return \Helmut\Forms\Form |
||
491 | */ |
||
492 | public function getForm() |
||
496 | |||
497 | /** |
||
498 | * Gives this class the ability to set and get any of the |
||
499 | * properties of the instances that inherit from this class |
||
500 | * using shorthand notation. Call label() instead of |
||
501 | * setLabel() for example. |
||
502 | * |
||
503 | * Also it allows you to specify validations in the same dynamic |
||
504 | * manner like required() or between(1, 10). |
||
505 | * |
||
506 | * @param string $method |
||
507 | * @param array $parameters |
||
508 | * @return mixed |
||
509 | */ |
||
510 | public function __call($method, $parameters) |
||
532 | |||
533 | } |
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.