| Conditions | 15 |
| Paths | 27 |
| Total Lines | 46 |
| Code Lines | 28 |
| 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 |
||
| 120 | protected function buildCache(RouteCollection $collection, bool $doCache): string |
||
| 121 | { |
||
| 122 | $dynamicRoutes = []; |
||
| 123 | $this->optimized = [[], [[], []]]; |
||
| 124 | |||
| 125 | foreach ($collection->getRoutes() as $i => $route) { |
||
| 126 | $trimmed = \preg_replace('/\W$/', '', $path = $route['path']); |
||
| 127 | |||
| 128 | if (\in_array($prefix = $route['prefix'] ?? '/', [$trimmed, $path], true)) { |
||
| 129 | $this->optimized[0][$trimmed ?: '/'][] = $i; |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | [$path, $var] = $this->getCompiler()->compile($path, $route['placeholders'] ?? []); |
||
| 133 | $path = \str_replace('\/', '/', \substr($path, 1 + \strpos($path, '^'), -(\strlen($path) - \strrpos($path, '$')))); |
||
| 134 | |||
| 135 | if (($l = \array_key_last($dynamicRoutes)) && !\in_array($l, ['/', $prefix], true)) { |
||
| 136 | for ($o = 0, $new = ''; $o < \strlen($prefix); ++$o) { |
||
| 137 | if ($prefix[$o] !== ($l[$o] ?? null)) { |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | $new .= $l[$o]; |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($new && '/' !== $new) { |
||
| 144 | if ($l !== $new) { |
||
| 145 | $dynamicRoutes[$new] = $dynamicRoutes[$l]; |
||
| 146 | unset($dynamicRoutes[$l]); |
||
| 147 | } |
||
| 148 | $prefix = $new; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | $dynamicRoutes[$prefix][] = \preg_replace('#\?(?|P<\w+>|<\w+>|\'\w+\')#', '', $path)."(*:{$i})"; |
||
| 152 | $this->optimized[1][1][$i] = $var; |
||
| 153 | } |
||
| 154 | \ksort($this->optimized[0], \SORT_NATURAL); |
||
| 155 | \uksort($dynamicRoutes, fn (string $a, string $b): int => \in_array('/', [$a, $b], true) ? \strcmp($b, $a) : \strcmp($a, $b)); |
||
| 156 | |||
| 157 | foreach ($dynamicRoutes as $offset => $paths) { |
||
| 158 | $numParts = \max(1, \round(($c = \count($paths)) / 30)); |
||
| 159 | |||
| 160 | foreach (\array_chunk($paths, (int) \ceil($c / $numParts)) as $chunk) { |
||
| 161 | $this->optimized[1][0]['/'.\ltrim($offset, '/')][] = '~^(?|'.\implode('|', $chunk).')$~'; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | return self::export([...$this->optimized, $doCache ? $collection : null]); |
||
| 166 | } |
||
| 168 |