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 = '4.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\ImportConfig', |
||
| 42 | 'Cecil\Step\LocateContent', |
||
| 43 | 'Cecil\Step\CreatePages', |
||
| 44 | 'Cecil\Step\ConvertPages', |
||
| 45 | 'Cecil\Step\GeneratePages', |
||
| 46 | 'Cecil\Step\GenerateMenus', |
||
| 47 | 'Cecil\Step\CopyStatic', |
||
| 48 | 'Cecil\Step\RenderPages', |
||
| 49 | 'Cecil\Step\SavePages', |
||
| 50 | ]; |
||
| 51 | /** |
||
| 52 | * Config. |
||
| 53 | * |
||
| 54 | * @var Config |
||
| 55 | */ |
||
| 56 | protected $config; |
||
| 57 | /** |
||
| 58 | * Content iterator. |
||
| 59 | * |
||
| 60 | * @var Finder |
||
| 61 | */ |
||
| 62 | protected $content; |
||
| 63 | /** |
||
| 64 | * Pages collection. |
||
| 65 | * |
||
| 66 | * @var PageCollection |
||
| 67 | */ |
||
| 68 | protected $pages; |
||
| 69 | /** |
||
| 70 | * Collection of site menus. |
||
| 71 | * |
||
| 72 | * @var Collection\Menu\Collection |
||
| 73 | */ |
||
| 74 | protected $menus; |
||
| 75 | /** |
||
| 76 | * Twig renderer. |
||
| 77 | * |
||
| 78 | * @var Renderer\Twig |
||
| 79 | */ |
||
| 80 | protected $renderer; |
||
| 81 | /** |
||
| 82 | * @var \Closure |
||
| 83 | */ |
||
| 84 | protected $messageCallback; |
||
| 85 | /** |
||
| 86 | * @var GeneratorManager |
||
| 87 | */ |
||
| 88 | protected $generatorManager; |
||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $log; |
||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $options; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Builder constructor. |
||
| 100 | * |
||
| 101 | * @param Config|array|null $config |
||
| 102 | * @param \Closure|null $messageCallback |
||
| 103 | */ |
||
| 104 | public function __construct($config = null, \Closure $messageCallback = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Creates a new Builder instance. |
||
| 114 | * |
||
| 115 | * @return Builder |
||
| 116 | */ |
||
| 117 | public static function create() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set config. |
||
| 126 | * |
||
| 127 | * @param Config|array|null $config |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setConfig($config) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return Config |
||
| 145 | */ |
||
| 146 | public function getConfig() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Config::setSourceDir alias. |
||
| 153 | * |
||
| 154 | * @param $sourceDir |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function setSourceDir($sourceDir) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Config::setDestinationDir alias. |
||
| 167 | * |
||
| 168 | * @param $destinationDir |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function setDestinationDir($destinationDir) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param $content |
||
| 181 | */ |
||
| 182 | public function setContent($content) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return Finder |
||
| 189 | */ |
||
| 190 | public function getContent() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $pages |
||
| 197 | */ |
||
| 198 | public function setPages($pages) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return PageCollection |
||
| 205 | */ |
||
| 206 | public function getPages() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param $menus |
||
| 213 | */ |
||
| 214 | public function setMenus($menus) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return Collection\Menu\Collection |
||
| 221 | */ |
||
| 222 | public function getMenus() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param \Closure|null $messageCallback |
||
| 229 | */ |
||
| 230 | public function setMessageCallback($messageCallback = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return \Closure |
||
| 284 | */ |
||
| 285 | public function getMessageCb() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $renderer |
||
| 292 | */ |
||
| 293 | public function setRenderer($renderer) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return Renderer\Twig |
||
| 300 | */ |
||
| 301 | public function getRenderer() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $log |
||
| 308 | * @param int $type |
||
| 309 | * |
||
| 310 | * @return array|null |
||
| 311 | */ |
||
| 312 | public function addLog($log, $type = 0) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param int $type |
||
| 324 | * |
||
| 325 | * @return array|null |
||
| 326 | */ |
||
| 327 | public function getLog($type = 0) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param int $type |
||
| 338 | * |
||
| 339 | * Display $log string. |
||
| 340 | */ |
||
| 341 | public function showLog($type = 0) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return array $options |
||
| 352 | */ |
||
| 353 | public function getBuildOptions() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Builds a new website. |
||
| 360 | * |
||
| 361 | * @param array $options |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function build($options) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Return version. |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public static function getVersion() |
||
| 429 | } |
||
| 430 |