Complex classes like Uri 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 Uri, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Uri implements UriInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $scheme = ''; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $userInfo = ''; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $host = ''; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $port; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $path = ''; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $query = ''; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $fragment = ''; |
||
44 | |||
45 | /** |
||
46 | * @var int[] Default Ports for known Schemes. |
||
47 | */ |
||
48 | private $defaultSchemePorts = [ |
||
49 | 'http' => 80, |
||
50 | 'https' => 443, |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @param string|array $uri |
||
55 | * @throws InvalidArgumentException on non-string $uri argument |
||
56 | */ |
||
57 | 108 | public function __construct($uri = '') |
|
58 | { |
||
59 | 108 | if (is_array($uri)) { |
|
60 | 45 | $this->parseUriParts($uri); |
|
61 | } |
||
62 | |||
63 | 108 | if (is_string($uri) && !empty($uri)) { |
|
64 | 81 | $parts = parse_url($uri); |
|
65 | 81 | if (false === $parts) { |
|
66 | 6 | throw new \InvalidArgumentException( |
|
67 | 6 | 'The source URI string appears to be malformed' |
|
68 | ); |
||
69 | } |
||
70 | |||
71 | 75 | $this->parseUriParts($parts); |
|
72 | } |
||
73 | 102 | } |
|
74 | |||
75 | 51 | public function __toString() |
|
79 | |||
80 | 75 | private function getUriString() |
|
81 | { |
||
82 | 75 | $uri = ''; |
|
83 | |||
84 | 75 | if ($this->hasScheme()) { |
|
85 | 51 | $uri .= sprintf('%s://', $this->getScheme()); |
|
86 | } |
||
87 | |||
88 | 75 | $uri .= $this->getAuthority() . $this->getPath(); |
|
89 | |||
90 | 75 | if ($this->hasQuery()) { |
|
91 | 24 | $uri .= sprintf('?%s', $this->getQuery()); |
|
92 | } |
||
93 | |||
94 | 75 | if ($this->hasFragment()) { |
|
95 | 24 | $uri .= sprintf('#%s', $this->getFragment()); |
|
96 | } |
||
97 | |||
98 | 75 | return $uri; |
|
99 | } |
||
100 | |||
101 | 99 | private function parseUriParts($parts) |
|
102 | { |
||
103 | 99 | $this->scheme = isset($parts['scheme']) ? $parts['scheme'] : ''; |
|
104 | 99 | $this->userInfo = isset($parts['user']) ? $parts['user'] : ''; |
|
105 | 99 | $this->host = isset($parts['host']) ? $parts['host'] : ''; |
|
106 | 99 | $this->port = isset($parts['port']) ? $parts['port'] : null; |
|
107 | 99 | $this->path = isset($parts['path']) ? $parts['path'] : ''; |
|
108 | 99 | $this->query = isset($parts['query']) ? $parts['query'] : ''; |
|
109 | 99 | $this->fragment = isset($parts['fragment']) ? $parts['fragment'] : ''; |
|
110 | |||
111 | 99 | if (isset($parts['pass'])) { |
|
112 | 30 | $this->userInfo .= ':' . $parts['pass']; |
|
113 | } |
||
114 | 99 | } |
|
115 | |||
116 | 87 | public function hasScheme() |
|
117 | { |
||
118 | 87 | if (!empty($this->scheme)) { |
|
119 | 66 | return true; |
|
120 | } |
||
121 | |||
122 | 24 | return false; |
|
123 | } |
||
124 | |||
125 | 66 | public function getScheme() |
|
129 | |||
130 | 81 | public function getAuthority() |
|
131 | { |
||
132 | 81 | if (!$this->hasHost()) { |
|
133 | 24 | return ''; |
|
134 | } |
||
135 | |||
136 | 57 | $authority = $this->getHost(); |
|
137 | |||
138 | 57 | if ($this->hasUserInfo()) { |
|
139 | 24 | $authority = $this->getUserInfo() . '@' . $authority; |
|
140 | } |
||
141 | |||
142 | 57 | if (!$this->isDefaultPort()) { |
|
143 | 24 | $authority .= ':' . $this->getPort(); |
|
144 | } |
||
145 | |||
146 | 57 | return $authority; |
|
147 | } |
||
148 | |||
149 | 27 | public function getUserInfo() |
|
153 | |||
154 | 63 | public function hasUserInfo() |
|
162 | |||
163 | 84 | public function hasHost() |
|
164 | { |
||
165 | 84 | if (!empty($this->host)) { |
|
166 | 63 | return true; |
|
167 | } |
||
168 | |||
169 | 24 | return false; |
|
170 | } |
||
171 | |||
172 | 75 | public function getHost() |
|
176 | |||
177 | 66 | public function hasPort() |
|
178 | { |
||
179 | 66 | if (!empty($this->port)) { |
|
180 | 39 | return true; |
|
181 | } |
||
182 | |||
183 | 30 | return false; |
|
184 | } |
||
185 | |||
186 | 30 | public function getPort() |
|
187 | { |
||
188 | 30 | if ($this->isDefaultPort()) { |
|
189 | 3 | return null; |
|
190 | } |
||
191 | |||
192 | 27 | return $this->port; |
|
193 | } |
||
194 | |||
195 | 63 | private function isDefaultPort() |
|
196 | { |
||
197 | 63 | if (!$this->hasPort()) { |
|
198 | 27 | return true; |
|
199 | } |
||
200 | |||
201 | 36 | if (!$this->hasScheme()) { |
|
202 | 3 | return false; |
|
203 | } |
||
204 | |||
205 | 33 | if (isset($this->defaultSchemePorts[$this->getScheme()]) |
|
206 | 33 | && $this->defaultSchemePorts[$this->getScheme()] == $this->port |
|
207 | ) { |
||
208 | 9 | return true; |
|
209 | } |
||
210 | |||
211 | 24 | return false; |
|
212 | } |
||
213 | |||
214 | 9 | public function hasPath() |
|
222 | |||
223 | 78 | public function getPath() |
|
228 | |||
229 | 78 | public function hasQuery() |
|
230 | { |
||
231 | 78 | if (!empty($this->query)) { |
|
232 | 27 | return true; |
|
233 | } |
||
234 | |||
235 | 51 | return false; |
|
236 | } |
||
237 | |||
238 | 30 | public function getQuery() |
|
239 | { |
||
240 | 30 | return $this->query; |
|
241 | } |
||
242 | |||
243 | 78 | public function hasFragment() |
|
251 | |||
252 | 27 | public function getFragment() |
|
256 | |||
257 | 9 | public function withScheme($scheme) |
|
263 | |||
264 | 6 | public function withUserInfo($user, $password = null) |
|
273 | |||
274 | 6 | public function withHost($host) |
|
275 | { |
||
276 | 6 | return $this->withModifiedParts( |
|
277 | 6 | ['host' => $host] |
|
278 | ); |
||
279 | } |
||
280 | |||
281 | 6 | public function withPort($port) |
|
287 | |||
288 | 6 | public function withPath($path) |
|
289 | { |
||
290 | 6 | return $this->withModifiedParts( |
|
291 | 6 | ['path' => $path] |
|
292 | ); |
||
293 | } |
||
294 | |||
295 | 6 | public function withQuery($query) |
|
296 | { |
||
297 | 6 | return $this->withModifiedParts( |
|
298 | 6 | ['query' => $query] |
|
299 | ); |
||
300 | } |
||
301 | |||
302 | 6 | public function withFragment($fragment) |
|
308 | |||
309 | /** |
||
310 | * @param array $modifiedParts |
||
311 | * @return Uri |
||
312 | */ |
||
313 | 45 | private function withModifiedParts($modifiedParts) |
|
321 | } |
||
322 |