| Conditions | 26 |
| Paths | 42 |
| Total Lines | 57 |
| Code Lines | 34 |
| 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 |
||
| 141 | private function getCommonPrefix(string $prefix, string $anotherPrefix): array |
||
| 142 | { |
||
| 143 | $baseLength = \strlen($this->prefix); |
||
| 144 | $end = \min(\strlen($prefix), \strlen($anotherPrefix)); |
||
| 145 | $staticLength = null; |
||
| 146 | \set_error_handler([__CLASS__, 'handleError']); |
||
| 147 | |||
| 148 | for ($i = $baseLength; $i < $end && $prefix[$i] === $anotherPrefix[$i]; ++$i) { |
||
| 149 | if ('(' === $prefix[$i]) { |
||
| 150 | $staticLength = $staticLength ?? $i; |
||
| 151 | |||
| 152 | for ($j = 1 + $i, $n = 1; $j < $end && 0 < $n; ++$j) { |
||
| 153 | if ($prefix[$j] !== $anotherPrefix[$j]) { |
||
| 154 | break 2; |
||
| 155 | } |
||
| 156 | |||
| 157 | if ('(' === $prefix[$j]) { |
||
| 158 | ++$n; |
||
| 159 | } elseif (')' === $prefix[$j]) { |
||
| 160 | --$n; |
||
| 161 | } elseif ('\\' === $prefix[$j] && (++$j === $end || $prefix[$j] !== $anotherPrefix[$j])) { |
||
| 162 | --$j; |
||
| 163 | |||
| 164 | break; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | if (0 < $n) { |
||
| 169 | break; |
||
| 170 | } |
||
| 171 | |||
| 172 | if (('?' === ($prefix[$j] ?? '') || '?' === ($anotherPrefix[$j] ?? '')) && ($prefix[$j] ?? '') !== ($anotherPrefix[$j] ?? '')) { |
||
| 173 | break; |
||
| 174 | } |
||
| 175 | $subPattern = \substr($prefix, $i, $j - $i); |
||
| 176 | |||
| 177 | if ($prefix !== $anotherPrefix && !\preg_match('/^\(\[[^\]]++\]\+\+\)$/', $subPattern) && !\preg_match('{(?<!' . $subPattern . ')}', '')) { |
||
| 178 | // sub-patterns of variable length are not considered as common prefixes because their greediness would break in-order matching |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | $i = $j - 1; |
||
| 182 | } elseif ('\\' === $prefix[$i] && (++$i === $end || $prefix[$i] !== $anotherPrefix[$i])) { |
||
| 183 | --$i; |
||
| 184 | |||
| 185 | break; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | \restore_error_handler(); |
||
| 189 | |||
| 190 | if ($i < $end && 0b10 === (\ord($prefix[$i]) >> 6) && \preg_match('//u', $prefix . ' ' . $anotherPrefix)) { |
||
| 191 | do { |
||
| 192 | // Prevent cutting in the middle of an UTF-8 characters |
||
| 193 | --$i; |
||
| 194 | } while (0b10 === (\ord($prefix[$i]) >> 6)); |
||
| 195 | } |
||
| 196 | |||
| 197 | return [\substr($prefix, 0, $i), \substr($prefix, 0, $staticLength ?? $i)]; |
||
| 198 | } |
||
| 200 |