| Total Complexity | 48 |
| Total Lines | 217 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RegexGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RegexGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class RegexGenerator |
||
| 37 | { |
||
| 38 | /** @var string */ |
||
| 39 | private $prefix; |
||
| 40 | |||
| 41 | /** @var string[] */ |
||
| 42 | private $staticPrefixes = []; |
||
| 43 | |||
| 44 | /** @var string[] */ |
||
| 45 | private $prefixes = []; |
||
| 46 | |||
| 47 | /** @var array[]|self[] */ |
||
| 48 | private $items = []; |
||
| 49 | |||
| 50 | public function __construct(string $prefix = '/') |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getPrefix(): string |
||
| 56 | { |
||
| 57 | return $this->prefix; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return array[]|self[] |
||
| 62 | */ |
||
| 63 | public function getRoutes(): array |
||
| 64 | { |
||
| 65 | return $this->items; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * This method uses default routes compiler. |
||
| 70 | * |
||
| 71 | * @param array<int,FastRoute> $routes |
||
| 72 | * |
||
| 73 | * @return array<int,mixed> |
||
| 74 | */ |
||
| 75 | public static function beforeCaching(RouteCompilerInterface $compiler, array $routes): array |
||
| 76 | { |
||
| 77 | $tree = new static(); |
||
| 78 | $indexedRoutes = []; |
||
| 79 | |||
| 80 | for ($i = 0; $i < \count($routes); ++$i) { |
||
|
|
|||
| 81 | [$pathRegex, $hostsRegex, $variables] = $compiler->compile($route = $routes[$i]); |
||
| 82 | $pathRegex = \preg_replace('/\?(?|P<\w+>|<\w+>|\'\w+\')/', '', $pathRegex); |
||
| 83 | |||
| 84 | $tree->addRoute($pathRegex, [$pathRegex, $i, [$route, $hostsRegex, $variables]]); |
||
| 85 | } |
||
| 86 | |||
| 87 | $compiledRegex = '~^(?' . $tree->compile(0, $indexedRoutes) . ')$~u'; |
||
| 88 | \ksort($indexedRoutes); |
||
| 89 | |||
| 90 | return [$compiledRegex, $indexedRoutes, $compiler]; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Compiles a regexp tree of sub-patterns that matches nested same-prefix routes. |
||
| 95 | * |
||
| 96 | * The route item should contain: |
||
| 97 | * - pathRegex |
||
| 98 | * - an id used for (*:MARK) |
||
| 99 | * - an array of additional/optional values if maybe required. |
||
| 100 | */ |
||
| 101 | public function compile(int $prefixLen, array &$variables = []): string |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Adds a route to a group. |
||
| 122 | * |
||
| 123 | * @param array|self $route |
||
| 124 | */ |
||
| 125 | public function addRoute(string $prefix, $route): void |
||
| 183 | } |
||
| 184 | |||
| 185 | public static function handleError(int $type, string $msg): bool |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the full and static common prefixes between two route patterns. |
||
| 192 | * |
||
| 193 | * The static prefix stops at last at the first opening bracket. |
||
| 194 | */ |
||
| 195 | private function getCommonPrefix(string $prefix, string $anotherPrefix): array |
||
| 255 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: