Complex classes like HttpRouter 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 HttpRouter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class HttpRouter implements HttpRouterInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var RouteCollection |
||
25 | */ |
||
26 | protected $routes; |
||
27 | |||
28 | /** |
||
29 | * @var RequestContext |
||
30 | */ |
||
31 | protected $context; |
||
32 | |||
33 | /** |
||
34 | * @var UrlMatcherInterface |
||
35 | */ |
||
36 | protected $matcher; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $host; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $checkOrigin; |
||
47 | |||
48 | /** |
||
49 | * @var string[] |
||
50 | */ |
||
51 | protected $allowedOrigins; |
||
52 | |||
53 | /** |
||
54 | * @param NetworkComponentAwareInterface $aware |
||
55 | * @param mixed[] $params |
||
56 | */ |
||
57 | 49 | public function __construct(NetworkComponentAwareInterface $aware = null, $params = []) |
|
80 | |||
81 | /** |
||
82 | * |
||
83 | */ |
||
84 | public function __destruct() |
||
93 | |||
94 | /** |
||
95 | * @override |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | 5 | public function allowOrigin($address) |
|
104 | |||
105 | /** |
||
106 | * @override |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | 2 | public function disallowOrigin($address) |
|
118 | |||
119 | /** |
||
120 | * @override |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | 3 | public function isOriginAllowed($address) |
|
127 | |||
128 | /** |
||
129 | * @override |
||
130 | * @inheritDoc |
||
131 | */ |
||
132 | 1 | public function getAllowedOrigins() |
|
136 | |||
137 | /** |
||
138 | * @override |
||
139 | * @inheritDoc |
||
140 | */ |
||
141 | 6 | public function existsRoute($path) |
|
145 | |||
146 | /** |
||
147 | * @override |
||
148 | * @inheritDoc |
||
149 | */ |
||
150 | 6 | public function addRoute($path, NetworkComponentInterface $component) |
|
165 | |||
166 | /** |
||
167 | * @override |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | 2 | public function removeRoute($path) |
|
176 | |||
177 | /** |
||
178 | * @override |
||
179 | * @inheritDoc |
||
180 | */ |
||
181 | 3 | public function handleConnect(NetworkConnectionInterface $conn) |
|
183 | |||
184 | /** |
||
185 | * @override |
||
186 | * @inheritDoc |
||
187 | */ |
||
188 | 4 | public function handleDisconnect(NetworkConnectionInterface $conn) |
|
195 | |||
196 | /** |
||
197 | * @override |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | 10 | public function handleMessage(NetworkConnectionInterface $conn, NetworkMessageInterface $message) |
|
263 | |||
264 | /** |
||
265 | * @override |
||
266 | * @inheritDoc |
||
267 | */ |
||
268 | 3 | public function handleError(NetworkConnectionInterface $conn, $ex) |
|
284 | |||
285 | /** |
||
286 | * Close a connection with an HTTP response. |
||
287 | * |
||
288 | * @param NetworkConnectionInterface $conn |
||
289 | * @param int $code |
||
290 | * @return null |
||
291 | */ |
||
292 | 1 | protected function close(NetworkConnectionInterface $conn, $code = 400) |
|
299 | } |
||
300 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: