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 OUTPUT_DIR_SUFFIX = '-output'; |
||
50 | const BASE_DIR_MARKER = '<<<base-dir>>>'; |
||
51 | |||
52 | public function __construct(array $files = [], $outputDir = null) |
||
57 | |||
58 | public function setFiles(array $files) |
||
62 | |||
63 | public function setOutputDir($outputDir) |
||
67 | |||
68 | public function setAddition(array $addition) |
||
72 | |||
73 | public function loadFiles() |
||
78 | |||
79 | public function saveFiles() |
||
84 | |||
85 | public static function rebuild($outputDir = null) |
||
91 | |||
92 | /** |
||
93 | * Returns default output dir. |
||
94 | * @param string $vendor path to vendor dir |
||
95 | * @return string |
||
96 | */ |
||
97 | public static function findOutputDir($vendor = null) |
||
107 | |||
108 | /** |
||
109 | * Returns full path to assembled config file. |
||
110 | * @param string $filename name of config |
||
111 | * @param string $vendor path to vendor dir |
||
112 | * @return string absolute path |
||
113 | */ |
||
114 | public static function path($filename, $vendor = null) |
||
118 | |||
119 | /** |
||
120 | * Builds configs by given files list. |
||
121 | * @param null|array $files files to process: config name => list of files |
||
122 | */ |
||
123 | public function buildConfigs($files = null) |
||
139 | |||
140 | protected function loadConfigs(array $paths) |
||
152 | |||
153 | /** |
||
154 | * Merges given configs and writes at given name. |
||
155 | * @param mixed $name |
||
156 | * @param array $configs |
||
157 | */ |
||
158 | public function buildConfig($name, array $configs, $defines = []) |
||
171 | |||
172 | protected function pushEnvVars($vars) |
||
184 | |||
185 | protected function isSpecialConfig($name) |
||
189 | |||
190 | /** |
||
191 | * Writes config file by name. |
||
192 | * @param string $name |
||
193 | * @param array $data |
||
194 | */ |
||
195 | public function writeConfig($name, array $data, array $defines = []) |
||
204 | |||
205 | public function getOutputPath($name) |
||
209 | |||
210 | /** |
||
211 | * Writes config file by full path. |
||
212 | * @param string $path |
||
213 | * @param array $data |
||
214 | */ |
||
215 | public static function writeFile($path, array $data, array $defines = []) |
||
225 | |||
226 | /** |
||
227 | * Writes file if content changed. |
||
228 | * @param string $path |
||
229 | * @param string $content |
||
230 | */ |
||
231 | protected static function putFile($path, $content) |
||
240 | |||
241 | /** |
||
242 | * Substitute output paths in given data array recursively with marker. |
||
243 | * @param array $data |
||
244 | * @return array |
||
245 | */ |
||
246 | public function substituteOutputDirs($data) |
||
250 | |||
251 | /** |
||
252 | * Substitute all paths in given array recursively with alias if applicable. |
||
253 | * @param array $data |
||
254 | * @param string $dir |
||
255 | * @param string $alias |
||
256 | * @return array |
||
257 | */ |
||
258 | public static function substitutePaths($data, $dir, $alias) |
||
270 | |||
271 | /** |
||
272 | * Substitute path with alias if applicable. |
||
273 | * @param string $path |
||
274 | * @param string $dir |
||
275 | * @param string $alias |
||
276 | * @return string |
||
277 | */ |
||
278 | protected static function substitutePath($path, $dir, $alias) |
||
288 | |||
289 | public function loadConfig($name) |
||
293 | |||
294 | /** |
||
295 | * Reads config file. |
||
296 | * @param string $path |
||
297 | * @return array configuration read from file |
||
298 | */ |
||
299 | public function loadFile($path) |
||
305 | |||
306 | public function setIo(IOInterface $io) |
||
310 | |||
311 | protected function writeError($text) |
||
319 | |||
320 | public function getVars() |
||
324 | } |
||
325 |
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: