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 |
||
| 18 | class Builder |
||
| 19 | { |
||
| 20 | const VERSION = '2.x-dev'; |
||
| 21 | const VERBOSITY_QUIET = -1; |
||
| 22 | const VERBOSITY_NORMAL = 0; |
||
| 23 | const VERBOSITY_VERBOSE = 1; |
||
| 24 | const VERBOSITY_DEBUG = 2; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Library version. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $version; |
||
| 32 | /** |
||
| 33 | * Steps that are processed by build(). |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | * |
||
| 37 | * @see build() |
||
| 38 | */ |
||
| 39 | protected $steps = [ |
||
| 40 | 'Cecil\Step\ImportConfig', |
||
| 41 | 'Cecil\Step\LocateContent', |
||
| 42 | 'Cecil\Step\CreatePages', |
||
| 43 | 'Cecil\Step\ConvertPages', |
||
| 44 | 'Cecil\Step\GeneratePages', |
||
| 45 | 'Cecil\Step\GenerateMenus', |
||
| 46 | 'Cecil\Step\CopyStatic', |
||
| 47 | 'Cecil\Step\RenderPages', |
||
| 48 | 'Cecil\Step\SavePages', |
||
| 49 | ]; |
||
| 50 | /** |
||
| 51 | * Config. |
||
| 52 | * |
||
| 53 | * @var Config |
||
| 54 | */ |
||
| 55 | protected $config; |
||
| 56 | /** |
||
| 57 | * Content iterator. |
||
| 58 | * |
||
| 59 | * @var Finder |
||
| 60 | */ |
||
| 61 | protected $content; |
||
| 62 | /** |
||
| 63 | * Pages collection. |
||
| 64 | * |
||
| 65 | * @var PageCollection |
||
| 66 | */ |
||
| 67 | protected $pages; |
||
| 68 | /** |
||
| 69 | * Collection of site menus. |
||
| 70 | * |
||
| 71 | * @var Collection\Menu\Collection |
||
| 72 | */ |
||
| 73 | protected $menus; |
||
| 74 | /** |
||
| 75 | * Twig renderer. |
||
| 76 | * |
||
| 77 | * @var Renderer\Twig |
||
| 78 | */ |
||
| 79 | protected $renderer; |
||
| 80 | /** |
||
| 81 | * @var \Closure |
||
| 82 | */ |
||
| 83 | protected $messageCallback; |
||
| 84 | /** |
||
| 85 | * @var GeneratorManager |
||
| 86 | */ |
||
| 87 | protected $generatorManager; |
||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $log; |
||
| 92 | /** |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $options; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Builder constructor. |
||
| 99 | * |
||
| 100 | * @param Config|array|null $config |
||
| 101 | * @param \Closure|null $messageCallback |
||
| 102 | */ |
||
| 103 | public function __construct($config = null, \Closure $messageCallback = null) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Creates a new Builder instance. |
||
| 113 | * |
||
| 114 | * @return Builder |
||
| 115 | */ |
||
| 116 | public static function create() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set config. |
||
| 125 | * |
||
| 126 | * @param Config|array|null $config |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function setConfig($config) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return Config |
||
| 144 | */ |
||
| 145 | public function getConfig() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Config::setSourceDir alias. |
||
| 152 | * |
||
| 153 | * @param $sourceDir |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function setSourceDir($sourceDir) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Config::setDestinationDir alias. |
||
| 166 | * |
||
| 167 | * @param $destinationDir |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function setDestinationDir($destinationDir) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param $content |
||
| 180 | */ |
||
| 181 | public function setContent($content) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return Finder |
||
| 188 | */ |
||
| 189 | public function getContent() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $pages |
||
| 196 | */ |
||
| 197 | public function setPages($pages) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return PageCollection |
||
| 204 | */ |
||
| 205 | public function getPages() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param $menus |
||
| 212 | */ |
||
| 213 | public function setMenus($menus) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return Collection\Menu\Collection |
||
| 220 | */ |
||
| 221 | public function getMenus() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param \Closure|null $messageCallback |
||
| 228 | */ |
||
| 229 | public function setMessageCallback($messageCallback = null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return \Closure |
||
| 283 | */ |
||
| 284 | public function getMessageCb() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $renderer |
||
| 291 | */ |
||
| 292 | public function setRenderer($renderer) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return Renderer\Twig |
||
| 299 | */ |
||
| 300 | public function getRenderer() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $log |
||
| 307 | * @param int $type |
||
| 308 | * |
||
| 309 | * @return array|null |
||
| 310 | */ |
||
| 311 | public function addLog($log, $type = 0) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param int $type |
||
| 323 | * |
||
| 324 | * @return array|null |
||
| 325 | */ |
||
| 326 | public function getLog($type = 0) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param int $type |
||
| 337 | * |
||
| 338 | * Display $log string. |
||
| 339 | */ |
||
| 340 | public function showLog($type = 0) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return array $options |
||
| 351 | */ |
||
| 352 | public function getBuildOptions() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Builds a new website. |
||
| 359 | * |
||
| 360 | * @param array $options |
||
| 361 | * |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function build($options) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Return version. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getVersion() |
||
| 421 | } |
||
| 422 |