Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Builder |
||
15 | { |
||
16 | /** |
||
17 | * Previous url key. |
||
18 | */ |
||
19 | const PREVIOUS_URL_KEY = '_previous_'; |
||
20 | |||
21 | /** |
||
22 | * @var mixed |
||
23 | */ |
||
24 | protected $id; |
||
25 | |||
26 | /** |
||
27 | * @var Form |
||
28 | */ |
||
29 | protected $form; |
||
30 | |||
31 | /** |
||
32 | * @var |
||
33 | */ |
||
34 | protected $action; |
||
35 | |||
36 | /** |
||
37 | * @var Collection |
||
38 | */ |
||
39 | protected $fields; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $options = [ |
||
45 | 'enableSubmit' => true, |
||
46 | 'enableReset' => true, |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Modes constants. |
||
51 | */ |
||
52 | const MODE_VIEW = 'view'; |
||
53 | const MODE_EDIT = 'edit'; |
||
54 | const MODE_CREATE = 'create'; |
||
55 | |||
56 | /** |
||
57 | * Form action mode, could be create|view|edit. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $mode = 'create'; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $hiddenFields = []; |
||
67 | |||
68 | /** |
||
69 | * @var Tools |
||
70 | */ |
||
71 | protected $tools; |
||
72 | |||
73 | /** |
||
74 | * Width for label and field. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $width = [ |
||
79 | 'label' => 2, |
||
80 | 'field' => 8, |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * View for this form. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $view = 'admin::form'; |
||
89 | |||
90 | /** |
||
91 | * Form title. |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $title; |
||
96 | |||
97 | /** |
||
98 | * Builder constructor. |
||
99 | * |
||
100 | * @param Form $form |
||
101 | */ |
||
102 | public function __construct(Form $form) |
||
110 | |||
111 | /** |
||
112 | * Setup grid tools. |
||
113 | */ |
||
114 | public function setupTools() |
||
118 | |||
119 | /** |
||
120 | * @return Tools |
||
121 | */ |
||
122 | public function getTools() |
||
126 | |||
127 | /** |
||
128 | * Set the builder mode. |
||
129 | * |
||
130 | * @param string $mode |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | public function setMode($mode = 'create') |
||
138 | |||
139 | /** |
||
140 | * Returns builder is $mode. |
||
141 | * |
||
142 | * @param $mode |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function isMode($mode) |
||
150 | |||
151 | /** |
||
152 | * Set resource Id. |
||
153 | * |
||
154 | * @param $id |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | public function setResourceId($id) |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getResource($slice = null) |
||
177 | |||
178 | /** |
||
179 | * @param int $field |
||
180 | * @param int $label |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setWidth($field = 8, $label = 2) |
||
193 | |||
194 | /** |
||
195 | * Set form action. |
||
196 | * |
||
197 | * @param string $action |
||
198 | */ |
||
199 | public function setAction($action) |
||
203 | |||
204 | /** |
||
205 | * Get Form action. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getAction() |
||
225 | |||
226 | /** |
||
227 | * Set view for this form. |
||
228 | * |
||
229 | * @param string $view |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function setView($view) |
||
239 | |||
240 | /** |
||
241 | * Set title for form. |
||
242 | * |
||
243 | * @param string $title |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function setTitle($title) |
||
253 | |||
254 | /** |
||
255 | * Get fields of this builder. |
||
256 | * |
||
257 | * @return Collection |
||
258 | */ |
||
259 | public function fields() |
||
263 | |||
264 | /** |
||
265 | * Get specify field. |
||
266 | * |
||
267 | * @param string $name |
||
268 | * |
||
269 | * @return mixed |
||
270 | */ |
||
271 | public function field($name) |
||
277 | |||
278 | /** |
||
279 | * If the parant form has rows. |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function hasRows() |
||
287 | |||
288 | /** |
||
289 | * Get field rows of form. |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | public function getRows() |
||
297 | |||
298 | /** |
||
299 | * @return array |
||
300 | */ |
||
301 | public function getHiddenFields() |
||
305 | |||
306 | /** |
||
307 | * @param Field $field |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | public function addHiddenField(Field $field) |
||
315 | |||
316 | /** |
||
317 | * Add or get options. |
||
318 | * |
||
319 | * @param array $options |
||
320 | * |
||
321 | * @return array|null |
||
322 | */ |
||
323 | public function options($options = []) |
||
331 | |||
332 | /** |
||
333 | * Get or set option. |
||
334 | * |
||
335 | * @param string $option |
||
336 | * @param mixed $value |
||
337 | * |
||
338 | * @return $this |
||
339 | */ |
||
340 | public function option($option, $value = null) |
||
350 | |||
351 | /** |
||
352 | * @return string |
||
353 | */ |
||
354 | public function title() |
||
374 | |||
375 | /** |
||
376 | * Determine if form fields has files. |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function hasFile() |
||
390 | |||
391 | /** |
||
392 | * Add field for store redirect url after update or store. |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | protected function addRedirectUrlField() |
||
408 | |||
409 | /** |
||
410 | * Open up a new HTML form. |
||
411 | * |
||
412 | * @param array $options |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | public function open($options = []) |
||
443 | |||
444 | /** |
||
445 | * Close the current form. |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | public function close() |
||
456 | |||
457 | /** |
||
458 | * Submit button of form.. |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | public function submitButton() |
||
484 | |||
485 | /** |
||
486 | * Reset button of form. |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | public function resetButton() |
||
504 | |||
505 | /** |
||
506 | * Remove reserved fields like `id` `created_at` `updated_at` in form fields. |
||
507 | * |
||
508 | * @return void |
||
509 | */ |
||
510 | protected function removeReservedFields() |
||
526 | |||
527 | /** |
||
528 | * Render form. |
||
529 | * |
||
530 | * @return string |
||
531 | */ |
||
532 | public function render() |
||
573 | |||
574 | /** |
||
575 | * @return string |
||
576 | */ |
||
577 | public function renderHeaderTools() |
||
581 | |||
582 | /** |
||
583 | * @return string |
||
584 | */ |
||
585 | public function __toString() |
||
589 | } |
||
590 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.