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) |
||
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 | /** |
||
130 | * Merges given configs and writes at given name. |
||
131 | * @param mixed $name |
||
132 | * @param array $configs |
||
133 | */ |
||
134 | public function buildConfig($name, array $configs) |
||
144 | |||
145 | protected function isSpecialConfig($name) |
||
149 | |||
150 | /** |
||
151 | * Writes config file by name. |
||
152 | * @param string $name |
||
153 | * @param array $data |
||
154 | */ |
||
155 | public function writeConfig($name, array $data) |
||
160 | |||
161 | public function getOutputPath($name) |
||
165 | |||
166 | /** |
||
167 | * Writes config file by full path. |
||
168 | * @param string $path |
||
169 | * @param array $data |
||
170 | */ |
||
171 | public static function writeFile($path, array $data) |
||
181 | |||
182 | /** |
||
183 | * Writes file if content changed. |
||
184 | * @param string $path |
||
185 | * @param string $content |
||
186 | */ |
||
187 | public static function putFile($path, $content) |
||
193 | |||
194 | /** |
||
195 | * Substitute all pathes in given array recursively with alias if applicable. |
||
196 | * @param array $data |
||
197 | * @param string $dir |
||
198 | * @param string $alias |
||
199 | * @return string |
||
200 | */ |
||
201 | public static function substitutePathes($data, $dir, $alias) |
||
213 | |||
214 | /** |
||
215 | * Substitute path with alias if applicable. |
||
216 | * @param string $path |
||
217 | * @param string $dir |
||
218 | * @param string $alias |
||
219 | * @return string |
||
220 | */ |
||
221 | protected static function substitutePath($path, $dir, $alias) |
||
231 | |||
232 | public function readConfig($name) |
||
236 | |||
237 | /** |
||
238 | * Reads config file. |
||
239 | * @param string $__path |
||
240 | * @return array configuration read from file |
||
241 | */ |
||
242 | public function readFile($__path) |
||
243 | { |
||
244 | $__skippable = strncmp($__path, '?', 1) === 0 ? '?' : ''; |
||
245 | if ($__skippable) { |
||
246 | $__path = substr($__path, 1); |
||
247 | } |
||
248 | |||
249 | if (file_exists($__path)) { |
||
250 | /// Expose variables to be used in configs |
||
251 | extract($this->vars); |
||
252 | |||
253 | return (array) require $__path; |
||
254 | } |
||
255 | |||
256 | if (empty($__skippable)) { |
||
257 | $this->writeError("Failed read file $__path"); |
||
258 | } |
||
259 | |||
260 | return []; |
||
261 | } |
||
262 | |||
263 | public function setIo(IOInterface $io) |
||
267 | |||
268 | protected function writeError($text) |
||
276 | } |
||
277 |