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) |
||
186 | |||
187 | protected function isSpecialConfig($name) |
||
191 | |||
192 | /** |
||
193 | * Writes config file by name. |
||
194 | * @param string $name |
||
195 | * @param array $data |
||
196 | */ |
||
197 | public function writeConfig($name, array $data, array $defines = []) |
||
206 | |||
207 | public function getOutputPath($name) |
||
211 | |||
212 | /** |
||
213 | * Writes config file by full path. |
||
214 | * @param string $path |
||
215 | * @param array $data |
||
216 | */ |
||
217 | public static function writeFile($path, array $data, array $defines = []) |
||
227 | |||
228 | /** |
||
229 | * Writes file if content changed. |
||
230 | * @param string $path |
||
231 | * @param string $content |
||
232 | */ |
||
233 | protected static function putFile($path, $content) |
||
242 | |||
243 | /** |
||
244 | * Substitute output paths in given data array recursively with marker. |
||
245 | * @param array $data |
||
246 | * @return array |
||
247 | */ |
||
248 | public function substituteOutputDirs($data) |
||
254 | |||
255 | public static function normalizeDir($path, $ds = '/') |
||
256 | { |
||
257 | return rtrim(strtr($path, '\/', $ds), $ds); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Substitute all paths in given array recursively with alias if applicable. |
||
262 | * @param array $data |
||
263 | * @param string $dir |
||
264 | * @param string $alias |
||
265 | * @return array |
||
266 | */ |
||
267 | public static function substitutePaths($data, $dir, $alias) |
||
279 | |||
280 | /** |
||
281 | * Substitute path with alias if applicable. |
||
282 | * @param string $path |
||
283 | * @param string $dir |
||
284 | * @param string $alias |
||
285 | * @return string |
||
286 | */ |
||
287 | protected static function substitutePath($path, $dir, $alias) |
||
297 | |||
298 | public function loadConfig($name) |
||
302 | |||
303 | /** |
||
304 | * Reads config file. |
||
305 | * @param string $path |
||
306 | * @return array configuration read from file |
||
307 | */ |
||
308 | public function loadFile($path) |
||
314 | |||
315 | public function setIo(IOInterface $io) |
||
319 | |||
320 | protected function writeError($text) |
||
328 | |||
329 | public function getVars() |
||
333 | } |
||
334 |
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: