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 |
||
| 19 | class Builder |
||
| 20 | { |
||
| 21 | const VERSION = '5.x-dev'; |
||
| 22 | const VERBOSITY_QUIET = -1; |
||
| 23 | const VERBOSITY_NORMAL = 0; |
||
| 24 | const VERBOSITY_VERBOSE = 1; |
||
| 25 | const VERBOSITY_DEBUG = 2; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * App version. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected static $version; |
||
| 33 | /** |
||
| 34 | * Steps that are processed by build(). |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | * |
||
| 38 | * @see build() |
||
| 39 | */ |
||
| 40 | protected $steps = [ |
||
| 41 | 'Cecil\Step\ConfigImport', |
||
| 42 | 'Cecil\Step\ContentLoad', |
||
| 43 | 'Cecil\Step\DataLoad', |
||
| 44 | 'Cecil\Step\PagesCreate', |
||
| 45 | 'Cecil\Step\PagesConvert', |
||
| 46 | 'Cecil\Step\TaxonomiesCreate', |
||
| 47 | 'Cecil\Step\PagesGenerate', |
||
| 48 | 'Cecil\Step\MenusCreate', |
||
| 49 | 'Cecil\Step\StaticCopy', |
||
| 50 | 'Cecil\Step\PagesRender', |
||
| 51 | 'Cecil\Step\PagesSave', |
||
| 52 | ]; |
||
| 53 | /** |
||
| 54 | * Config. |
||
| 55 | * |
||
| 56 | * @var Config |
||
| 57 | */ |
||
| 58 | protected $config; |
||
| 59 | /** |
||
| 60 | * Content iterator. |
||
| 61 | * |
||
| 62 | * @var Finder |
||
| 63 | */ |
||
| 64 | protected $content; |
||
| 65 | /** |
||
| 66 | * Data array. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $data = []; |
||
| 71 | /** |
||
| 72 | * Pages collection. |
||
| 73 | * |
||
| 74 | * @var PagesCollection |
||
| 75 | */ |
||
| 76 | protected $pages; |
||
| 77 | /** |
||
| 78 | * Collection of site menus. |
||
| 79 | * |
||
| 80 | * @var Collection\Menu\Collection |
||
| 81 | */ |
||
| 82 | protected $menus; |
||
| 83 | /** |
||
| 84 | * Collection of site taxonomies. |
||
| 85 | * |
||
| 86 | * @var Collection\Taxonomy\Collection |
||
| 87 | */ |
||
| 88 | protected $taxonomies; |
||
| 89 | /** |
||
| 90 | * Twig renderer. |
||
| 91 | * |
||
| 92 | * @var Renderer\Twig |
||
| 93 | */ |
||
| 94 | protected $renderer; |
||
| 95 | /** |
||
| 96 | * @var \Closure |
||
| 97 | */ |
||
| 98 | protected $messageCallback; |
||
| 99 | /** |
||
| 100 | * @var GeneratorManager |
||
| 101 | */ |
||
| 102 | protected $generatorManager; |
||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $log; |
||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $options; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Builder constructor. |
||
| 114 | * |
||
| 115 | * @param Config|array|null $config |
||
| 116 | * @param \Closure|null $messageCallback |
||
| 117 | */ |
||
| 118 | public function __construct($config = null, \Closure $messageCallback = null) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Creates a new Builder instance. |
||
| 128 | * |
||
| 129 | * @return Builder |
||
| 130 | */ |
||
| 131 | public static function create() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set config. |
||
| 140 | * |
||
| 141 | * @param Config|array|null $config |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function setConfig($config) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return Config |
||
| 159 | */ |
||
| 160 | public function getConfig() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Config::setSourceDir alias. |
||
| 167 | * |
||
| 168 | * @param $sourceDir |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setSourceDir($sourceDir) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Config::setDestinationDir alias. |
||
| 181 | * |
||
| 182 | * @param $destinationDir |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function setDestinationDir($destinationDir) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param Finder $content |
||
| 195 | */ |
||
| 196 | public function setContent(Finder $content) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return Finder |
||
| 203 | */ |
||
| 204 | public function getContent(): Finder |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param array $data |
||
| 211 | */ |
||
| 212 | public function setData(array $data) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public function getData(): array |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param $pages |
||
| 227 | */ |
||
| 228 | public function setPages($pages) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return PagesCollection |
||
| 235 | */ |
||
| 236 | public function getPages() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $menus |
||
| 243 | */ |
||
| 244 | public function setMenus($menus) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return Collection\Menu\Collection |
||
| 251 | */ |
||
| 252 | public function getMenus() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param $taxonomies |
||
| 259 | */ |
||
| 260 | public function setTaxonomies($taxonomies) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return Collection\Taxonomy\Collection |
||
| 267 | */ |
||
| 268 | public function getTaxonomies() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param \Closure|null $messageCallback |
||
| 275 | */ |
||
| 276 | public function setMessageCallback($messageCallback = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return \Closure |
||
| 334 | */ |
||
| 335 | public function getMessageCb() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param $renderer |
||
| 342 | */ |
||
| 343 | public function setRenderer($renderer) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return Renderer\Twig |
||
| 350 | */ |
||
| 351 | public function getRenderer() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $log |
||
| 358 | * @param int $type |
||
| 359 | * |
||
| 360 | * @return array|null |
||
| 361 | */ |
||
| 362 | public function addLog($log, $type = 0) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param int $type |
||
| 374 | * |
||
| 375 | * @return array|null |
||
| 376 | */ |
||
| 377 | public function getLog($type = 0) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param int $type |
||
| 388 | * |
||
| 389 | * Display $log string. |
||
| 390 | */ |
||
| 391 | public function showLog($type = 0) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return array $options |
||
| 402 | */ |
||
| 403 | public function getBuildOptions() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Builds a new website. |
||
| 410 | * |
||
| 411 | * @param array $options |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | public function build($options) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return version. |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public static function getVersion() |
||
| 482 | } |
||
| 483 |