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