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 |
||
21 | class Builder |
||
22 | { |
||
23 | /** |
||
24 | * @var string path to output assembled configs |
||
25 | */ |
||
26 | protected $outputDir; |
||
27 | |||
28 | /** |
||
29 | * @var array files to build configs |
||
30 | * @see buildConfigs() |
||
31 | */ |
||
32 | protected $files = []; |
||
33 | |||
34 | /** |
||
35 | * @var array additional data to be merged into every config (e.g. aliases) |
||
36 | */ |
||
37 | protected $addition = []; |
||
38 | |||
39 | /** |
||
40 | * @var IOInterface |
||
41 | */ |
||
42 | protected $io; |
||
43 | |||
44 | /** |
||
45 | * @var array collected variables |
||
46 | */ |
||
47 | protected $vars = []; |
||
48 | |||
49 | const UNIX_DS = '/'; |
||
50 | const OUTPUT_DIR_SUFFIX = '-output'; |
||
51 | const BASE_DIR_MARKER = '<<<base-dir>>>'; |
||
52 | |||
53 | public function __construct(array $files = [], $outputDir = null) |
||
58 | |||
59 | public function setFiles(array $files) |
||
63 | |||
64 | public function setOutputDir($outputDir) |
||
68 | |||
69 | public function setAddition(array $addition) |
||
73 | |||
74 | public function loadFiles() |
||
79 | |||
80 | public function saveFiles() |
||
85 | |||
86 | public static function rebuild($outputDir = null) |
||
92 | |||
93 | /** |
||
94 | * Returns default output dir. |
||
95 | * @param string $vendor path to vendor dir |
||
96 | * @return string |
||
97 | */ |
||
98 | public static function findOutputDir($vendor = null) |
||
108 | |||
109 | /** |
||
110 | * Returns full path to assembled config file. |
||
111 | * @param string $filename name of config |
||
112 | * @param string $vendor path to vendor dir |
||
113 | * @return string absolute path |
||
114 | */ |
||
115 | public static function path($filename, $vendor = null) |
||
119 | |||
120 | /** |
||
121 | * Builds configs by given files list. |
||
122 | * @param null|array $files files to process: config name => list of files |
||
123 | */ |
||
124 | public function buildConfigs($files = null) |
||
140 | |||
141 | protected function loadConfigs(array $paths) |
||
153 | |||
154 | /** |
||
155 | * Merges given configs and writes at given name. |
||
156 | * @param mixed $name |
||
157 | * @param array $configs |
||
158 | */ |
||
159 | public function buildConfig($name, array $configs, $defines = []) |
||
172 | |||
173 | protected function pushEnvVars($vars) |
||
187 | |||
188 | protected function isSpecialConfig($name) |
||
192 | |||
193 | /** |
||
194 | * Writes config file by name. |
||
195 | * @param string $name |
||
196 | * @param array $data |
||
197 | */ |
||
198 | public function writeConfig($name, array $data, array $defines = []) |
||
207 | |||
208 | public function getOutputPath($name) |
||
212 | |||
213 | /** |
||
214 | * Writes config file by full path. |
||
215 | * @param string $path |
||
216 | * @param array $data |
||
217 | */ |
||
218 | public static function writeFile($path, array $data, array $defines = []) |
||
228 | |||
229 | /** |
||
230 | * Writes file if content changed. |
||
231 | * @param string $path |
||
232 | * @param string $content |
||
233 | */ |
||
234 | protected static function putFile($path, $content) |
||
243 | |||
244 | /** |
||
245 | * Substitute output paths in given data array recursively with marker. |
||
246 | * @param array $data |
||
247 | * @return array |
||
248 | */ |
||
249 | public function substituteOutputDirs($data) |
||
255 | |||
256 | /** |
||
257 | * Normalizes given path with given directory separator. |
||
258 | * Default forced to Unix directory separator for substitutePaths to work properly in Windows. |
||
259 | * @param string $path path to be normalized |
||
260 | * @param string $ds directory separator. |
||
261 | * @return string |
||
262 | */ |
||
263 | public static function normalizePath($path, $ds = self::UNIX_DS) |
||
267 | |||
268 | /** |
||
269 | * Substitute all paths in given array recursively with alias if applicable. |
||
270 | * @param array $data |
||
271 | * @param string $dir |
||
272 | * @param string $alias |
||
273 | * @return array |
||
274 | */ |
||
275 | public static function substitutePaths($data, $dir, $alias) |
||
287 | |||
288 | /** |
||
289 | * Substitute path with alias if applicable. |
||
290 | * @param string $path |
||
291 | * @param string $dir |
||
292 | * @param string $alias |
||
293 | * @return string |
||
294 | */ |
||
295 | protected static function substitutePath($path, $dir, $alias) |
||
306 | |||
307 | public function loadConfig($name) |
||
311 | |||
312 | /** |
||
313 | * Reads config file. |
||
314 | * @param string $path |
||
315 | * @return array configuration read from file |
||
316 | */ |
||
317 | public function loadFile($path) |
||
323 | |||
324 | public function setIo(IOInterface $io) |
||
328 | |||
329 | protected function writeError($text) |
||
337 | |||
338 | public function getVars() |
||
342 | } |
||
343 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: