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 |
||
15 | class Builder |
||
16 | { |
||
17 | /** |
||
18 | * Previous url key. |
||
19 | */ |
||
20 | const PREVIOUS_URL_KEY = '_previous_'; |
||
21 | |||
22 | /** |
||
23 | * @var mixed |
||
24 | */ |
||
25 | protected $id; |
||
26 | |||
27 | /** |
||
28 | * @var Form |
||
29 | */ |
||
30 | protected $form; |
||
31 | |||
32 | /** |
||
33 | * @var |
||
34 | */ |
||
35 | protected $action; |
||
36 | |||
37 | /** |
||
38 | * @var Collection |
||
39 | */ |
||
40 | protected $fields; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $options = []; |
||
46 | |||
47 | /** |
||
48 | * Modes constants. |
||
49 | */ |
||
50 | const MODE_EDIT = 'edit'; |
||
51 | const MODE_CREATE = 'create'; |
||
52 | |||
53 | /** |
||
54 | * Form action mode, could be create|view|edit. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $mode = 'create'; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $hiddenFields = []; |
||
64 | |||
65 | /** |
||
66 | * @var Tools |
||
67 | */ |
||
68 | protected $tools; |
||
69 | |||
70 | /** |
||
71 | * @var Footer |
||
72 | */ |
||
73 | protected $footer; |
||
74 | |||
75 | /** |
||
76 | * Width for label and field. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $width = [ |
||
81 | 'label' => 2, |
||
82 | 'field' => 8, |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * View for this form. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $view = 'admin::form'; |
||
91 | |||
92 | /** |
||
93 | * Form title. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $title; |
||
98 | |||
99 | /** |
||
100 | * Builder constructor. |
||
101 | * |
||
102 | * @param Form $form |
||
103 | */ |
||
104 | public function __construct(Form $form) |
||
112 | |||
113 | /** |
||
114 | * Do initialize. |
||
115 | */ |
||
116 | public function init() |
||
121 | |||
122 | /** |
||
123 | * Get form tools instance. |
||
124 | * |
||
125 | * @return Tools |
||
126 | */ |
||
127 | public function getTools() |
||
131 | |||
132 | /** |
||
133 | * Get form footer instance. |
||
134 | * |
||
135 | * @return Footer |
||
136 | */ |
||
137 | public function getFooter() |
||
141 | |||
142 | /** |
||
143 | * Set the builder mode. |
||
144 | * |
||
145 | * @param string $mode |
||
146 | * |
||
147 | * @return void |
||
148 | */ |
||
149 | public function setMode($mode = 'create') |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getMode() |
||
161 | |||
162 | /** |
||
163 | * Returns builder is $mode. |
||
164 | * |
||
165 | * @param $mode |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function isMode($mode) |
||
173 | |||
174 | /** |
||
175 | * Check if is creating resource. |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function isCreating() |
||
183 | |||
184 | /** |
||
185 | * Check if is editing resource. |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function isEditing() |
||
193 | |||
194 | /** |
||
195 | * Set resource Id. |
||
196 | * |
||
197 | * @param $id |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | public function setResourceId($id) |
||
205 | |||
206 | /** |
||
207 | * Get Resource id. |
||
208 | * |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function getResourceId() |
||
215 | |||
216 | /** |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getResource($slice = null) |
||
230 | |||
231 | /** |
||
232 | * @param int $field |
||
233 | * @param int $label |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function setWidth($field = 8, $label = 2) |
||
246 | |||
247 | /** |
||
248 | * Get label and field width. |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | public function getWidth() |
||
256 | |||
257 | /** |
||
258 | * Set form action. |
||
259 | * |
||
260 | * @param string $action |
||
261 | */ |
||
262 | public function setAction($action) |
||
266 | |||
267 | /** |
||
268 | * Get Form action. |
||
269 | * |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getAction() |
||
288 | |||
289 | /** |
||
290 | * Set view for this form. |
||
291 | * |
||
292 | * @param string $view |
||
293 | * |
||
294 | * @return $this |
||
295 | */ |
||
296 | public function setView($view) |
||
302 | |||
303 | /** |
||
304 | * Set title for form. |
||
305 | * |
||
306 | * @param string $title |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function setTitle($title) |
||
316 | |||
317 | /** |
||
318 | * Get fields of this builder. |
||
319 | * |
||
320 | * @return Collection |
||
321 | */ |
||
322 | public function fields() |
||
326 | |||
327 | /** |
||
328 | * Get specify field. |
||
329 | * |
||
330 | * @param string $name |
||
331 | * |
||
332 | * @return mixed |
||
333 | */ |
||
334 | public function field($name) |
||
340 | |||
341 | /** |
||
342 | * If the parant form has rows. |
||
343 | * |
||
344 | * @return bool |
||
345 | */ |
||
346 | public function hasRows() |
||
350 | |||
351 | /** |
||
352 | * Get field rows of form. |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | public function getRows() |
||
360 | |||
361 | /** |
||
362 | * @return array |
||
363 | */ |
||
364 | public function getHiddenFields() |
||
368 | |||
369 | /** |
||
370 | * @param Field $field |
||
371 | * |
||
372 | * @return void |
||
373 | */ |
||
374 | public function addHiddenField(Field $field) |
||
378 | |||
379 | /** |
||
380 | * Add or get options. |
||
381 | * |
||
382 | * @param array $options |
||
383 | * |
||
384 | * @return array|null |
||
385 | */ |
||
386 | public function options($options = []) |
||
394 | |||
395 | /** |
||
396 | * Get or set option. |
||
397 | * |
||
398 | * @param string $option |
||
399 | * @param mixed $value |
||
400 | * |
||
401 | * @return $this |
||
402 | */ |
||
403 | public function option($option, $value = null) |
||
413 | |||
414 | /** |
||
415 | * @return string |
||
416 | */ |
||
417 | public function title() |
||
433 | |||
434 | /** |
||
435 | * Determine if form fields has files. |
||
436 | * |
||
437 | * @return bool |
||
438 | */ |
||
439 | public function hasFile() |
||
449 | |||
450 | /** |
||
451 | * Add field for store redirect url after update or store. |
||
452 | * |
||
453 | * @return void |
||
454 | */ |
||
455 | protected function addRedirectUrlField() |
||
467 | |||
468 | /** |
||
469 | * Open up a new HTML form. |
||
470 | * |
||
471 | * @param array $options |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | public function open($options = []) |
||
502 | |||
503 | /** |
||
504 | * Close the current form. |
||
505 | * |
||
506 | * @return string |
||
507 | */ |
||
508 | public function close() |
||
515 | |||
516 | /** |
||
517 | * Remove reserved fields like `id` `created_at` `updated_at` in form fields. |
||
518 | * |
||
519 | * @return void |
||
520 | */ |
||
521 | protected function removeReservedFields() |
||
537 | |||
538 | /** |
||
539 | * Render form header tools. |
||
540 | * |
||
541 | * @return string |
||
542 | */ |
||
543 | public function renderTools() |
||
547 | |||
548 | /** |
||
549 | * Render form footer. |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | public function renderFooter() |
||
557 | |||
558 | /** |
||
559 | * Render form. |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | public function render() |
||
604 | } |
||
605 |
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.