Total Complexity | 46 |
Total Lines | 204 |
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 = '/') |
||
51 | { |
||
52 | $this->prefix = $prefix; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * This method uses default routes compiler. |
||
57 | * |
||
58 | * @param array<int,FastRoute> $routes |
||
59 | * |
||
60 | * @return array<int,mixed> |
||
61 | */ |
||
62 | public static function beforeCaching(RouteCompilerInterface $compiler, array $routes): array |
||
63 | { |
||
64 | $tree = new static(); |
||
65 | $indexedRoutes = []; |
||
66 | |||
67 | for ($i = 0; $i < \count($routes); ++$i) { |
||
|
|||
68 | [$pathRegex, $hostsRegex, $variables] = $compiler->compile($route = $routes[$i]); |
||
69 | $pathRegex = \preg_replace('/\?(?|P<\w+>|<\w+>|\'\w+\')/', '', $pathRegex); |
||
70 | |||
71 | $tree->addRoute($pathRegex, [$pathRegex, $i, [$route, $hostsRegex, $variables]]); |
||
72 | } |
||
73 | |||
74 | $compiledRegex = '~^(?' . $tree->compile(0, $indexedRoutes) . ')$~u'; |
||
75 | \ksort($indexedRoutes); |
||
76 | |||
77 | return [$compiledRegex, $indexedRoutes, $compiler]; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Compiles a regexp tree of sub-patterns that matches nested same-prefix routes. |
||
82 | * |
||
83 | * The route item should contain: |
||
84 | * - pathRegex |
||
85 | * - an id used for (*:MARK) |
||
86 | * - an array of additional/optional values if maybe required. |
||
87 | */ |
||
88 | public function compile(int $prefixLen, array &$variables = []): string |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Adds a route to a group. |
||
109 | * |
||
110 | * @param array|self $route |
||
111 | */ |
||
112 | public function addRoute(string $prefix, $route): void |
||
170 | } |
||
171 | |||
172 | public static function handleError(int $type, string $msg): bool |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Gets the full and static common prefixes between two route patterns. |
||
179 | * |
||
180 | * The static prefix stops at last at the first opening bracket. |
||
181 | */ |
||
182 | private function getCommonPrefix(string $prefix, string $anotherPrefix): array |
||
242 |
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: