Conditions | 34 |
Paths | 348 |
Total Lines | 120 |
Code Lines | 76 |
Lines | 6 |
Ratio | 5 % |
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 |
||
110 | private function expandMatch(array $matches) |
||
111 | { |
||
112 | static $rfc1738to3986 = array( |
||
113 | '+' => '%20', |
||
114 | '%7e' => '~' |
||
115 | ); |
||
116 | |||
117 | $parsed = self::parseExpression($matches[1]); |
||
118 | $replacements = array(); |
||
119 | |||
120 | $prefix = $parsed['operator']; |
||
121 | $joiner = $parsed['operator']; |
||
122 | $useQueryString = false; |
||
123 | if ($parsed['operator'] == '?') { |
||
124 | $joiner = '&'; |
||
125 | $useQueryString = true; |
||
126 | } elseif ($parsed['operator'] == '&') { |
||
127 | $useQueryString = true; |
||
128 | } elseif ($parsed['operator'] == '#') { |
||
129 | $joiner = ','; |
||
130 | } elseif ($parsed['operator'] == ';') { |
||
131 | $useQueryString = true; |
||
132 | } elseif ($parsed['operator'] == '' || $parsed['operator'] == '+') { |
||
133 | $joiner = ','; |
||
134 | $prefix = ''; |
||
135 | } |
||
136 | |||
137 | foreach ($parsed['values'] as $value) { |
||
138 | |||
139 | if (!array_key_exists($value['value'], $this->variables) || $this->variables[$value['value']] === null) { |
||
140 | continue; |
||
141 | } |
||
142 | |||
143 | $variable = $this->variables[$value['value']]; |
||
144 | $actuallyUseQueryString = $useQueryString; |
||
145 | $expanded = ''; |
||
146 | |||
147 | if (is_array($variable)) { |
||
148 | |||
149 | $isAssoc = $this->isAssoc($variable); |
||
150 | $kvp = array(); |
||
151 | foreach ($variable as $key => $var) { |
||
152 | |||
153 | if ($isAssoc) { |
||
154 | $key = rawurlencode($key); |
||
155 | $isNestedArray = is_array($var); |
||
156 | } else { |
||
157 | $isNestedArray = false; |
||
158 | } |
||
159 | |||
160 | if (!$isNestedArray) { |
||
161 | $var = rawurlencode($var); |
||
162 | View Code Duplication | if ($parsed['operator'] == '+' || $parsed['operator'] == '#') { |
|
|
|||
163 | $var = $this->decodeReserved($var); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | if ($value['modifier'] == '*') { |
||
168 | if ($isAssoc) { |
||
169 | if ($isNestedArray) { |
||
170 | // Nested arrays must allow for deeply nested structures |
||
171 | $var = strtr(http_build_query(array($key => $var)), $rfc1738to3986); |
||
172 | } else { |
||
173 | $var = $key . '=' . $var; |
||
174 | } |
||
175 | } elseif ($key > 0 && $actuallyUseQueryString) { |
||
176 | $var = $value['value'] . '=' . $var; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | $kvp[$key] = $var; |
||
181 | } |
||
182 | |||
183 | if (empty($variable)) { |
||
184 | $actuallyUseQueryString = false; |
||
185 | } elseif ($value['modifier'] == '*') { |
||
186 | $expanded = implode($joiner, $kvp); |
||
187 | if ($isAssoc) { |
||
188 | // Don't prepend the value name when using the explode modifier with an associative array |
||
189 | $actuallyUseQueryString = false; |
||
190 | } |
||
191 | } else { |
||
192 | if ($isAssoc) { |
||
193 | // When an associative array is encountered and the explode modifier is not set, then the |
||
194 | // result must be a comma separated list of keys followed by their respective values. |
||
195 | foreach ($kvp as $k => &$v) { |
||
196 | $v = $k . ',' . $v; |
||
197 | } |
||
198 | } |
||
199 | $expanded = implode(',', $kvp); |
||
200 | } |
||
201 | |||
202 | } else { |
||
203 | if ($value['modifier'] == ':') { |
||
204 | $variable = substr($variable, 0, $value['position']); |
||
205 | } |
||
206 | $expanded = rawurlencode($variable); |
||
207 | View Code Duplication | if ($parsed['operator'] == '+' || $parsed['operator'] == '#') { |
|
208 | $expanded = $this->decodeReserved($expanded); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | if ($actuallyUseQueryString) { |
||
213 | if (!$expanded && $joiner != '&') { |
||
214 | $expanded = $value['value']; |
||
215 | } else { |
||
216 | $expanded = $value['value'] . '=' . $expanded; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | $replacements[] = $expanded; |
||
221 | } |
||
222 | |||
223 | $ret = implode($joiner, $replacements); |
||
224 | if ($ret && $prefix) { |
||
225 | return $prefix . $ret; |
||
226 | } |
||
227 | |||
228 | return $ret; |
||
229 | } |
||
230 | |||
255 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.