| Total Complexity | 48 |
| Total Lines | 216 |
| 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 | private string $prefix; |
||
| 39 | |||
| 40 | /** @var string[] */ |
||
| 41 | private array $staticPrefixes = []; |
||
| 42 | |||
| 43 | /** @var string[] */ |
||
| 44 | private array $prefixes = []; |
||
| 45 | |||
| 46 | /** @var array[]|self[] */ |
||
| 47 | private array $items = []; |
||
| 48 | |||
| 49 | public function __construct(string $prefix = '/') |
||
| 50 | { |
||
| 51 | $this->prefix = $prefix; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function getPrefix(): string |
||
| 55 | { |
||
| 56 | return $this->prefix; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @return array[]|self[] |
||
| 61 | */ |
||
| 62 | public function getRoutes(): array |
||
| 63 | { |
||
| 64 | return $this->items; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * This method uses default routes compiler. |
||
| 69 | * |
||
| 70 | * @param array<int,FastRoute> $routes |
||
| 71 | * |
||
| 72 | * @return array<int,mixed> |
||
| 73 | */ |
||
| 74 | public static function beforeCaching(RouteCompilerInterface $compiler, array $routes): array |
||
| 75 | { |
||
| 76 | $tree = new static(); |
||
| 77 | $indexedRoutes = []; |
||
| 78 | |||
| 79 | for ($i = 0; $i < \count($routes); ++$i) { |
||
|
|
|||
| 80 | [$pathRegex, $hostsRegex, $variables] = $compiler->compile($route = $routes[$i]); |
||
| 81 | $pathRegex = \preg_replace('/\?(?|P<\w+>|<\w+>|\'\w+\')/', '', $pathRegex); |
||
| 82 | |||
| 83 | $tree->addRoute($pathRegex, [$pathRegex, $i, [$route, $hostsRegex, $variables]]); |
||
| 84 | } |
||
| 85 | |||
| 86 | $compiledRegex = '~^(?' . $tree->compile(0, $indexedRoutes) . ')$~u'; |
||
| 87 | \ksort($indexedRoutes); |
||
| 88 | |||
| 89 | return [$compiledRegex, $indexedRoutes, $compiler]; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Compiles a regexp tree of sub-patterns that matches nested same-prefix routes. |
||
| 94 | * |
||
| 95 | * The route item should contain: |
||
| 96 | * - pathRegex |
||
| 97 | * - an id used for (*:MARK) |
||
| 98 | * - an array of additional/optional values if maybe required. |
||
| 99 | */ |
||
| 100 | public function compile(int $prefixLen, array &$variables = []): string |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Adds a route to a group. |
||
| 121 | * |
||
| 122 | * @param array|self $route |
||
| 123 | */ |
||
| 124 | public function addRoute(string $prefix, $route): void |
||
| 182 | } |
||
| 183 | |||
| 184 | public static function handleError(int $type, string $msg): bool |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Gets the full and static common prefixes between two route patterns. |
||
| 191 | * |
||
| 192 | * The static prefix stops at last at the first opening bracket. |
||
| 193 | */ |
||
| 194 | private function getCommonPrefix(string $prefix, string $anotherPrefix): array |
||
| 254 |
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: