Complex classes like Config 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.
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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Config implements ConfigInterface |
||
22 | { |
||
23 | protected $cacheDir; |
||
24 | protected $installDir; |
||
25 | protected $filesystem; |
||
26 | protected $basePackagesUrl = 'http://bower.herokuapp.com/packages/'; |
||
27 | protected $allPackagesUrl = 'https://bower-component-list.herokuapp.com/'; |
||
28 | protected $saveToBowerJsonFile = false; |
||
29 | protected $bowerFileNames = ['bower.json', 'package.json']; |
||
30 | protected $stdBowerFileName = 'bower.json'; |
||
31 | protected $scripts = ['preinstall'=>[],'postinstall'=>[],'preuninstall'=>[]]; |
||
32 | |||
33 | /** |
||
34 | * @param Filesystem $filesystem |
||
35 | */ |
||
36 | public function __construct(Filesystem $filesystem) |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function getScripts() |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function getBasePackagesUrl() |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function getAllPackagesUrl() |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function getCacheDir() |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function getInstallDir() |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function isSaveToBowerJsonFile() |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function setSaveToBowerJsonFile($flag = true) |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function initBowerJsonFile(array $params) |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | public function updateBowerJsonFile(PackageInterface $package) |
||
145 | { |
||
146 | if (!$this->isSaveToBowerJsonFile()) { |
||
147 | return 0; |
||
148 | } |
||
149 | |||
150 | $decode = $this->getBowerFileContent(); |
||
151 | $decode['dependencies'][$package->getName()] = $package->getRequiredVersion(); |
||
152 | $file = getcwd() . '/' . $this->stdBowerFileName; |
||
153 | $json = json_encode($decode, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); |
||
154 | |||
155 | return $this->filesystem->write($file, $json); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function updateBowerJsonFile2(array $old, array $new) |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function getBowerFileContent() |
||
173 | { |
||
174 | if (!$this->bowerFileExists()) { |
||
175 | throw new RuntimeException('No ' . $this->stdBowerFileName . ' found. You can run "init" command to create it.'); |
||
176 | } |
||
177 | $bowerJson = $this->filesystem->read(getcwd() . '/' . $this->stdBowerFileName); |
||
178 | if (empty($bowerJson) || !is_array($decode = json_decode($bowerJson, true))) { |
||
179 | throw new RuntimeException(sprintf('Malformed JSON in %s: %s.', $this->stdBowerFileName, $bowerJson)); |
||
180 | } |
||
181 | |||
182 | return $decode; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function getOverridesSection() |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | public function getOverrideFor($packageName) |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function getPackageBowerFileContent(PackageInterface $package) |
||
217 | { |
||
218 | $file = $this->getInstallDir() . '/' . $package->getName() . '/.bower.json'; |
||
219 | if (!$this->filesystem->exists($file)) { |
||
220 | throw new RuntimeException(sprintf('Could not find .bower.json file for package %s.', $package->getName())); |
||
221 | } |
||
222 | $bowerJson = $this->filesystem->read($file); |
||
223 | $bower = json_decode($bowerJson, true); |
||
224 | if (is_null($bower)) { |
||
225 | throw new RuntimeException(sprintf('Invalid content in .bower.json for package %s.', $package->getName())); |
||
226 | } |
||
227 | |||
228 | return $bower; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function bowerFileExists() |
||
238 | |||
239 | /** |
||
240 | * @param array $params |
||
241 | * @return array |
||
242 | */ |
||
243 | protected function createAClearBowerFile(array $params) |
||
257 | |||
258 | /** |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function getHomeDir() |
||
278 | } |
||
279 |
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.