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; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Container |
||
| 26 | */ |
||
| 27 | protected $app; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $items; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $indexes = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $name; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $type; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var AttributesContract |
||
| 51 | */ |
||
| 52 | protected $activeAttributes; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var MenuRender |
||
| 56 | */ |
||
| 57 | protected $viewFactory; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $config; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $view = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @inheritDoc |
||
| 71 | */ |
||
| 72 | 24 | public function __construct(Container $container, $name, AttributesContract $attributes, |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritDoc |
||
| 90 | */ |
||
| 91 | 5 | public function submenu($name, \Closure $itemCallable, \Closure $menuCallable) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritDoc |
||
| 109 | */ |
||
| 110 | 16 | public function create($name, $title, $url, $attributes = [], $linkAttributes = [], $callback = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @inheritDoc |
||
| 126 | */ |
||
| 127 | public function insertBefore($name, \Closure $callback) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritDoc |
||
| 134 | */ |
||
| 135 | public function insertAfter($name, \Closure $callback) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritDoc |
||
| 142 | */ |
||
| 143 | public function has($name) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritDoc |
||
| 150 | */ |
||
| 151 | public function get($name, $default = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @inheritDoc |
||
| 161 | */ |
||
| 162 | public function getByIndex($index, $default = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritDoc |
||
| 171 | */ |
||
| 172 | public function all() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritDoc |
||
| 179 | */ |
||
| 180 | public function forget($name) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritDoc |
||
| 189 | */ |
||
| 190 | public function getType() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @inheritDoc |
||
| 197 | */ |
||
| 198 | public function setType($type) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @inheritDoc |
||
| 205 | */ |
||
| 206 | public function render($renderView = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritDoc |
||
| 226 | */ |
||
| 227 | public function activeAttributes($callback = null) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @inheritDoc |
||
| 238 | */ |
||
| 239 | public function getView() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @inheritDoc |
||
| 246 | */ |
||
| 247 | public function setView($view) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Minify html |
||
| 258 | * |
||
| 259 | * @param string $html |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function minifyHtmlOutput($html) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get view for render |
||
| 281 | * |
||
| 282 | * @param string $view |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function getRenderView($view = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $title |
||
| 302 | * @param string $url |
||
| 303 | * @param array $attributes |
||
| 304 | * @return LinkContract |
||
| 305 | */ |
||
| 306 | protected function linkFactory($title = '', $url = '#', $attributes = []) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param LinkContract $link |
||
| 317 | * @param array $attributes |
||
| 318 | * @param \Closure $callback |
||
| 319 | * @return ItemContract |
||
| 320 | */ |
||
| 321 | protected function itemFactory($link, $attributes = [], $callback = null) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $name |
||
| 339 | * @param ItemContract|SubMenuContract $item |
||
| 340 | */ |
||
| 341 | protected function saveItem($name, $item) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $name |
||
| 349 | * @param array $attributes |
||
| 350 | * @param array $activeAttributes |
||
| 351 | * @param \Closure|null $callback |
||
| 352 | * @return BuilderContract |
||
| 353 | */ |
||
| 354 | protected function builderFactory($name, $attributes = [], $activeAttributes = [], $callback = null) |
||
| 370 | |||
| 371 | protected function rebuildIndexesArray() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $name |
||
| 383 | * @param \Closure $callback |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | protected function prepareInsert($name, $callback) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param int $position |
||
| 404 | * @param array $values |
||
| 405 | */ |
||
| 406 | protected function insert($position, $values) |
||
| 412 | } |