| Conditions | 10 |
| Paths | 18 |
| Total Lines | 47 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 33 | private static function do_tokenize(string $pattern): array |
||
| 34 | { |
||
| 35 | $tokens = []; |
||
| 36 | $is_literal = false; |
||
| 37 | $literal = ''; |
||
| 38 | $z = mb_strlen($pattern); |
||
| 39 | |||
| 40 | for ($i = 0; $i < $z; ++$i) { |
||
| 41 | $c = mb_substr($pattern, $i, 1); |
||
| 42 | |||
| 43 | if ($c === self::QUOTE) { |
||
| 44 | // Two adjacent single vertical quotes (''), which represent a literal single quote, |
||
| 45 | // either inside or outside a quoted text. |
||
| 46 | if (mb_substr($pattern, $i + 1, 1) === self::QUOTE) { |
||
| 47 | $i++; |
||
| 48 | $literal .= self::QUOTE; |
||
| 49 | } else { |
||
| 50 | // Toggle literal |
||
| 51 | $is_literal = !$is_literal; |
||
| 52 | } |
||
| 53 | } elseif ($is_literal) { |
||
| 54 | $literal .= $c; |
||
| 55 | } elseif (ctype_alpha($c)) { |
||
| 56 | if ($literal) { |
||
| 57 | $tokens[] = $literal; |
||
| 58 | $literal = ''; |
||
| 59 | } |
||
| 60 | |||
| 61 | for ($j = $i + 1; $j < $z; ++$j) { |
||
| 62 | $nc = mb_substr($pattern, $j, 1); |
||
| 63 | if ($nc !== $c) { |
||
| 64 | break; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | $tokens[] = [ $c, $j - $i ]; |
||
| 68 | $i = $j - 1; // because +1 from the for loop |
||
| 69 | } else { |
||
| 70 | $literal .= $c; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | // If the pattern ends with literal (could also be a malformed quote) |
||
| 75 | if ($literal) { |
||
| 76 | $tokens[] = $literal; |
||
| 77 | } |
||
| 78 | |||
| 79 | return $tokens; |
||
| 80 | } |
||
| 82 |