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 |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string path to output assembled configs |
||
| 24 | */ |
||
| 25 | protected $outputDir; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array files to build configs |
||
| 29 | * @see buildConfigs() |
||
| 30 | */ |
||
| 31 | protected $files = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array additional data to be merged into every config (e.g. aliases) |
||
| 35 | */ |
||
| 36 | protected $addition = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var IOInterface |
||
| 40 | */ |
||
| 41 | protected $io; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array collected variables |
||
| 45 | */ |
||
| 46 | protected $vars = []; |
||
| 47 | |||
| 48 | const OUTPUT_DIR_SUFFIX = '-output'; |
||
| 49 | const BASE_DIR_MARKER = '<<<base-dir>>>'; |
||
| 50 | |||
| 51 | public function __construct(array $files = [], $outputDir = null) |
||
| 56 | |||
| 57 | public function setFiles(array $files) |
||
| 61 | |||
| 62 | public function setOutputDir($outputDir) |
||
| 66 | |||
| 67 | public function setAddition(array $addition) |
||
| 71 | |||
| 72 | public function loadFiles() |
||
| 77 | |||
| 78 | public function saveFiles() |
||
| 83 | |||
| 84 | public static function rebuild($outputDir = null) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Returns default output dir. |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public static function defaultOutputDir() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns full path to assembled config file. |
||
| 102 | * @param string $filename name of config |
||
| 103 | * @return string absolute path |
||
| 104 | */ |
||
| 105 | public static function path($filename) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Builds configs by given files list. |
||
| 112 | * @param null|array $files files to process: config name => list of files |
||
| 113 | */ |
||
| 114 | public function buildConfigs($files = null) |
||
| 128 | |||
| 129 | protected function readConfigs(array $pathes) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Merges given configs and writes at given name. |
||
| 144 | * @param mixed $name |
||
| 145 | * @param array $configs |
||
| 146 | */ |
||
| 147 | public function buildConfig($name, array $configs, $defines = []) |
||
| 157 | |||
| 158 | protected function isSpecialConfig($name) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Writes config file by name. |
||
| 165 | * @param string $name |
||
| 166 | * @param array $data |
||
| 167 | */ |
||
| 168 | public function writeConfig($name, array $data, array $defines = []) |
||
| 174 | |||
| 175 | public function getOutputPath($name) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Writes config file by full path. |
||
| 182 | * @param string $path |
||
| 183 | * @param array $data |
||
| 184 | */ |
||
| 185 | public static function writeFile($path, array $data, array $defines = []) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Writes file if content changed. |
||
| 198 | * @param string $path |
||
| 199 | * @param string $content |
||
| 200 | */ |
||
| 201 | protected static function putFile($path, $content) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Substitute output pathes in given data array recursively with marker. |
||
| 213 | * @param array $data |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function substituteOutputDirs($data) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Substitute all pathes in given array recursively with alias if applicable. |
||
| 223 | * @param array $data |
||
| 224 | * @param string $dir |
||
| 225 | * @param string $alias |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public static function substitutePaths($data, $dir, $alias) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Substitute path with alias if applicable. |
||
| 243 | * @param string $path |
||
| 244 | * @param string $dir |
||
| 245 | * @param string $alias |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | protected static function substitutePath($path, $dir, $alias) |
||
| 258 | |||
| 259 | public function readConfig($name) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Reads config file. |
||
| 266 | * @param string $__path |
||
| 267 | * @return array configuration read from file |
||
| 268 | */ |
||
| 269 | public function readFile($__path) |
||
| 290 | |||
| 291 | public function setIo(IOInterface $io) |
||
| 295 | |||
| 296 | protected function writeError($text) |
||
| 304 | } |
||
| 305 |