Complex classes like Package 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 Package, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class Package extends Abstracts\Package implements \Pickle\Base\Interfaces\Package |
||
43 | { |
||
44 | /** |
||
45 | * @var string Package's root directory |
||
46 | */ |
||
47 | protected $path; |
||
48 | |||
49 | /** |
||
50 | * Get the package's root directory. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | public function getRootDir() |
||
58 | |||
59 | /** |
||
60 | * Get the package's root directory. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getSourceDir() |
||
65 | { |
||
66 | 1 | $path = $this->getRootDir(); |
|
67 | 1 | $release = $path.DIRECTORY_SEPARATOR.$this->getPrettyName().'-'.$this->getPrettyVersion(); |
|
68 | |||
69 | 1 | if (is_dir($release)) { |
|
70 | 1 | $path = $release; |
|
71 | } |
||
72 | |||
73 | /* Do subdir search */ |
||
74 | 1 | if (!$this->extConfigIsIn($path)) { |
|
75 | 1 | $path = $this->locateSourceDirByExtConfig($path); |
|
76 | |||
77 | 1 | if (null === $path) { |
|
78 | throw new \Exception('config*.(m4|w32) not found'); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | 1 | return $path; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Set the package's source directory, containing config.m4/config.w32. |
||
87 | * |
||
88 | * @param string $path |
||
89 | */ |
||
90 | public function setRootDir($path) |
||
94 | |||
95 | public function setStability($stability) |
||
99 | |||
100 | /** |
||
101 | * @return array |
||
102 | */ |
||
103 | public function getConfigureOptions() |
||
104 | { |
||
105 | 1 | $options = []; |
|
106 | |||
107 | 1 | if (defined('PHP_WINDOWS_VERSION_MAJOR')) { |
|
108 | 1 | $config_file = $this->getSourceDir().'/config.w32'; |
|
109 | |||
110 | 1 | if (!file_exists($config_file)) { |
|
111 | throw new \Exception('cnofig.w32 not found'); |
||
112 | } |
||
113 | |||
114 | 1 | $config = file_get_contents($config_file); |
|
115 | |||
116 | 1 | $options = array_merge( |
|
117 | 1 | $this->fetchArgWindows('ARG_WITH', $config), |
|
118 | 1 | $this->fetchArgWindows('ARG_ENABLE', $config) |
|
119 | ); |
||
120 | } else { |
||
121 | 1 | $configs = glob($this->getSourceDir().'/'.'config*.m4'); |
|
122 | |||
123 | 1 | if (!empty($configs)) { |
|
124 | 1 | foreach ($configs as $config) { |
|
125 | 1 | $options = array_merge($options, $this->getConfigureOptionsFromFile($config)); |
|
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | 1 | return $options; |
|
131 | } |
||
132 | |||
133 | public function getConfigureOptionsFromFile($file) |
||
134 | { |
||
135 | 1 | $config = file_get_contents($file); |
|
136 | |||
137 | 1 | return array_merge( |
|
138 | 1 | $this->fetchArg('PHP_ARG_WITH', $config), |
|
139 | 1 | $this->fetchArgAc('AC_ARG_WITH', $config), |
|
140 | 1 | $this->fetchArg('PHP_ARG_ENABLE', $config), |
|
141 | 1 | $this->fetchArgAc('AC_ARG_ENABLE', $config) |
|
142 | ); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $which |
||
147 | * @param string $config |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function fetchArgWindows($which, $config) |
||
152 | { |
||
153 | 1 | $next = 0; |
|
154 | 1 | $options = []; |
|
155 | 1 | $type = false !== strpos($which, 'ENABLE') ? 'enable' : 'with'; |
|
156 | 1 | while (false !== ($s = strpos($config, $which, $next))) { |
|
157 | 1 | $s = strpos($config, '(', $s); |
|
158 | 1 | $e = strpos($config, ')', $s + 1); |
|
159 | 1 | $option = substr($config, $s + 1, $e - $s); |
|
160 | |||
161 | 1 | $elems = explode(',', $option); |
|
162 | 1 | array_walk($elems, function (&$a) { |
|
163 | 1 | $a = str_replace([')', "'"], ['', ''], $a); |
|
164 | 1 | $a = trim($a); |
|
165 | 1 | }); |
|
166 | |||
167 | 1 | @list($name, $prompt, $default) = $elems; |
|
|
|||
168 | 1 | $name = str_replace('"', '', $name); |
|
169 | 1 | $options[$name] = (object) [ |
|
170 | 1 | 'prompt' => $prompt, |
|
171 | 1 | 'type' => $type, |
|
172 | 1 | 'default' => $default, |
|
173 | ]; |
||
174 | 1 | $next = $e + 1; |
|
175 | } |
||
176 | |||
177 | 1 | return $options; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param string $which |
||
182 | * @param string $config |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | protected function fetchArgAc($which, $config) |
||
187 | { |
||
188 | 1 | $next = 0; |
|
189 | 1 | $options = []; |
|
190 | 1 | $type = false !== strpos($which, 'ENABLE') ? 'enable' : 'with'; |
|
191 | 1 | while (false !== ($s = strpos($config, $which, $next))) { |
|
192 | 1 | $default = true; |
|
193 | 1 | $s = strpos($config, '(', $s); |
|
194 | 1 | $e = strpos($config, ')', $s + 1); |
|
195 | 1 | $option = substr($config, $s + 1, $e - $s); |
|
196 | |||
197 | 1 | if ('enable' == $type) { |
|
198 | 1 | $default = (false !== strpos($option, '-disable-')) ? true : false; |
|
199 | 1 | } elseif ('with' == $type) { |
|
200 | 1 | $default = (false !== strpos($option, '-without-')) ? true : false; |
|
201 | } |
||
202 | |||
203 | 1 | list($name, $desc) = explode(',', $option); |
|
204 | |||
205 | 1 | $desc = preg_replace('/\s+/', ' ', trim($desc)); |
|
206 | 1 | $desc = trim(substr($desc, 1, strlen($desc) - 2)); |
|
207 | 1 | $s_a = strpos($desc, ' '); |
|
208 | 1 | $desc = trim(substr($desc, $s_a)); |
|
209 | |||
210 | 1 | $options[$name] = (object) [ |
|
211 | 1 | 'prompt' => $desc, |
|
212 | 1 | 'type' => $type, |
|
213 | 1 | 'default' => $default, |
|
214 | ]; |
||
215 | |||
216 | 1 | $next = $e + 1; |
|
217 | } |
||
218 | |||
219 | 1 | return $options; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param string $which |
||
224 | * @param string $config |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function fetchArg($which, $config) |
||
229 | { |
||
230 | 1 | $next = 0; |
|
231 | 1 | $options = []; |
|
232 | |||
233 | 1 | $type = false !== strpos($which, 'ENABLE') ? 'enable' : 'with'; |
|
234 | 1 | while (false !== ($s = strpos($config, $which, $next))) { |
|
235 | 1 | $default = 'y'; |
|
236 | 1 | $s = strpos($config, '(', $s); |
|
237 | 1 | $e = strpos($config, ')', $s + 1); |
|
238 | 1 | $option = substr($config, $s + 1, $e - $s); |
|
239 | |||
240 | 1 | list($name, $desc) = explode(',', $option); |
|
241 | |||
242 | /* Description can be part of the 3rd argument */ |
||
243 | 1 | if (empty($desc) || $desc === '[]') { |
|
244 | 1 | list($name, , $desc) = explode(',', $option); |
|
245 | 1 | $desc = preg_replace('/\s+/', ' ', trim($desc)); |
|
246 | 1 | $desc = trim(substr($desc, 1, strlen($desc) - 2)); |
|
247 | 1 | $desc = trim(str_replace(['[', ']'], ['', ''], $desc)); |
|
248 | 1 | $s_a = strpos($desc, ' '); |
|
249 | 1 | $desc = trim(substr($desc, $s_a)); |
|
250 | } |
||
251 | |||
252 | 1 | if ('enable' == $type) { |
|
253 | 1 | $default = (false !== strpos($option, '-disable-')) ? true : false; |
|
254 | 1 | } elseif ('with' == $type) { |
|
255 | 1 | $default = (false !== strpos($option, '-without-')) ? true : false; |
|
256 | } |
||
257 | 1 | $name = str_replace(['[', ']'], ['', ''], $name); |
|
258 | 1 | $options[$name] = (object) [ |
|
259 | 1 | 'prompt' => trim($desc), |
|
260 | 1 | 'type' => $type, |
|
261 | 1 | 'default' => $default, |
|
262 | ]; |
||
263 | 1 | $next = $e + 1; |
|
264 | } |
||
265 | |||
266 | 1 | return $options; |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * Get files, will not return gitignore files. |
||
271 | * |
||
272 | * @return \CallbackFilterIterator |
||
273 | */ |
||
274 | public function getFiles() |
||
275 | { |
||
276 | return new \CallbackFilterIterator( |
||
277 | new \RecursiveIteratorIterator( |
||
278 | new \RecursiveDirectoryIterator($this->getSourceDir()) |
||
279 | ), |
||
280 | new GitIgnore($this) |
||
281 | ); |
||
282 | } |
||
283 | |||
284 | public function getVersionFromHeader() |
||
285 | { |
||
286 | $headers = glob($this->path.DIRECTORY_SEPARATOR.'*.h'); |
||
287 | $version_define = 'PHP_'.strtoupper($this->getSimpleName()).'_VERSION'; |
||
288 | foreach ($headers as $header) { |
||
289 | $contents = @file_get_contents($header); |
||
290 | if (!$contents) { |
||
291 | throw new \Exception("Cannot read header <$header>"); |
||
292 | } |
||
293 | $pos_version = strpos($contents, $version_define); |
||
294 | if ($pos_version !== false) { |
||
295 | $nl = strpos($contents, "\n", $pos_version); |
||
296 | $version_line = trim(substr($contents, $pos_version, $nl - $pos_version)); |
||
297 | list($version_define, $version) = explode(' ', $version_line); |
||
298 | $version = trim(str_replace('"', '', $version)); |
||
299 | break; |
||
300 | } |
||
301 | } |
||
302 | if (empty($version)) { |
||
303 | throw new \Exception('No '.$version_define.' can be found'); |
||
304 | } |
||
305 | |||
306 | return [trim($version_define), $version]; |
||
307 | } |
||
308 | |||
309 | protected function extConfigIsIn($path) |
||
319 | |||
320 | protected function locateSourceDirByExtConfig($path) |
||
321 | { |
||
322 | 1 | $it = new \RecursiveIteratorIterator( |
|
323 | 1 | new \RecursiveDirectoryIterator($path), |
|
324 | 1 | \RecursiveIteratorIterator::SELF_FIRST |
|
325 | ); |
||
326 | |||
327 | 1 | foreach ($it as $fl_obj) { |
|
335 | } |
||
336 | |||
338 |
If you suppress an error, we recommend checking for the error condition explicitly: