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 |
||
22 | class Field |
||
23 | { |
||
24 | /** |
||
25 | * The field name |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $name; |
||
30 | |||
31 | /** |
||
32 | * Options for the field |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $params; |
||
37 | |||
38 | /** |
||
39 | * Unique ID for the field |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $id; |
||
43 | |||
44 | /** |
||
45 | * Show the label or not ? |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $show_label = true; |
||
49 | |||
50 | /** |
||
51 | * The attributes that will be applied to the <input> |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $input_attributes = [ |
||
55 | 'class' => ['elm', 'form-control'], |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * The attributes that will be applied to the <label> |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $label_attributes = [ |
||
63 | 'class' => ['elm'], |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * The attributes that will be applied to the <span> with the label |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $span_attributes = [ |
||
71 | 'class' => ['field-label', 'control-label'], |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * The final content of the field |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $result; |
||
79 | |||
80 | /** |
||
81 | * Is the field required or not ? |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | protected $required = ''; |
||
86 | |||
87 | /** |
||
88 | * The fields type, will be added in <input type="" ... |
||
89 | * |
||
90 | * @var string |
||
91 | */ |
||
92 | protected $type = 'text'; |
||
93 | |||
94 | /** |
||
95 | * The javascript events bound to the field |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | protected $events = []; |
||
100 | |||
101 | /** |
||
102 | * The javascript events bound to the label |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $events_label = []; |
||
106 | |||
107 | protected static $templates; |
||
108 | |||
109 | protected static $config; |
||
110 | |||
111 | /** |
||
112 | * @var \Rocket\UI\Script\JS |
||
113 | */ |
||
114 | protected static $js; |
||
115 | |||
116 | /** |
||
117 | * @var callable |
||
118 | */ |
||
119 | protected static $js_resolver; |
||
120 | |||
121 | /** |
||
122 | * Initializes the field |
||
123 | * @param string $name |
||
124 | * @param array $data |
||
125 | */ |
||
126 | public function __construct($name, $data = []) |
||
143 | |||
144 | /** |
||
145 | * Set the javascript queueing instance callback |
||
146 | * |
||
147 | * @param $callable callable |
||
148 | */ |
||
149 | public static function setJSResolver($callable) |
||
153 | |||
154 | /** |
||
155 | * Get the Javascript queueing instance |
||
156 | * |
||
157 | * @throws \Exception |
||
158 | * @return \Rocket\UI\Script\JS |
||
159 | */ |
||
160 | protected function getJS() |
||
170 | |||
171 | /** |
||
172 | * Get the Javascript queueing instance |
||
173 | * |
||
174 | * @throws \Exception |
||
175 | * @return \Rocket\UI\Forms\ValidatorAdapters\ValidatorInterface |
||
176 | */ |
||
177 | protected function getValidator() |
||
181 | |||
182 | /** |
||
183 | * Get the default values array |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | protected function getDefaults() |
||
209 | |||
210 | /** |
||
211 | * Proper destructor |
||
212 | */ |
||
213 | public function __destruct() |
||
224 | |||
225 | /** |
||
226 | * Adds a jquery event to the field |
||
227 | * @param string $name |
||
228 | * @param string $action |
||
229 | * @param bool $bind_to_attributes |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function event($name, $action, $bind_to_attributes = false) |
||
242 | |||
243 | /** |
||
244 | * Adds a jquery event to the field |
||
245 | * @param string $name |
||
246 | * @param string $action |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function eventLabel($name, $action) |
||
255 | |||
256 | /** |
||
257 | * Change parameters |
||
258 | * |
||
259 | * @param $method |
||
260 | * @param $arguments |
||
261 | * @throws \Exception |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function __call($method, $arguments) |
||
283 | |||
284 | /** |
||
285 | * Set data attributes to the field |
||
286 | * |
||
287 | * @param $key string |
||
288 | * @param $value string|array |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function data($key, $value) |
||
297 | |||
298 | /** |
||
299 | * Checks if it's an error |
||
300 | */ |
||
301 | protected function hasError() |
||
309 | |||
310 | /** |
||
311 | * Is required ? |
||
312 | */ |
||
313 | protected function isRequired() |
||
322 | |||
323 | /** |
||
324 | * Adds default css classes |
||
325 | */ |
||
326 | protected function classes() |
||
350 | |||
351 | /** |
||
352 | * Adds the width to the field |
||
353 | */ |
||
354 | protected function applyWidth() |
||
384 | |||
385 | /** |
||
386 | * Extracts the validation rules to make a javascript validation |
||
387 | */ |
||
388 | protected function inputValidation() |
||
406 | |||
407 | /** |
||
408 | * generate an unique id to use in the field |
||
409 | */ |
||
410 | protected function generateId() |
||
416 | |||
417 | public function template($string, array $args = []) |
||
431 | |||
432 | /** |
||
433 | * Renders the field |
||
434 | * @return string |
||
435 | */ |
||
436 | public function render() |
||
487 | |||
488 | protected function formatErrors($errors) |
||
500 | |||
501 | /** |
||
502 | * Auto render the field |
||
503 | * @return string |
||
504 | */ |
||
505 | public function __toString() |
||
509 | |||
510 | /** |
||
511 | * Renders the script |
||
512 | */ |
||
513 | protected function renderScript() |
||
519 | |||
520 | /** |
||
521 | * Renders the javascript events |
||
522 | */ |
||
523 | protected function renderEvents() |
||
539 | |||
540 | /** |
||
541 | * Renders the title |
||
542 | */ |
||
543 | protected function renderTitle() |
||
563 | |||
564 | /** |
||
565 | * Render the inner field |
||
566 | */ |
||
567 | protected function renderInner() |
||
571 | |||
572 | /** |
||
573 | * Renders the input attributes |
||
574 | */ |
||
575 | protected function inputAttributes() |
||
603 | |||
604 | protected function labelAttributes() |
||
608 | |||
609 | /** |
||
610 | * Take all data attributes and render them to the input item |
||
611 | */ |
||
612 | protected function inputDataAttributes() |
||
618 | |||
619 | /** |
||
620 | * Renders the input tip |
||
621 | */ |
||
622 | protected function inputTip() |
||
628 | |||
629 | /** |
||
630 | * Renders the special attributes |
||
631 | * |
||
632 | * @param array $attr |
||
633 | * @return string |
||
634 | */ |
||
635 | protected function renderAttributes($attr) |
||
656 | } |
||
657 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.