| Conditions | 27 |
| Paths | 25 |
| Total Lines | 73 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 56 | public static function convertGlobToRegEx($globPattern) |
||
| 57 | { |
||
| 58 | // Remove beginning and ending * globs because they're useless |
||
| 59 | $globPattern = trim($globPattern, '*'); |
||
| 60 | $escaping = false; |
||
| 61 | $inCurrent = 0; |
||
| 62 | $chars = str_split($globPattern); |
||
| 63 | $regexPattern = ''; |
||
| 64 | foreach ($chars as $currentChar) { |
||
| 65 | switch ($currentChar) { |
||
| 66 | case '*': |
||
| 67 | $regexPattern .= ($escaping ? "\\*" : '.*'); |
||
| 68 | $escaping = false; |
||
| 69 | break; |
||
| 70 | case '?': |
||
| 71 | $regexPattern .= ($escaping ? "\\?" : '.'); |
||
| 72 | $escaping = false; |
||
| 73 | break; |
||
| 74 | case '.': |
||
| 75 | case '(': |
||
| 76 | case ')': |
||
| 77 | case '+': |
||
| 78 | case '|': |
||
| 79 | case '^': |
||
| 80 | case '$': |
||
| 81 | case '@': |
||
| 82 | case '%': |
||
| 83 | $regexPattern .= '\\' . $currentChar; |
||
| 84 | $escaping = false; |
||
| 85 | break; |
||
| 86 | case '\\': |
||
| 87 | if ($escaping) { |
||
| 88 | $regexPattern .= "\\\\"; |
||
| 89 | $escaping = false; |
||
| 90 | } else { |
||
| 91 | $escaping = true; |
||
| 92 | } |
||
| 93 | break; |
||
| 94 | case '{': |
||
| 95 | if ($escaping) { |
||
| 96 | $regexPattern .= "\\{"; |
||
| 97 | } else { |
||
| 98 | $regexPattern = '('; |
||
| 99 | $inCurrent++; |
||
| 100 | } |
||
| 101 | $escaping = false; |
||
| 102 | break; |
||
| 103 | case '}': |
||
| 104 | if ($inCurrent > 0 && !$escaping) { |
||
| 105 | $regexPattern .= ')'; |
||
| 106 | $inCurrent--; |
||
| 107 | } elseif ($escaping) { |
||
| 108 | $regexPattern = "\\}"; |
||
| 109 | } else { |
||
| 110 | $regexPattern = "}"; |
||
| 111 | } |
||
| 112 | $escaping = false; |
||
| 113 | break; |
||
| 114 | case ',': |
||
| 115 | if ($inCurrent > 0 && !$escaping) { |
||
| 116 | $regexPattern .= '|'; |
||
| 117 | } elseif ($escaping) { |
||
| 118 | $regexPattern .= "\\,"; |
||
| 119 | } else { |
||
| 120 | $regexPattern = ","; |
||
| 121 | } |
||
| 122 | break; |
||
| 123 | default: |
||
| 124 | $escaping = false; |
||
| 125 | $regexPattern .= $currentChar; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | return $regexPattern; |
||
| 129 | } |
||
| 248 |