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 |
||
20 | class Router |
||
21 | { |
||
22 | /** |
||
23 | * Requested route |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $path; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * HTTP verb |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $verb; |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Installed routes |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $routes = []; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Logger |
||
48 | * |
||
49 | * @var Logger |
||
50 | */ |
||
51 | protected $logger; |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Init router |
||
56 | * |
||
57 | * @param array $server |
||
58 | * @param Logger $logger |
||
59 | * @return void |
||
60 | */ |
||
61 | public function __construct(array $server, Logger $logger) |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Prepend route |
||
77 | * |
||
78 | * @param Route $route |
||
79 | * @return Router |
||
80 | */ |
||
81 | public function prependRoute(Route $route): Router |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Add route |
||
91 | * |
||
92 | * @param Route $route |
||
93 | * @return Router |
||
94 | */ |
||
95 | public function addRoute(Route $route): Router |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Clear routing table |
||
105 | * |
||
106 | * @return Router |
||
107 | */ |
||
108 | public function clearRoutingTable(): Router |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Get active routes |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getRoutes(): array |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Set HTTP verb |
||
128 | * |
||
129 | * @param string $verb |
||
130 | * @return Router |
||
131 | */ |
||
132 | public function setVerb(string $verb): Router |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Get http verb |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getVerb(): string |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Set routing path |
||
152 | * |
||
153 | * @param string $path |
||
154 | * @return Router |
||
155 | */ |
||
156 | public function setPath(string $path): Router |
||
162 | |||
163 | |||
164 | /** |
||
165 | * Get path |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getPath(): string |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Build method name |
||
177 | * |
||
178 | * @param string $name |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function _buildMethodName(string $name): string |
||
191 | |||
192 | |||
193 | /** |
||
194 | * Execute router |
||
195 | * |
||
196 | * @param array $constructor |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function run(array $constructor=[]): bool |
||
252 | |||
253 | |||
254 | /** |
||
255 | * Sends a exception response to the client |
||
256 | * |
||
257 | * @param \Exception $exception |
||
258 | * @return void |
||
259 | */ |
||
260 | public function sendException(\Exception $exception): void |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Check if method got params and combine these with |
||
302 | * $_REQUEST |
||
303 | * |
||
304 | * @param string $class |
||
305 | * @param string $method |
||
306 | * @param array $parsed_params |
||
307 | * @return array |
||
308 | */ |
||
309 | protected function getParams(string $class, string $method, array $parsed_params): array |
||
379 | } |
||
380 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: