| 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 |
||
| 219 | private static function toRegEx(string $glob) : string |
||
| 220 | { |
||
| 221 | $delimiter = '~'; |
||
| 222 | $inSquare = false; |
||
| 223 | $curlyLevels = 0; |
||
| 224 | $regex = ''; |
||
| 225 | $length = strlen($glob); |
||
| 226 | for ($i = 0; $i < $length; ++$i) { |
||
| 227 | $c = $glob[$i]; |
||
| 228 | switch ($c) { |
||
| 229 | case '.': |
||
| 230 | case '(': |
||
| 231 | case ')': |
||
| 232 | case '|': |
||
| 233 | case '+': |
||
| 234 | case '^': |
||
| 235 | case '$': |
||
| 236 | case $delimiter: |
||
| 237 | $regex .= '\\' . $c; |
||
| 238 | break; |
||
| 239 | case '/': |
||
| 240 | if (self::isRecursiveWildcard($glob, $i)) { |
||
| 241 | $regex .= '/([^/]+/)*'; |
||
| 242 | $i += 3; |
||
| 243 | } else { |
||
| 244 | $regex .= '/'; |
||
| 245 | } |
||
| 246 | break; |
||
| 247 | case '*': |
||
| 248 | $regex .= '[^/]*'; |
||
| 249 | break; |
||
| 250 | case '?': |
||
| 251 | $regex .= '.'; |
||
| 252 | break; |
||
| 253 | case '{': |
||
| 254 | $regex .= '('; |
||
| 255 | ++$curlyLevels; |
||
| 256 | break; |
||
| 257 | case '}': |
||
| 258 | if ($curlyLevels > 0) { |
||
| 259 | $regex .= ')'; |
||
| 260 | --$curlyLevels; |
||
| 261 | } else { |
||
| 262 | $regex .= '}'; |
||
| 263 | } |
||
| 264 | break; |
||
| 265 | case ',': |
||
| 266 | $regex .= $curlyLevels > 0 ? '|' : ','; |
||
| 267 | break; |
||
| 268 | case '[': |
||
| 269 | $regex .= '['; |
||
| 270 | $inSquare = true; |
||
| 271 | if (isset($glob[$i + 1]) && $glob[$i + 1] === '^') { |
||
| 272 | $regex .= '^'; |
||
| 273 | ++$i; |
||
| 274 | } |
||
| 275 | break; |
||
| 276 | case ']': |
||
| 277 | $regex .= $inSquare ? ']' : '\\]'; |
||
| 278 | $inSquare = false; |
||
| 279 | break; |
||
| 280 | case '-': |
||
| 281 | $regex .= $inSquare ? '-' : '\\-'; |
||
| 282 | break; |
||
| 283 | case '\\': |
||
| 284 | if (isset($glob[$i + 1])) { |
||
| 285 | switch ($glob[$i + 1]) { |
||
| 286 | case '*': |
||
| 287 | case '?': |
||
| 288 | case '{': |
||
| 289 | case '}': |
||
| 290 | case '[': |
||
| 291 | case ']': |
||
| 292 | case '-': |
||
| 293 | case '^': |
||
| 294 | case '$': |
||
| 295 | case '~': |
||
| 296 | case '\\': |
||
| 297 | $regex .= '\\' . $glob[$i + 1]; |
||
| 298 | ++$i; |
||
| 299 | break; |
||
| 300 | default: |
||
| 301 | $regex .= '\\\\'; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | break; |
||
| 305 | default: |
||
| 306 | $regex .= $c; |
||
| 307 | break; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | if ($inSquare) { |
||
| 311 | throw new InvalidArgumentException(sprintf( |
||
| 312 | 'Invalid glob: missing ] in %s', |
||
| 313 | $glob |
||
| 314 | )); |
||
| 315 | } |
||
| 316 | if ($curlyLevels > 0) { |
||
| 317 | throw new InvalidArgumentException(sprintf( |
||
| 318 | 'Invalid glob: missing } in %s', |
||
| 319 | $glob |
||
| 320 | )); |
||
| 321 | } |
||
| 322 | return $delimiter . '^' . $regex . '$' . $delimiter; |
||
| 323 | } |
||
| 324 | |||
| 351 |