Conditions | 19 |
Paths | 42 |
Total Lines | 60 |
Code Lines | 40 |
Lines | 0 |
Ratio | 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 |
||
94 | function getParameter(&$parameter_array, $parameter, $require_value=false) { |
||
95 | for ($i=0; $i < count($parameter_array); $i++) { |
||
|
|||
96 | $res = array(); |
||
97 | if (preg_match("/^\\-\\-(.+)/s",$parameter_array[$i],$res)) { // several-character parameter? (IE, "--username=john") |
||
98 | $passed_string = $res[1]; |
||
99 | // is it --parameter=value or just --parameter? |
||
100 | $res = preg_split("/=(.+)/", $passed_string, -1, PREG_SPLIT_DELIM_CAPTURE); |
||
101 | if (isset($res[1])) { |
||
102 | $passed_parameter = $res[0]; |
||
103 | $passed_value = $res[1]; |
||
104 | $has_value = true; |
||
105 | } else { |
||
106 | $passed_parameter = $passed_string; |
||
107 | $has_value = false; |
||
108 | } |
||
109 | |||
110 | if (!is_array($parameter)) $search_array = array($parameter); |
||
111 | else $search_array = $parameter; |
||
112 | |||
113 | foreach ($search_array as $alias) { |
||
114 | if ($alias == $passed_parameter) { // Match |
||
115 | if ($has_value) return $passed_value; |
||
116 | else if ($require_value) return null; // Requires a value but none is passed |
||
117 | else return true; // notify parameter was passed |
||
118 | } |
||
119 | } |
||
120 | |||
121 | } else if (preg_match("/^\\-(.+)/s",$parameter_array[$i],$res)) { // Single character parameter? (IE "-z") or a group of flags (IE "-zxvf") |
||
122 | $passed_parameter = $res[1]; |
||
123 | if (strlen($passed_parameter) == 1) { // Some flag like "-x" or parameter "-U username" |
||
124 | // Check to see if there is a value associated to this parameter, like in "-U username". |
||
125 | // To do this, we must see the following string in the parameter array |
||
126 | if (($i+1) < count($parameter_array) && !preg_match("/^\\-/", $parameter_array[$i+1])) { |
||
127 | $i++; // position in value |
||
128 | $passed_value = $parameter_array[$i]; |
||
129 | $has_value = true; |
||
130 | } else { |
||
131 | $has_value = false; |
||
132 | } |
||
133 | } else { // Several flags grouped into one string like "-zxvf" |
||
134 | $has_value = false; |
||
135 | } |
||
136 | |||
137 | if (!is_array($parameter)) $search_array = array($parameter); |
||
138 | else $search_array = $parameter; |
||
139 | |||
140 | foreach ($search_array as $alias) { |
||
141 | if (strlen($alias) == 1) { |
||
142 | if (strpos($passed_parameter, $alias) !== false) { // Found a match |
||
143 | if ($has_value) return $passed_value; |
||
144 | else if ($require_value) return null; |
||
145 | else return true; // indicates that the flag was set |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | return null; |
||
153 | } |
||
154 | /** |
||
181 | ?> |
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: