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 |
||
20 | class Builder implements BuilderContract |
||
21 | { |
||
22 | use HasAttributes, TraitHasActiveAttributes; |
||
23 | |||
24 | /** |
||
25 | * @var Container |
||
26 | */ |
||
27 | protected $app; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $elements; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $indexes = []; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $type; |
||
43 | |||
44 | /** |
||
45 | * @var MenuRender |
||
46 | */ |
||
47 | protected $viewFactory; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $config; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $view = null; |
||
58 | |||
59 | /** |
||
60 | * @inheritDoc |
||
61 | */ |
||
62 | 23 | public function __construct(Container $container, AttributesContract $attributes, |
|
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | 19 | public function create($name, $type, $callback = null) |
|
111 | |||
112 | /** |
||
113 | * @inheritDoc |
||
114 | */ |
||
115 | public function insertBefore($name, \Closure $callback) |
||
119 | |||
120 | /** |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | public function insertAfter($name, \Closure $callback) |
||
127 | |||
128 | /** |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | public function has($name) |
||
135 | |||
136 | /** |
||
137 | * @inheritDoc |
||
138 | */ |
||
139 | public function get($name, $default = null) |
||
146 | |||
147 | /** |
||
148 | * @inheritDoc |
||
149 | */ |
||
150 | public function getByIndex($index, $default = null) |
||
156 | |||
157 | /** |
||
158 | * @inheritDoc |
||
159 | */ |
||
160 | public function all() |
||
164 | |||
165 | /** |
||
166 | * @inheritDoc |
||
167 | */ |
||
168 | public function forget($name) |
||
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | public function getType() |
||
182 | |||
183 | /** |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | public function setType($type) |
||
190 | |||
191 | /** |
||
192 | * @inheritDoc |
||
193 | */ |
||
194 | public function render($renderView = null) |
||
211 | |||
212 | /** |
||
213 | * @inheritDoc |
||
214 | */ |
||
215 | public function getView() |
||
219 | |||
220 | /** |
||
221 | * @inheritDoc |
||
222 | */ |
||
223 | public function setView($view) |
||
231 | |||
232 | /** |
||
233 | * Minify html |
||
234 | * |
||
235 | * @param string $html |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function minifyHtmlOutput($html) |
||
254 | |||
255 | /** |
||
256 | * Get view for render |
||
257 | * |
||
258 | * @param string $view |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function getRenderView($view = null) |
||
275 | |||
276 | /** |
||
277 | * @param string $name |
||
278 | * @param Element $item |
||
279 | */ |
||
280 | protected function saveItem($name, $item) |
||
285 | |||
286 | /** |
||
287 | * @param string $name |
||
288 | * @param array $attributes |
||
289 | * @param array $activeAttributes |
||
290 | * @param \Closure|null $callback |
||
291 | * @return BuilderContract |
||
292 | */ |
||
293 | protected function builderFactory($name, $attributes = [], $activeAttributes = [], $callback = null) |
||
309 | |||
310 | protected function rebuildIndexesArray() |
||
319 | |||
320 | /** |
||
321 | * @param string $name |
||
322 | * @param \Closure $callback |
||
323 | * @return array |
||
324 | */ |
||
325 | protected function prepareInsert($name, $callback) |
||
340 | |||
341 | /** |
||
342 | * @param int $position |
||
343 | * @param array $values |
||
344 | */ |
||
345 | protected function insert($position, $values) |
||
351 | |||
352 | /** |
||
353 | * @param $element |
||
354 | * @return ElementFactory |
||
355 | */ |
||
356 | protected function getFactory($element) |
||
362 | |||
363 | /** |
||
364 | * @inheritDoc |
||
365 | */ |
||
366 | public function toArray() |
||
384 | |||
385 | /** |
||
386 | * @inheritDoc |
||
387 | */ |
||
388 | public function offsetExists($offset) |
||
392 | |||
393 | /** |
||
394 | * @inheritDoc |
||
395 | */ |
||
396 | public function offsetGet($offset) |
||
400 | |||
401 | /** |
||
402 | * @inheritDoc |
||
403 | */ |
||
404 | public function offsetSet($offset, $value) |
||
408 | |||
409 | /** |
||
410 | * @inheritDoc |
||
411 | */ |
||
412 | public function offsetUnset($offset) |
||
416 | |||
417 | /** |
||
418 | * @inheritDoc |
||
419 | */ |
||
420 | public function serialize() |
||
430 | |||
431 | /** |
||
432 | * @inheritDoc |
||
433 | */ |
||
434 | public function unserialize($serialized) |
||
450 | |||
451 | /** |
||
452 | * @inheritDoc |
||
453 | */ |
||
454 | static public function fromArray(array $builder) |
||
488 | } |