Conditions | 13 |
Paths | 22 |
Total Lines | 124 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
35 | public static function process(array $argv) |
||
36 | { |
||
37 | if(count($argv) < 1) |
||
38 | { |
||
39 | throw(new ArghException(__CLASS__ . ': $argv is empty')); |
||
40 | } |
||
41 | |||
42 | // |
||
43 | // Remove first element from $argv, it contains the name of the php script |
||
44 | // |
||
45 | |||
46 | $args = array_slice($argv, 1); |
||
47 | |||
48 | // |
||
49 | // Lists |
||
50 | // |
||
51 | |||
52 | for($i=0; $i<count($args); $i++) |
||
|
|||
53 | { |
||
54 | // Lists begin with an opening bracket '[' |
||
55 | if( strpos($args[$i], '[') !== FALSE ) |
||
56 | { |
||
57 | // Found the beginning of a list at $i |
||
58 | //echo "DEBUG: Found beginning of a list at $i\n"; |
||
59 | |||
60 | // Search for the end of the list; starting at the current element |
||
61 | for($j=$i; $j<count($args); $j++) |
||
62 | { |
||
63 | if( strpos($args[$j], ']') !== FALSE ) |
||
64 | { |
||
65 | // Found the end of a list at $j |
||
66 | //echo "DEBUG: Found end of a list at $j\n"; |
||
67 | |||
68 | if($j != $i) |
||
69 | { |
||
70 | // List does NOT open and close in the same element; elements must be re-combined |
||
71 | |||
72 | // List closing bracket should always be the last character of the element |
||
73 | if( strpos($args[$j], ']') == (strlen($args[$j])-1) ) |
||
74 | { |
||
75 | // Create a new string for $args $i thru $j |
||
76 | $list = ""; |
||
77 | for($k=$i; $k<=$j; $k++) $list .= $args[$k]; |
||
78 | |||
79 | //echo "DEBUG: Replace elements $i thru $j with: $list\n"; |
||
80 | |||
81 | // Replace $args from $i thru $j with $list |
||
82 | array_splice($args, $i, ($j-$i)+1, $list); |
||
83 | |||
84 | // Stop searching for the end of the list |
||
85 | break; |
||
86 | } |
||
87 | else |
||
88 | { |
||
89 | throw new ArghException(__METHOD__ . ': Syntax Error: Invalid list.'); |
||
90 | } |
||
91 | |||
92 | } // END: if($j != $i) |
||
93 | else |
||
94 | { |
||
95 | //echo "DEBUG: List is already contained in a single element.\n"; |
||
96 | } |
||
97 | |||
98 | } // END: if( ($closeAt = strpos($args[$j], ']')) !== FALSE ) |
||
99 | |||
100 | } // END: for($j=$i; $j<count($args); $j++) |
||
101 | |||
102 | } // END: if( strpos($args[$i], ' ') !== FALSE ) |
||
103 | |||
104 | } // END: for($i=0; $i<count($args); $i++) |
||
105 | |||
106 | // |
||
107 | // Quotes |
||
108 | // |
||
109 | |||
110 | // Check for arguments with spaces, these were originally quoted on the command line |
||
111 | for($i=0; $i<count($args); $i++) |
||
112 | { |
||
113 | if( strpos($args[$i], ' ') !== FALSE ) |
||
114 | { |
||
115 | |||
116 | // Check if argument is a list |
||
117 | if( strpos($args[$i], '[') !== FALSE ) |
||
118 | { |
||
119 | |||
120 | // |
||
121 | // DO NOT ADD QUOTES SURROUNDING ITEMS IN A LIST |
||
122 | // THEY WILL JUST HAVE TO BE STRIPPED LATER |
||
123 | // |
||
124 | |||
125 | } // END: if( strpos($args[$i], '[') !== FALSE ) |
||
126 | else |
||
127 | { |
||
128 | //!TODO: handle --msg="Hello World"; We don't want "--msg=Hello World" |
||
129 | //! TODO: FIX. THIS IS BROKEN |
||
130 | |||
131 | if( strpos($args[$i], '=') === FALSE ) |
||
132 | { |
||
133 | // Wrap (space containing) argument in quotes |
||
134 | $args[$i] = "'" . $args[$i] . "'"; |
||
135 | } |
||
136 | else |
||
137 | { |
||
138 | // Wrap (space containing) argument value - after '=' sign in quotes |
||
139 | |||
140 | // Split argument into parts, delimted by '=' |
||
141 | $tokens = explode('=', $args[$i]); |
||
142 | |||
143 | $args[$i] = $tokens[0] . '=' . "'" . $tokens[1] . "'"; |
||
144 | } |
||
145 | |||
146 | |||
147 | } |
||
148 | |||
149 | } // END: if( strpos($args[$i], ' ') !== FALSE ) |
||
150 | |||
151 | } // END: for($i=0; $i<count($args); $i++) |
||
152 | |||
153 | // DEBUG |
||
154 | //echo "\n------- AFTER PRE PROCESSING --------\n"; |
||
155 | //print_r($args); |
||
156 | //echo "\n-------------------------------------\n\n"; |
||
157 | |||
158 | return $args; |
||
159 | } |
||
161 | } |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: