Conditions | 41 |
Paths | 114 |
Total Lines | 105 |
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 |
||
156 | private static function toRegEx(string $glob) : string |
||
157 | { |
||
158 | $delimiter = '~'; |
||
159 | $inSquare = false; |
||
160 | $curlyLevels = 0; |
||
161 | $regex = ''; |
||
162 | $length = strlen($glob); |
||
163 | for ($i = 0; $i < $length; ++$i) { |
||
164 | $c = $glob[$i]; |
||
165 | switch ($c) { |
||
166 | case '.': |
||
167 | case '(': |
||
168 | case ')': |
||
169 | case '|': |
||
170 | case '+': |
||
171 | case '^': |
||
172 | case '$': |
||
173 | case $delimiter: |
||
174 | $regex .= '\\' . $c; |
||
175 | break; |
||
176 | case '/': |
||
177 | if (self::isRecursiveWildcard($glob, $i)) { |
||
178 | $regex .= '/([^/]+/)*'; |
||
179 | $i += 3; |
||
180 | } else { |
||
181 | $regex .= '/'; |
||
182 | } |
||
183 | break; |
||
184 | case '*': |
||
185 | $regex .= '[^/]*'; |
||
186 | break; |
||
187 | case '?': |
||
188 | $regex .= '.'; |
||
189 | break; |
||
190 | case '{': |
||
191 | $regex .= '('; |
||
192 | ++$curlyLevels; |
||
193 | break; |
||
194 | case '}': |
||
195 | if ($curlyLevels > 0) { |
||
196 | $regex .= ')'; |
||
197 | --$curlyLevels; |
||
198 | } else { |
||
199 | $regex .= '}'; |
||
200 | } |
||
201 | break; |
||
202 | case ',': |
||
203 | $regex .= $curlyLevels > 0 ? '|' : ','; |
||
204 | break; |
||
205 | case '[': |
||
206 | $regex .= '['; |
||
207 | $inSquare = true; |
||
208 | if (isset($glob[$i + 1]) && $glob[$i + 1] === '^') { |
||
209 | $regex .= '^'; |
||
210 | ++$i; |
||
211 | } |
||
212 | break; |
||
213 | case ']': |
||
214 | $regex .= $inSquare ? ']' : '\\]'; |
||
215 | $inSquare = false; |
||
216 | break; |
||
217 | case '-': |
||
218 | $regex .= $inSquare ? '-' : '\\-'; |
||
219 | break; |
||
220 | case '\\': |
||
221 | if (isset($glob[$i + 1])) { |
||
222 | switch ($glob[$i + 1]) { |
||
223 | case '*': |
||
224 | case '?': |
||
225 | case '{': |
||
226 | case '}': |
||
227 | case '[': |
||
228 | case ']': |
||
229 | case '-': |
||
230 | case '^': |
||
231 | case '$': |
||
232 | case '~': |
||
233 | case '\\': |
||
234 | $regex .= '\\' . $glob[$i + 1]; |
||
235 | ++$i; |
||
236 | break; |
||
237 | default: |
||
238 | $regex .= '\\\\'; |
||
239 | } |
||
240 | } |
||
241 | break; |
||
242 | default: |
||
243 | $regex .= $c; |
||
244 | break; |
||
245 | } |
||
246 | } |
||
247 | if ($inSquare) { |
||
248 | throw new InvalidArgumentException(sprintf( |
||
249 | 'Invalid glob: missing ] in %s', |
||
250 | $glob |
||
251 | )); |
||
252 | } |
||
253 | if ($curlyLevels > 0) { |
||
254 | throw new InvalidArgumentException(sprintf( |
||
255 | 'Invalid glob: missing } in %s', |
||
256 | $glob |
||
257 | )); |
||
258 | } |
||
259 | return $delimiter . '^' . $regex . '$' . $delimiter; |
||
260 | } |
||
261 | } |
||
262 |