Complex classes like CLI_Action 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 CLI_Action, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CLI_Action { |
||
9 | |||
10 | var $name; |
||
11 | var $description; |
||
12 | var $module; |
||
13 | var $soap; |
||
14 | var $params; |
||
15 | var $soapCommand; |
||
16 | function CLI_Action($name, $description) { |
||
17 | $this->name = $name; |
||
18 | $this->soapCommand = $name; |
||
19 | $this->description = $description; |
||
20 | $this->params = array(); |
||
21 | $this->addParam(array( |
||
22 | 'name' => 'quiet', |
||
23 | 'description' => '--quiet or -q Quiet-mode. Suppress result output.', |
||
24 | 'parameters' => array('q', 'quiet'), |
||
25 | 'value_required' => false, |
||
26 | 'soap' => false, |
||
27 | )); |
||
28 | $this->addProjectParam(); |
||
29 | } |
||
30 | function addProjectParam() { |
||
31 | $this->addParam(array( |
||
32 | 'name' => 'group_id', |
||
33 | 'description' => '--project=<name> Name of the project the item belongs to. If you specified the name of |
||
34 | the working project when you logged in, this parameter is not needed.', |
||
35 | 'parameters' => array('project'), |
||
36 | )); |
||
37 | } |
||
38 | function getName() { |
||
56 | function help() { |
||
57 | $help = $this->getName() .":\n"; |
||
58 | $help .= $this->getDescription() ."\n\n"; |
||
59 | $help .= "Available parameters:\n"; |
||
60 | foreach($this->params as $param) { |
||
61 | $help .= " ". $param['description'] ."\n"; |
||
62 | } |
||
63 | $help .= "\n --help Show this screen\n"; |
||
64 | return $help; |
||
65 | } |
||
66 | |||
67 | function soapResult($params, $soap_result, $fieldnames = array(), $loaded_params = array()) { |
||
70 | function loadParams($params) { |
||
71 | $all_params = array('soap' => array(), 'others' => array()); |
||
72 | foreach($this->params as $param) { |
||
73 | $search_for = $param['name']; |
||
74 | if (isset($param['parameters'])) { |
||
75 | $search_for = $param['parameters']; |
||
76 | } |
||
77 | $type = 'soap'; |
||
78 | if (isset($param['soap']) && !$param['soap']) { |
||
79 | $type = 'others'; |
||
80 | } |
||
81 | $value_required = !isset($param['value_required']) || $param['value_required']; |
||
82 | if (isset($param['method'])) { |
||
83 | $t = call_user_func_array($param['method'], array(&$params)); |
||
84 | } else { |
||
85 | $t = $this->module->getParameter($params, $search_for, $value_required); |
||
86 | } |
||
87 | if (!method_exists($this, 'validate_'. $param['name']) |
||
88 | || |
||
89 | call_user_func_array(array($this, 'validate_'. $param['name']), array(&$t)) |
||
90 | ) { |
||
91 | $all_params[$type][$param['name']] = $t; |
||
92 | } |
||
93 | } |
||
94 | return $all_params; |
||
95 | } |
||
96 | function confirmation($params) { |
||
109 | /*function soapError() { |
||
110 | if (($error = $GLOBALS['soap']->getError())) { |
||
111 | $GLOBALS['LOG']->add($GLOBALS['soap']->responseData); |
||
112 | exit_error($error, $GLOBALS['soap']->faultcode); |
||
113 | } |
||
114 | }*/ |
||
115 | |||
116 | function execute($params) { |
||
136 | |||
137 | function validate_group_id(&$group_id) { |
||
141 | |||
142 | function user_confirm($msg) { |
||
146 | /** |
||
147 | * show_output - Format a SOAP result in order to display it on the user's screen |
||
148 | * |
||
149 | * extracted from GForge Command-line Interface |
||
150 | * contained in GForge. |
||
151 | * Copyright 2005 GForge, LLC |
||
152 | * http://gforge.org/ |
||
153 | * |
||
154 | * @param array Result of a SOAP call |
||
155 | * @param array Titles to assign to each field (optional). |
||
156 | */ |
||
157 | function show_output($result, $fieldnames = array()) { |
||
158 | // There are 3 types of output: a scalar (int, string), a vector or a matrix (table-like). |
||
159 | // Try to guess which output is the best for $result |
||
160 | if (is_object($result)) { |
||
161 | |||
162 | $this->show_object($result, $fieldnames); |
||
163 | |||
164 | } elseif(is_array($result)) { |
||
165 | |||
166 | if (count($result) == 0) { |
||
167 | echo "No results\n"; |
||
168 | return; |
||
169 | } |
||
170 | |||
171 | if (isset($result[0]) && is_array($result[0])) { |
||
172 | $this->show_matrix($result, $fieldnames); |
||
173 | } elseif (isset($result[0]) && is_object($result[0])) { |
||
174 | $this->show_matrix($result, $fieldnames); |
||
175 | } else { |
||
176 | $this->show_vector($result, $fieldnames); |
||
177 | } |
||
178 | } else { |
||
179 | $this->show_scalar($result, $fieldnames); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Check if the object returned by a SOAP call is valid |
||
185 | * @see CLI_Action::show_object() |
||
186 | * |
||
187 | * @param array $row Result of a SOAP call |
||
188 | * |
||
189 | * @return Boolean |
||
190 | */ |
||
191 | private function isIterable($row) { |
||
194 | |||
195 | function show_object($result, $fieldnames = array()) { |
||
196 | $titles = array(); |
||
197 | $lengths = array(); |
||
198 | |||
199 | // this is for showing multidimensional arrays |
||
200 | static $recursive_id = 1; |
||
201 | |||
202 | foreach ($result as $colname => $value) { |
||
203 | if (!isset($titles[$colname])) { |
||
204 | if (!isset($fieldnames[$colname])) { |
||
205 | $titles[$colname] = $colname; |
||
206 | } else { |
||
207 | $titles[$colname] = $fieldnames[$colname]; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | if (!is_array($value)) { |
||
212 | if (!isset($lengths[$colname]) || $lengths[$colname] < strlen($value)+2) { |
||
213 | $lengths[$colname] = max(strlen($value), strlen($titles[$colname])); |
||
214 | $lengths[$colname] += 2; |
||
215 | } |
||
216 | } else { |
||
217 | $lengths[$colname] = strlen($titles[$colname]) + 2; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | // show the titles |
||
222 | foreach ($titles as $colname => $title) { |
||
223 | $length = $lengths[$colname]; |
||
224 | echo "+".str_repeat("-", $length); |
||
225 | } |
||
226 | echo "+\n"; |
||
227 | foreach ($titles as $colname => $title) { |
||
228 | $length = $lengths[$colname]; |
||
229 | echo "|".$this->center_text($title, $length); |
||
230 | } |
||
231 | echo "|\n"; |
||
232 | foreach ($titles as $colname => $title) { |
||
233 | $length = $lengths[$colname]; |
||
234 | echo "+".str_repeat("-", $length); |
||
235 | } |
||
236 | echo "+\n"; |
||
237 | |||
238 | $recursive_items = array(); |
||
239 | // now show the values |
||
240 | foreach ($result as $colname => $value) { |
||
241 | // recursively show the multi dimensional array |
||
242 | if (is_array($value)) { |
||
243 | if (array_key_exists($colname, $fieldnames)) $rec_titles = $fieldnames[$colname]; |
||
244 | else $rec_titles = array(); |
||
245 | $recursive_items[$recursive_id] = array("titles" => $rec_titles, "values" => $value); |
||
246 | // show the reference # instead |
||
247 | $value = "[".$recursive_id."]"; |
||
248 | $recursive_id++; |
||
249 | } |
||
250 | |||
251 | $length = $lengths[$colname]; |
||
252 | if (is_array($value)) continue; |
||
253 | echo "| ".$value.str_repeat(" ", $length-strlen($value)-1); |
||
254 | } |
||
255 | echo "|\n"; |
||
256 | |||
257 | |||
258 | // show last line |
||
259 | foreach ($titles as $colname => $title) { |
||
260 | $length = $lengths[$colname]; |
||
261 | echo "+".str_repeat("-", $length); |
||
262 | } |
||
263 | echo "+\n"; |
||
264 | |||
265 | // now recursively show the multidimensional array |
||
266 | foreach ($recursive_items as $id => $item) { |
||
267 | echo "\n"; |
||
268 | echo "[".$id."]:\n"; |
||
269 | $this->show_output($item["values"], $item["titles"]); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | |||
274 | function show_objects($result, $fieldnames = array()) { |
||
275 | $titles = array(); |
||
276 | $lengths = array(); |
||
277 | |||
278 | // this is for showing multidimensional arrays |
||
279 | static $recursive_id = 1; |
||
280 | |||
281 | foreach ($result as $row) { |
||
282 | foreach ($row as $colname => $value) { |
||
283 | if (!isset($titles[$colname])) { |
||
284 | if (!isset($fieldnames[$colname])) { |
||
285 | $titles[$colname] = $colname; |
||
286 | } else { |
||
287 | $titles[$colname] = $fieldnames[$colname]; |
||
288 | } |
||
289 | } |
||
290 | |||
291 | if (!is_array($value)) { |
||
292 | if (!isset($lengths[$colname]) || $lengths[$colname] < strlen($value)+2) { |
||
293 | $lengths[$colname] = max(strlen($value), strlen($titles[$colname])); |
||
294 | $lengths[$colname] += 2; |
||
295 | } |
||
296 | } else { |
||
297 | $lengths[$colname] = strlen($titles[$colname]) + 2; |
||
298 | } |
||
299 | } |
||
300 | } |
||
301 | |||
302 | // show the titles |
||
303 | foreach ($titles as $colname => $title) { |
||
304 | $length = $lengths[$colname]; |
||
305 | echo "+".str_repeat("-", $length); |
||
306 | } |
||
307 | echo "+\n"; |
||
308 | foreach ($titles as $colname => $title) { |
||
309 | $length = $lengths[$colname]; |
||
310 | echo "|".$this->center_text($title, $length); |
||
311 | } |
||
312 | echo "|\n"; |
||
313 | foreach ($titles as $colname => $title) { |
||
314 | $length = $lengths[$colname]; |
||
315 | echo "+".str_repeat("-", $length); |
||
316 | } |
||
317 | echo "+\n"; |
||
318 | |||
319 | $recursive_items = array(); |
||
320 | // now show the values |
||
321 | foreach ($result as $row) { |
||
322 | foreach ($row as $colname => $value) { |
||
323 | // recursively show the multi dimensional array |
||
324 | if (is_array($value)) { |
||
325 | if (array_key_exists($colname, $fieldnames)) $rec_titles = $fieldnames[$colname]; |
||
326 | else $rec_titles = array(); |
||
327 | $recursive_items[$recursive_id] = array("titles" => $rec_titles, "values" => $value); |
||
328 | // show the reference # instead |
||
329 | $value = "[".$recursive_id."]"; |
||
330 | $recursive_id++; |
||
331 | } |
||
332 | |||
333 | $length = $lengths[$colname]; |
||
334 | if (is_array($value)) continue; |
||
335 | echo "| ".$value.str_repeat(" ", $length-strlen($value)-1); |
||
336 | } |
||
337 | echo "|\n"; |
||
338 | } |
||
339 | |||
340 | // show last line |
||
341 | foreach ($titles as $colname => $title) { |
||
342 | $length = $lengths[$colname]; |
||
343 | echo "+".str_repeat("-", $length); |
||
344 | } |
||
345 | echo "+\n"; |
||
346 | |||
347 | // now recursively show the multidimensional array |
||
348 | foreach ($recursive_items as $id => $item) { |
||
349 | echo "\n"; |
||
350 | echo "[".$id."]:\n"; |
||
351 | $this->show_output($item["values"], $item["titles"]); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * extracted from GForge Command-line Interface |
||
357 | * contained in GForge. |
||
358 | * Copyright 2005 GForge, LLC |
||
359 | * http://gforge.org/ |
||
360 | * |
||
361 | */ |
||
362 | function show_scalar($result, $fieldnames = array()) { |
||
363 | $title = (isset($fieldnames[0])) ? $fieldnames[0] : "Result"; |
||
364 | // convert to string (may be an int) |
||
365 | $result = "$result"; |
||
366 | $length = max(strlen($result), strlen($title)); |
||
367 | $length = $length + 2; // +2 for having spaces at the beginning and the end |
||
368 | |||
369 | // show the title |
||
370 | echo "+".str_repeat("-", $length)."+\n"; |
||
371 | echo "|".$this->center_text($title, $length)."|\n"; |
||
372 | echo "+".str_repeat("-", $length)."+\n"; |
||
373 | |||
374 | // show the item |
||
375 | echo "| ".$result.str_repeat(" ", $length-strlen($result)-1)."|\n"; |
||
376 | echo "+".str_repeat("-", $length)."+\n"; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * extracted from GForge Command-line Interface |
||
381 | * contained in GForge. |
||
382 | * Copyright 2005 GForge, LLC |
||
383 | * http://gforge.org/ |
||
384 | * |
||
385 | */ |
||
386 | function show_vector($result, $fieldnames = array()) { |
||
387 | // There are two types of vector: those that are a set of items and those |
||
388 | // that are just like a row in the database. For the second case, reuse the |
||
389 | // show_matrix function considering the vector as a 1-row matrix |
||
390 | if (!isset($result[0])) { |
||
391 | // This happens when $result is an indexed array (i.e. $result["fieldname"]. In this |
||
392 | // case consider $result as a 1 row matrix |
||
393 | $foo_matrix = array(); |
||
394 | $foo_matrix[] = $result; |
||
395 | $this->show_matrix($foo_matrix, $fieldnames); |
||
396 | return; |
||
397 | } |
||
398 | |||
399 | $title = (isset($fieldnames[0])) ? $fieldnames[0] : "Result"; |
||
400 | |||
401 | $length = strlen($title); |
||
402 | // get the maximum length for a single item |
||
403 | foreach ($result as $item) { |
||
404 | if (is_array($item)) continue; // shouldn't happen |
||
405 | $length = max(strlen($item), $length); |
||
406 | } |
||
407 | $length = $length + 2; // +2 for having spaces at the beginning and the end |
||
408 | |||
409 | // show the title |
||
410 | echo "+".str_repeat("-", $length)."+\n"; |
||
411 | echo "|".$this->center_text($title, $length)."|\n"; |
||
412 | echo "+".str_repeat("-", $length)."+\n"; |
||
413 | |||
414 | // show each item |
||
415 | foreach ($result as $item) { |
||
416 | echo "| ".$item.str_repeat(" ", $length-strlen($item)-1)."|\n"; |
||
417 | } |
||
418 | |||
419 | // show last line |
||
420 | echo "+".str_repeat("-", $length)."+\n"; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * extracted from GForge Command-line Interface |
||
425 | * contained in GForge. |
||
426 | * Copyright 2005 GForge, LLC |
||
427 | * http://gforge.org/ |
||
428 | * |
||
429 | */ |
||
430 | function show_matrix($result, $fieldnames = array()) { |
||
539 | /** |
||
540 | * center_text - Given a text and a length, returns a string of length $lenght with $text located in |
||
541 | * the middle (the string is padded with whitespaces). |
||
542 | * |
||
543 | * extracted from GForge Command-line Interface |
||
544 | * contained in GForge. |
||
545 | * Copyright 2005 GForge, LLC |
||
546 | * http://gforge.org/ |
||
547 | * |
||
548 | * @param string |
||
549 | * @param int |
||
550 | */ |
||
551 | function center_text($text, $length) { |
||
552 | if (strlen($text) >= $length) return $text; |
||
553 | $delta = $length - strlen($text); |
||
554 | $pad_left = floor($delta/2); |
||
555 | $pad_right = $delta - $pad_left; |
||
556 | return str_repeat(" ", $pad_left).$text.str_repeat(" ", $pad_right); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * get_group_id - Given a group UNIX name, returns the group ID or 0 if the group doesn't exists |
||
561 | * |
||
562 | * extracted from GForge Command-line Interface |
||
563 | * contained in GForge. |
||
564 | * Copyright 2005 GForge, LLC |
||
565 | * http://gforge.org/ |
||
566 | * |
||
567 | * @param string UNIX name of the project |
||
568 | */ |
||
569 | function get_group_id($unix_group_name) { |
||
587 | var $cached_group_id; |
||
588 | |||
589 | /** |
||
590 | * get_working_group - Return the ID of the group the user is currently working with. The name of the group can be defined |
||
591 | * on the session or in the parameters. |
||
592 | * |
||
593 | * extracted from GForge Command-line Interface |
||
594 | * contained in GForge. |
||
595 | * Copyright 2005 GForge, LLC |
||
596 | * http://gforge.org/ |
||
597 | * |
||
598 | * @param array An array of parameters to look for the defined group. If the group isn't in the parameters, looks in the session |
||
599 | * @param bool Specify if we should abort the program if the group isn't found |
||
600 | */ |
||
601 | function get_working_group($group_name, $die=true) { |
||
617 | |||
618 | /** |
||
619 | * check_date - Check if a date entered by the user is correctly formatted and it is valid. |
||
620 | * |
||
621 | * extracted from GForge Command-line Interface |
||
622 | * contained in GForge. |
||
623 | * Copyright 2005 GForge, LLC |
||
624 | * http://gforge.org/ |
||
625 | * |
||
626 | * @param string Date |
||
627 | * @return string String with the error (if any) |
||
628 | */ |
||
629 | function check_date($date) { |
||
645 | |||
646 | /** |
||
647 | * convert_date - Convert a date entered by the user in format YYYY-MM-DD to a timestamp |
||
648 | * to be inserted in the database. |
||
649 | * |
||
650 | * extracted from GForge Command-line Interface |
||
651 | * contained in GForge. |
||
652 | * Copyright 2005 GForge, LLC |
||
653 | * http://gforge.org/ |
||
654 | * |
||
655 | * This function assumes the date has the correct format |
||
656 | * @param string Date |
||
657 | * @return int |
||
658 | */ |
||
659 | function convert_date($date) { |
||
669 | |||
670 | } |
||
671 | |||
673 |