Complex classes like Router 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Router |
||
7 | { |
||
8 | private $trailing_slash_check = true; |
||
9 | private $routes = []; |
||
10 | private $route_names = []; |
||
11 | private $regexp_map = [ |
||
12 | '/\{([A-Za-z]\w*)\}/' => '(?<$1>[^/]+)', |
||
13 | '/\{([A-Za-z]\w*):word\}/' => '(?<$1>\w+)', |
||
14 | '/\{([A-Za-z]\w*):number\}/' => '(?<$1>\d+)', |
||
15 | '/\{([A-Za-z]\w*):slug\}/' => '(?<$1>[A-Za-z0-9_-]+)', |
||
16 | '/\{([A-Za-z]\w*):([^}]+)\}/' => '(?<$1>$2)', |
||
17 | '/\//' => '\/' |
||
18 | ]; |
||
19 | private static $parsed_regexp = []; // cache |
||
20 | |||
21 | /** |
||
22 | * Creates a new Router. |
||
23 | * |
||
24 | * @param array $routes |
||
25 | * (Optional) Associative array of URIs to handlers. |
||
26 | * URIs take the form of [VERB1|VERB2]/[uri] or /[uri] (the latter |
||
27 | * assumes a verb of GET). Handlers take the form of |
||
28 | * ClassName::methodName or ClassName (in the latter case, a |
||
29 | * methodName of index is assumed). |
||
30 | */ |
||
31 | 20 | public function __construct(array $routes = []) |
|
53 | |||
54 | /** |
||
55 | * Registers a route. |
||
56 | * |
||
57 | * @param Route $route |
||
58 | * Route Value Object. |
||
59 | */ |
||
60 | 9 | public function add(Route $route) |
|
64 | |||
65 | /** |
||
66 | * Registers a route. |
||
67 | * |
||
68 | * @param string|array $methods |
||
69 | * Either a single HTTP verb, or an array of verbs. |
||
70 | * @param string $uri |
||
71 | * URI pattern to match for this route. |
||
72 | * @param string $handler |
||
73 | * ClassName::methodName to invoke for this route. If methodName |
||
74 | * is not present, a method of 'index' is assumed. |
||
75 | * @param string $name |
||
76 | * (Optional) An unique name for this route. |
||
77 | */ |
||
78 | 19 | public function addRoute($methods, $uri, $handler, $name = null) |
|
91 | |||
92 | /** |
||
93 | * Syntactic sugar: registers a HEAD route. |
||
94 | * |
||
95 | * @param string $uri |
||
96 | * @param string $handler |
||
97 | * @param string $name |
||
98 | * (Optional) An unique name for this route. |
||
99 | */ |
||
100 | 1 | public function addHead($uri, $handler, $name = null) |
|
104 | |||
105 | /** |
||
106 | * Syntactic sugar: registers a GET route. |
||
107 | * |
||
108 | * @param string $uri |
||
109 | * @param string $handler |
||
110 | * @param string $name |
||
111 | * (Optional) An unique name for this route. |
||
112 | */ |
||
113 | 1 | public function addGet($uri, $handler, $name = null) |
|
117 | |||
118 | /** |
||
119 | * Syntactic sugar: registers a DELETE route. |
||
120 | * |
||
121 | * @param string $uri |
||
122 | * @param string $handler |
||
123 | * @param string $name |
||
124 | * (Optional) An unique name for this route. |
||
125 | */ |
||
126 | 1 | public function addDelete($uri, $handler, $name = null) |
|
130 | |||
131 | /** |
||
132 | * Syntactic sugar: registers an OPTIONS route. |
||
133 | * |
||
134 | * @param string $uri |
||
135 | * @param string $handler |
||
136 | * @param string $name |
||
137 | * (Optional) An unique name for this route. |
||
138 | */ |
||
139 | 1 | public function addOptions($uri, $handler, $name = null) |
|
143 | |||
144 | /** |
||
145 | * Syntactic sugar: registers a PATCH route. |
||
146 | * |
||
147 | * @param string $uri |
||
148 | * @param string $handler |
||
149 | * @param string $name |
||
150 | * (Optional) An unique name for this route. |
||
151 | */ |
||
152 | 1 | public function addPatch($uri, $handler, $name = null) |
|
156 | |||
157 | /** |
||
158 | * Syntactic sugar: registers a POST route. |
||
159 | * |
||
160 | * @param string $uri |
||
161 | * @param string $handler |
||
162 | * @param string $name |
||
163 | * (Optional) An unique name for this route. |
||
164 | */ |
||
165 | 1 | public function addPost($uri, $handler, $name = null) |
|
169 | |||
170 | /** |
||
171 | * Syntactic sugar: registers a PUT route. |
||
172 | * |
||
173 | * @param string $uri |
||
174 | * @param string $handler |
||
175 | * @param string $name |
||
176 | * (Optional) An unique name for this route. |
||
177 | */ |
||
178 | 1 | public function addPut($uri, $handler, $name = null) |
|
182 | |||
183 | /** |
||
184 | * Disables distinguishing an extra slash on the end of incoming URIs as a |
||
185 | * different URL. |
||
186 | */ |
||
187 | 1 | public function disableTrailingSlashCheck() |
|
191 | |||
192 | /** |
||
193 | * Registers a matching pattern for URI parameters. |
||
194 | * |
||
195 | * @param string $name |
||
196 | * Name which will appear within curly-braces in URI patterns. |
||
197 | * @param string $regexp |
||
198 | * Regexp substitution pattern. |
||
199 | */ |
||
200 | 1 | public function addPattern($name, $regexp) |
|
204 | |||
205 | /** |
||
206 | * Finds the uri associated with a given name. |
||
207 | * |
||
208 | * @param string $name |
||
209 | * Name of the route to find. |
||
210 | * @param array $parameters |
||
211 | * (Optional) Parameters to complete the URI. |
||
212 | * |
||
213 | * @return string|null |
||
214 | */ |
||
215 | 2 | public function findURI($name, $parameters = []) |
|
226 | |||
227 | /** |
||
228 | * Parses an incoming method and URI, and returns a matching route. |
||
229 | * |
||
230 | * @param string $method |
||
231 | * HTTP verb to find a match on. |
||
232 | * @param string $uri |
||
233 | * URI pattern to find a match on. |
||
234 | * @param string $prefix |
||
235 | * (Optional) Prefix to prepend to URI path. |
||
236 | * |
||
237 | * @return ParsedRoute |
||
238 | * |
||
239 | * @throws MethodNotAllowedException |
||
240 | * Thrown if a handler is registered for this route, but it is not |
||
241 | * configured to handle this verb. |
||
242 | * @throws RouteNotFoundException |
||
243 | * Thrown if there is no handler registered for this route. |
||
244 | */ |
||
245 | 18 | public function parse($method, $uri, $prefix = "") |
|
270 | |||
271 | /** |
||
272 | * Finds the first matching route for a given method and URI. |
||
273 | * |
||
274 | * @param string $method |
||
275 | * HTTP verb to find a match on. |
||
276 | * @param string $uri |
||
277 | * URI pattern to find a match on. |
||
278 | * @param string $prefix |
||
279 | * (Optional) Prefix to prepend to URI path. |
||
280 | * |
||
281 | * @return ParsedRoute |
||
282 | * |
||
283 | * @throws RouteNotFoundException |
||
284 | */ |
||
285 | 18 | private function findMatches($method, $uri, $prefix = "") |
|
300 | |||
301 | /** |
||
302 | * Applies standard regexp patterns to an incoming URI route. |
||
303 | * |
||
304 | * @param string $route |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 18 | private function prepareRouteRegexp($route) |
|
315 | } |
||
316 |
If you suppress an error, we recommend checking for the error condition explicitly: