Complex classes like Console_GetoptPlus_Getopt 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 Console_GetoptPlus_Getopt, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
100 | class Console_GetoptPlus_Getopt |
||
101 | { |
||
102 | /** |
||
103 | * The list of ambigous option names |
||
104 | * |
||
105 | * @var array |
||
106 | * @access private |
||
107 | */ |
||
108 | private $ambigous; |
||
109 | |||
110 | /** |
||
111 | * The command arguments |
||
112 | * |
||
113 | * @var array |
||
114 | * @access private |
||
115 | */ |
||
116 | private $args; |
||
117 | |||
118 | /** |
||
119 | * The long option names |
||
120 | * |
||
121 | * @var array |
||
122 | * @access private |
||
123 | */ |
||
124 | private $longOptionsDef; |
||
125 | |||
126 | /** |
||
127 | * The parsed options |
||
128 | * |
||
129 | * @var array |
||
130 | * @access private |
||
131 | */ |
||
132 | private $options; |
||
133 | |||
134 | /** |
||
135 | * The option shortcut names |
||
136 | * |
||
137 | * @var array |
||
138 | * @access private |
||
139 | */ |
||
140 | private $shortcuts; |
||
141 | |||
142 | /** |
||
143 | * The short option names and their definition |
||
144 | * |
||
145 | * @var array |
||
146 | * @access private |
||
147 | */ |
||
148 | private $shortOptionsDef; |
||
149 | |||
150 | /** |
||
151 | * The option types |
||
152 | * |
||
153 | * @var array |
||
154 | * @access private |
||
155 | */ |
||
156 | private $type = array(// / |
||
157 | false => 'noarg', |
||
158 | '=' => 'mandatory', ':' => 'mandatory', |
||
159 | '==' => 'optional', '::' => 'optional', |
||
160 | ); |
||
161 | |||
162 | /** |
||
163 | * Creates the option shorcut names |
||
164 | * |
||
165 | * @param array $longOptionsDef the long option names |
||
166 | * @param string $ambiguity directive to handle option names ambiguity |
||
167 | * @return array the option shorcuts and the ambigous options |
||
168 | * @access public |
||
169 | */ |
||
170 | public function createShorcuts($longOptionsDef, $ambiguity) |
||
204 | |||
205 | /** |
||
206 | * Parses the command line |
||
207 | * |
||
208 | * See getopt() for a complete description. |
||
209 | * |
||
210 | * @param numeric $version the getopt version: 1 or 2 |
||
211 | * @param array $args the arguments |
||
212 | * @param string $shortOptions the short options definition, e.g. "ab:c::" |
||
213 | * @param array $longOptions the long options definition |
||
214 | * @param string $ambiguity directive to handle option names ambiguity |
||
215 | * @return array the parsed options, their arguments and parameters |
||
216 | * @access public |
||
217 | * @static |
||
218 | */ |
||
219 | public static function doGetopt($version = null, $args = array(), |
||
227 | |||
228 | /** |
||
229 | * Wraps the exception call |
||
230 | * |
||
231 | * @return void |
||
232 | * @access private |
||
233 | * @throws Console_GetoptPlus_Exception Exception |
||
234 | * @static |
||
235 | */ |
||
236 | private static function exception() |
||
241 | |||
242 | /** |
||
243 | * Parses the command line |
||
244 | * |
||
245 | * See the definition/example in the class Doc Block. |
||
246 | * |
||
247 | * Example: returning an index array |
||
248 | * <code> |
||
249 | * array( |
||
250 | * [0] => array("foo" => null, "bar" => "car", "c" => null), |
||
251 | * [1] => array([0] => "param1", [1] => "param2") |
||
252 | * ); |
||
253 | * </code> |
||
254 | * |
||
255 | * @param array $args the arguments |
||
256 | * @param string $shortOptions the short options definition, e.g. "ab:c::" |
||
257 | * <ul> |
||
258 | * <li>":" : the option requires an argument</li> |
||
259 | * <li>"::" : the option accepts an optional argument</li> |
||
260 | * <li>otherwise the option accepts no argument</li> |
||
261 | * </ul> |
||
262 | * @param array $longOptions the long options definition, |
||
263 | * e.g. array("art", "bar=", "car==) |
||
264 | * <ul> |
||
265 | * <li>"=" : the option requires an argument</li> |
||
266 | * <li>"==" : the option accepts an optional argument</li> |
||
267 | * <li>otherwise the option accepts no argument</li> |
||
268 | * </ul> |
||
269 | * @param string $ambiguity directive to handle option names ambiguity, |
||
270 | * e.g. "--foo" and "--foobar": |
||
271 | * <ul> |
||
272 | * <li>"loose": allowed if "--foo" does not |
||
273 | * accept an argument, this is the default |
||
274 | * behaviour</li> |
||
275 | * <li>"strict": no ambiguity allowed</li> |
||
276 | * <li>"shortcuts": implies "strict", the use of |
||
277 | * partial option names is allowed, |
||
278 | * e.g. "--f" or "--fo" instead of "--foo"</li> |
||
279 | * </ul> |
||
280 | * @return array the parsed options, their arguments and parameters |
||
281 | * @access public |
||
282 | * @static |
||
283 | */ |
||
284 | public static function getopt($args = array(), $shortOptions = '', |
||
289 | |||
290 | /** |
||
291 | * Parses the command line |
||
292 | * |
||
293 | * See getopt() for a complete description. |
||
294 | * |
||
295 | * @param array $args the arguments |
||
296 | * @param string $shortOptions the short options definition, e.g. "ab:c::" |
||
297 | * @param array $longOptions the long options definition |
||
298 | * @param string $ambiguity directive to handle option names ambiguity |
||
299 | * @return array the parsed options, their arguments and parameters |
||
300 | * @access public |
||
301 | * @static |
||
302 | */ |
||
303 | public static function getopt2($args = array(), $shortOptions = '', |
||
308 | |||
309 | /** |
||
310 | * Checks if the argument is an option |
||
311 | * |
||
312 | * @param string $argument the argument, e.g. "-f" or "--foo" |
||
313 | * @return boolean true if an option, false otherwise |
||
314 | * @access public |
||
315 | */ |
||
316 | public function isOption($argument) |
||
320 | |||
321 | /** |
||
322 | * Parses a long option |
||
323 | * |
||
324 | * @param string $argument the option and argument (excluding the "--" prefix), |
||
325 | * e.g. "file=foo.php", "file foo.php", "bar" |
||
326 | * @return void |
||
327 | * @access public |
||
328 | */ |
||
329 | public function parseLongOption($argument) |
||
353 | |||
354 | /** |
||
355 | * Parses the long option names and types |
||
356 | * |
||
357 | * @param array $options the long options, e.g. array("foo", "bar=") |
||
358 | * @return array the options name and type, |
||
359 | * e.g. array("foo"=>"noarg", "bar"=>"mandatory") |
||
360 | * @access public |
||
361 | */ |
||
362 | public function parseLongOptionsDef($options) |
||
386 | |||
387 | /** |
||
388 | * Parses a short option |
||
389 | * |
||
390 | * @param string $argument the option and argument (excluding the "-" prefix), |
||
391 | * e.g. "zfoo.php", "z foo.php", "z". |
||
392 | * @return void |
||
393 | * @access public |
||
394 | */ |
||
395 | public function parseShortOption($argument) |
||
431 | |||
432 | /** |
||
433 | * Parses the short option names and types |
||
434 | * |
||
435 | * @param string $options the short options, e.g. array("ab:c::) |
||
436 | * @return array the options name and type, |
||
437 | * e.g. array("a"=>"noarg", "b"=>"mandatory", "c"=>"optional") |
||
438 | * @access public |
||
439 | */ |
||
440 | public function parseShortOptionsDef($options) |
||
465 | |||
466 | /** |
||
467 | * Parses the command line |
||
468 | * |
||
469 | * See getopt() for a complete description. |
||
470 | * |
||
471 | * @param array $args the arguments |
||
472 | * @param string $shortOptions the short options definition, e.g. "ab:c::" |
||
473 | * @param array $longOptions the long options definition, e.g. array("foo", "bar=") |
||
474 | * @param string $ambiguity directive to handle option names ambiguity |
||
475 | * @param numeric $version the getopt version: 1 or 2 |
||
476 | * @return array the parsed options, their arguments and parameters |
||
477 | * @access public |
||
478 | */ |
||
479 | public function process($args = array(), $shortOptions, $longOptions, |
||
537 | |||
538 | /** |
||
539 | * Reads the command arguments |
||
540 | * |
||
541 | * @return array the arguments |
||
542 | * @access public |
||
543 | * @static |
||
544 | */ |
||
545 | public static function readPHPArgv() |
||
556 | |||
557 | /** |
||
558 | * Verifies there is no ambiguity with option names |
||
559 | * |
||
560 | * @param array $longOptionsDef the long options names and their types |
||
561 | * @param string $ambiguity directive to handle option names ambiguity, |
||
562 | * See getopt() for a complete description |
||
563 | * @return boolean no ambiguity if true, false otherwise |
||
564 | * @access public |
||
565 | */ |
||
566 | public function verifyNoAmbiguity($longOptionsDef, $ambiguity) |
||
589 | } |
||
590 | |||
592 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.