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 | 26 | public function __construct(Container $container, AttributesContract $attributes, |
|
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | 20 | public function create($name, $type, $callback = null) |
|
111 | |||
112 | /** |
||
113 | * @inheritDoc |
||
114 | */ |
||
115 | public function insertBefore($name, \Closure $callback) |
||
120 | |||
121 | /** |
||
122 | * @inheritDoc |
||
123 | */ |
||
124 | public function insertAfter($name, \Closure $callback) |
||
129 | |||
130 | /** |
||
131 | * @inheritDoc |
||
132 | */ |
||
133 | public function has($name) |
||
137 | |||
138 | /** |
||
139 | * @inheritDoc |
||
140 | */ |
||
141 | public function get($name, $default = null) |
||
148 | |||
149 | /** |
||
150 | * @inheritDoc |
||
151 | */ |
||
152 | public function getByIndex($index, $default = null) |
||
158 | |||
159 | /** |
||
160 | * @inheritDoc |
||
161 | */ |
||
162 | public function all() |
||
166 | |||
167 | /** |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | public function forget($name) |
||
176 | |||
177 | /** |
||
178 | * @inheritDoc |
||
179 | */ |
||
180 | public function getType() |
||
184 | |||
185 | /** |
||
186 | * @inheritDoc |
||
187 | */ |
||
188 | public function setType($type) |
||
192 | |||
193 | /** |
||
194 | * @inheritDoc |
||
195 | */ |
||
196 | public function render($renderView = null) |
||
213 | |||
214 | /** |
||
215 | * @inheritDoc |
||
216 | */ |
||
217 | public function getView() |
||
221 | |||
222 | /** |
||
223 | * @inheritDoc |
||
224 | */ |
||
225 | public function setView($view) |
||
233 | |||
234 | /** |
||
235 | * Minify html |
||
236 | * |
||
237 | * @param string $html |
||
238 | * @return string |
||
239 | */ |
||
240 | protected function minifyHtmlOutput($html) |
||
256 | |||
257 | /** |
||
258 | * Get view for render |
||
259 | * |
||
260 | * @param string $view |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function getRenderView($view = null) |
||
277 | |||
278 | /** |
||
279 | * @param string $name |
||
280 | * @param Element $item |
||
281 | */ |
||
282 | protected function saveItem($name, $item) |
||
287 | |||
288 | /** |
||
289 | * @param string $name |
||
290 | * @param array $attributes |
||
291 | * @param array $activeAttributes |
||
292 | * @param \Closure|null $callback |
||
293 | * @return BuilderContract |
||
294 | */ |
||
295 | protected function builderFactory($name, $attributes = [], $activeAttributes = [], $callback = null) |
||
311 | |||
312 | protected function rebuildIndexesArray() |
||
321 | |||
322 | /** |
||
323 | * @param string $name |
||
324 | * @param \Closure $callback |
||
325 | * @return array |
||
326 | */ |
||
327 | protected function prepareInsert($name, $callback) |
||
343 | |||
344 | /** |
||
345 | * @param int $position |
||
346 | * @param array $values |
||
347 | */ |
||
348 | protected function insert($position, $values) |
||
354 | |||
355 | /** |
||
356 | * @param $element |
||
357 | * @return ElementFactory |
||
358 | */ |
||
359 | protected function getFactory($element) |
||
365 | |||
366 | /** |
||
367 | * @inheritDoc |
||
368 | */ |
||
369 | public function toArray() |
||
387 | |||
388 | /** |
||
389 | * @inheritDoc |
||
390 | */ |
||
391 | public function offsetExists($offset) |
||
398 | |||
399 | /** |
||
400 | * @inheritDoc |
||
401 | */ |
||
402 | public function offsetGet($offset) |
||
412 | |||
413 | /** |
||
414 | * @inheritDoc |
||
415 | */ |
||
416 | public function offsetSet($offset, $value) |
||
422 | |||
423 | /** |
||
424 | * @inheritDoc |
||
425 | */ |
||
426 | public function offsetUnset($offset) |
||
430 | |||
431 | /** |
||
432 | * @inheritDoc |
||
433 | */ |
||
434 | public function serialize() |
||
444 | |||
445 | /** |
||
446 | * @inheritDoc |
||
447 | */ |
||
448 | public function unserialize($serialized) |
||
464 | |||
465 | /** |
||
466 | * @inheritDoc |
||
467 | */ |
||
468 | static public function fromArray(array $builder) |
||
502 | } |