| Conditions | 40 |
| Paths | 397 |
| Total Lines | 68 |
| 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 |
||
| 43 | public static function toRegex(string $glob, bool $strictLeadingDot = true, bool $strictWildcardSlash = true, string $delimiter = '#') |
||
| 44 | { |
||
| 45 | $firstByte = true; |
||
| 46 | $escaping = false; |
||
| 47 | $inCurlies = 0; |
||
| 48 | $regex = ''; |
||
| 49 | $sizeGlob = \strlen($glob); |
||
| 50 | for ($i = 0; $i < $sizeGlob; ++$i) { |
||
| 51 | $car = $glob[$i]; |
||
| 52 | if ($firstByte && $strictLeadingDot && '.' !== $car) { |
||
| 53 | $regex .= '(?=[^\.])'; |
||
| 54 | } |
||
| 55 | |||
| 56 | $firstByte = '/' === $car; |
||
| 57 | |||
| 58 | if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) { |
||
| 59 | $car = '[^/]++/'; |
||
| 60 | if (!isset($glob[$i + 3])) { |
||
| 61 | $car .= '?'; |
||
| 62 | } |
||
| 63 | |||
| 64 | if ($strictLeadingDot) { |
||
| 65 | $car = '(?=[^\.])'.$car; |
||
| 66 | } |
||
| 67 | |||
| 68 | $car = '/(?:'.$car.')*'; |
||
| 69 | $i += 2 + isset($glob[$i + 3]); |
||
| 70 | |||
| 71 | if ('/' === $delimiter) { |
||
| 72 | $car = str_replace('/', '\\/', $car); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) { |
||
| 77 | $regex .= "\\$car"; |
||
| 78 | } elseif ('*' === $car) { |
||
| 79 | $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*'); |
||
| 80 | } elseif ('?' === $car) { |
||
| 81 | $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.'); |
||
| 82 | } elseif ('{' === $car) { |
||
| 83 | $regex .= $escaping ? '\\{' : '('; |
||
| 84 | if (!$escaping) { |
||
| 85 | ++$inCurlies; |
||
| 86 | } |
||
| 87 | } elseif ('}' === $car && $inCurlies) { |
||
| 88 | $regex .= $escaping ? '}' : ')'; |
||
| 89 | if (!$escaping) { |
||
| 90 | --$inCurlies; |
||
| 91 | } |
||
| 92 | } elseif (',' === $car && $inCurlies) { |
||
| 93 | $regex .= $escaping ? ',' : '|'; |
||
| 94 | } elseif ('\\' === $car) { |
||
| 95 | if ($escaping) { |
||
| 96 | $regex .= '\\\\'; |
||
| 97 | $escaping = false; |
||
| 98 | } else { |
||
| 99 | $escaping = true; |
||
| 100 | } |
||
| 101 | |||
| 102 | continue; |
||
| 103 | } else { |
||
| 104 | $regex .= $car; |
||
| 105 | } |
||
| 106 | $escaping = false; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $delimiter.'^'.$regex.'$'.$delimiter; |
||
| 110 | } |
||
| 111 | } |
||
| 112 |