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) |
||
171 | { |
||
172 | 1 | if ($this->has($name)) { |
|
173 | 1 | unset($this->elements[$name]); |
|
174 | } |
||
175 | 1 | } |
|
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) |
||
197 | { |
||
198 | 7 | $view = $this->getRenderView($renderView); |
|
199 | |||
200 | 7 | $minify = $this->config['minify']; |
|
201 | |||
202 | 7 | $rendered = $this->viewFactory->make($view)->with([ |
|
203 | 7 | 'menu' => $this, |
|
204 | 7 | 'renderView' => $renderView, |
|
205 | 7 | ])->render(); |
|
206 | |||
207 | 7 | if ($minify) { |
|
208 | 7 | $rendered = $this->minifyHtmlOutput($rendered); |
|
209 | } |
||
210 | |||
211 | 7 | return $rendered; |
|
212 | } |
||
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) |
||
241 | { |
||
242 | $search = array( |
||
243 | 7 | '/\>[^\S]+/s', // strip whitespaces after tags, except space |
|
244 | '/[^\S]+\</s', // strip whitespaces before tags, except space |
||
245 | '/(\s)+/s' // shorten multiple whitespace sequences |
||
246 | ); |
||
247 | |||
248 | $replace = array( |
||
249 | 7 | '>', |
|
250 | '<', |
||
251 | '\\1' |
||
252 | ); |
||
253 | |||
254 | 7 | return preg_replace($search, $replace, $html); |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get view for render |
||
259 | * |
||
260 | * @param string $view |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function getRenderView($view = null) |
||
264 | { |
||
265 | 9 | $renderView = $this->config['view']; |
|
266 | |||
267 | 9 | if (!empty($this->view)) { |
|
268 | 3 | $renderView = $this->view; |
|
269 | } |
||
270 | |||
271 | 9 | if (!empty($view) && $this->viewFactory->exists($view)) { |
|
272 | 3 | $renderView = $view; |
|
273 | } |
||
274 | |||
275 | 9 | return $renderView; |
|
276 | } |
||
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) |
||
296 | { |
||
297 | 2 | $menu = $this->app->make(BuilderContract::class, [ |
|
298 | 2 | 'container' => $this->app, |
|
299 | 2 | 'name' => $name, |
|
300 | 2 | 'activeAttributes' => $this->app->make(AttributesContract::class, ['attributes' => $activeAttributes]), |
|
301 | 2 | 'attributes' => $this->app->make(AttributesContract::class, ['attributes' => $attributes]), |
|
302 | 2 | 'view' => $this->getView(), |
|
303 | ]); |
||
304 | |||
305 | 2 | if (is_callable($callback)) { |
|
306 | 2 | call_user_func($callback, $menu); |
|
307 | } |
||
308 | |||
309 | 2 | return $menu; |
|
310 | } |
||
311 | |||
312 | protected function rebuildIndexesArray() |
||
313 | { |
||
314 | 2 | $this->indexes = []; |
|
315 | 2 | $iterator = 0; |
|
316 | |||
317 | 2 | foreach ($this->elements as $key => $value) { |
|
318 | 2 | $this->indexes[$key] = $iterator++; |
|
319 | } |
||
320 | 2 | } |
|
321 | |||
322 | /** |
||
323 | * @param string $name |
||
324 | * @param \Closure $callback |
||
325 | * @return array |
||
326 | */ |
||
327 | protected function prepareInsert($name, $callback) |
||
328 | { |
||
329 | 3 | if (!$this->has($name)) { |
|
330 | 1 | throw new \RuntimeException("Menu item \"${name}\" must be exists"); |
|
331 | } |
||
332 | |||
333 | 2 | $forInsert = $this->builderFactory('tmp', [], [], $callback)->all(); |
|
334 | 2 | $diff = array_diff(array_keys(array_diff_key($this->elements, $forInsert)), array_keys($this->elements)); |
|
335 | 2 | $diff = array_intersect_key($this->elements, $forInsert); |
|
336 | |||
337 | 2 | if (count($diff) > 0) { |
|
338 | 1 | throw new \RuntimeException('Duplicated keys: ' . implode(', ', array_keys($diff))); |
|
339 | } |
||
340 | |||
341 | 1 | return $forInsert; |
|
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param int $position |
||
346 | * @param array $values |
||
347 | */ |
||
348 | protected function insert($position, $values) |
||
349 | { |
||
350 | 1 | $firstArray = array_splice($this->elements, 0, $position); |
|
351 | 1 | $this->elements = array_merge($firstArray, $values, $this->elements); |
|
352 | 1 | $this->rebuildIndexesArray(); |
|
353 | 1 | } |
|
354 | |||
355 | /** |
||
356 | * @param $element |
||
357 | * @return ElementFactory |
||
358 | */ |
||
359 | protected function getFactory($element) |
||
365 | |||
366 | /** |
||
367 | * @inheritDoc |
||
368 | */ |
||
369 | public function toArray() |
||
370 | { |
||
371 | 2 | $this->view = $this->getRenderView($this->view); |
|
372 | 2 | $elements = []; |
|
373 | |||
374 | 2 | foreach ($this->elements as $key => $element) { |
|
375 | 2 | $elements[$key] = $element->toArray(); |
|
376 | 2 | $elements[$key]['type'] = array_search(get_class($element), $this->config['alias']); |
|
377 | } |
||
378 | |||
379 | return [ |
||
380 | 2 | 'type' => $this->type, |
|
381 | 2 | 'view' => $this->view, |
|
382 | 2 | 'attributes' => $this->attributes->toArray(), |
|
383 | 2 | 'activeAttributes' => $this->activeAttributes->toArray(), |
|
384 | 2 | 'elements' => $elements, |
|
385 | ]; |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @inheritDoc |
||
390 | */ |
||
391 | public function offsetExists($offset) |
||
392 | { |
||
393 | 1 | if (is_int($offset)) { |
|
394 | return (bool) array_search($offset, $this->indexes, true); |
||
395 | } |
||
396 | 1 | return $this->has($offset); |
|
397 | } |
||
398 | |||
399 | /** |
||
400 | * @inheritDoc |
||
401 | */ |
||
402 | public function offsetGet($offset) |
||
403 | { |
||
404 | 2 | if (is_int($offset)) { |
|
405 | 1 | $offset = array_search($offset, $this->indexes, true); |
|
406 | 1 | if ($offset === false) { |
|
407 | 1 | return null; |
|
408 | } |
||
409 | } |
||
410 | 2 | return $this->get($offset); |
|
411 | } |
||
412 | |||
413 | /** |
||
414 | * @inheritDoc |
||
415 | */ |
||
416 | public function offsetSet($offset, $value) |
||
417 | { |
||
418 | 1 | if ($value instanceof Element) { |
|
419 | 1 | $this->elements[$offset] = $value; |
|
420 | } |
||
421 | 1 | } |
|
422 | |||
423 | /** |
||
424 | * @inheritDoc |
||
425 | */ |
||
426 | public function offsetUnset($offset) |
||
430 | |||
431 | /** |
||
432 | * @inheritDoc |
||
433 | */ |
||
434 | public function serialize() |
||
435 | { |
||
436 | 1 | return serialize([ |
|
437 | 1 | 'type' => $this->type, |
|
438 | 1 | 'view' => $this->view, |
|
439 | 1 | 'attributes' => $this->attributes, |
|
440 | 1 | 'activeAttributes' => $this->activeAttributes, |
|
441 | 1 | 'elements' => $this->elements, |
|
442 | ]); |
||
444 | |||
445 | /** |
||
446 | * @inheritDoc |
||
447 | */ |
||
448 | public function unserialize($serialized) |
||
464 | |||
465 | /** |
||
466 | * @inheritDoc |
||
467 | */ |
||
468 | static public function fromArray(array $builder) |
||
502 | } |