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 | public function __construct(Container $container, AttributesContract $attributes, |
||
| 77 | 27 | ||
| 78 | /** |
||
| 79 | * @inheritDoc |
||
| 80 | */ |
||
| 81 | public function create($name, $type, callable $callback = null) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritDoc |
||
| 106 | */ |
||
| 107 | public function insertBefore($name, callable $callback) |
||
| 112 | |||
| 113 | 2 | /** |
|
| 114 | 1 | * @inheritDoc |
|
| 115 | 1 | */ |
|
| 116 | public function insertAfter($name, callable $callback) |
||
| 121 | |||
| 122 | 2 | /** |
|
| 123 | 1 | * @inheritDoc |
|
| 124 | 1 | */ |
|
| 125 | public function has($name) |
||
| 129 | |||
| 130 | /** |
||
| 131 | 24 | * @inheritDoc |
|
| 132 | */ |
||
| 133 | public function get($name, $default = null) |
||
| 140 | 3 | ||
| 141 | /** |
||
| 142 | 1 | * @inheritDoc |
|
| 143 | */ |
||
| 144 | public function getByIndex($index, $default = null) |
||
| 150 | 1 | ||
| 151 | /** |
||
| 152 | 1 | * @inheritDoc |
|
| 153 | */ |
||
| 154 | public function all() |
||
| 158 | |||
| 159 | /** |
||
| 160 | 11 | * @inheritDoc |
|
| 161 | */ |
||
| 162 | public function forget($name) |
||
| 168 | 1 | ||
| 169 | 1 | /** |
|
| 170 | * @inheritDoc |
||
| 171 | 1 | */ |
|
| 172 | public function getType() |
||
| 176 | |||
| 177 | /** |
||
| 178 | 10 | * @inheritDoc |
|
| 179 | */ |
||
| 180 | public function setType($type) |
||
| 184 | |||
| 185 | /** |
||
| 186 | 1 | * @inheritDoc |
|
| 187 | 1 | */ |
|
| 188 | public function render($renderView = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | 9 | * @inheritDoc |
|
| 208 | */ |
||
| 209 | public function getView() |
||
| 213 | |||
| 214 | /** |
||
| 215 | 2 | * @inheritDoc |
|
| 216 | */ |
||
| 217 | public function setView($view) |
||
| 225 | |||
| 226 | /** |
||
| 227 | 3 | * Minify html |
|
| 228 | 3 | * |
|
| 229 | * @param string $html |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | protected function minifyHtmlOutput($html) |
||
| 248 | |||
| 249 | /** |
||
| 250 | 9 | * Get view for render |
|
| 251 | * |
||
| 252 | * @param string $view |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | protected function getRenderView($view = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | 9 | * @param string $name |
|
| 272 | * @param Element $item |
||
| 273 | */ |
||
| 274 | protected function saveItem($name, $item) |
||
| 279 | |||
| 280 | 20 | /** |
|
| 281 | 20 | * @param string $name |
|
| 282 | 20 | * @param array $attributes |
|
| 283 | * @param array $activeAttributes |
||
| 284 | * @param \Closure|null $callback |
||
| 285 | * @return BuilderContract |
||
| 286 | */ |
||
| 287 | protected function builderFactory($name, $attributes = [], $activeAttributes = [], $callback = null) |
||
| 305 | 2 | ||
| 306 | protected function rebuildIndexesArray() |
||
| 315 | |||
| 316 | 2 | /** |
|
| 317 | * @param string $name |
||
| 318 | * @param \Closure $callback |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | protected function prepareInsert($name, $callback) |
||
| 342 | 1 | ||
| 343 | /** |
||
| 344 | * @param int $position |
||
| 345 | * @param array $values |
||
| 346 | */ |
||
| 347 | protected function insert($position, $values) |
||
| 353 | 1 | ||
| 354 | 1 | /** |
|
| 355 | * @param $element |
||
| 356 | * @return ElementFactory |
||
| 357 | * @throws \RuntimeException |
||
| 358 | */ |
||
| 359 | protected function getFactory($element) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @inheritDoc |
||
| 372 | 2 | */ |
|
| 373 | 2 | public function toArray() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @inheritDoc |
||
| 398 | 1 | */ |
|
| 399 | public function offsetExists($offset) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @inheritDoc |
||
| 409 | 2 | */ |
|
| 410 | 1 | public function offsetGet($offset) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @inheritDoc |
||
| 423 | 1 | */ |
|
| 424 | 1 | public function offsetSet($offset, $value) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @inheritDoc |
||
| 433 | 1 | */ |
|
| 434 | 1 | public function offsetUnset($offset) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @inheritDoc |
||
| 441 | 1 | */ |
|
| 442 | 1 | static public function fromArray(array $builder) |
|
| 446 | 1 | ||
| 447 | /** |
||
| 448 | * @param string $type |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | protected function hasActiveAttributes($type) |
||
| 457 | } |