Total Complexity | 41 |
Total Lines | 311 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 0 | Features | 1 |
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.
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 |
||
14 | final class Uri |
||
15 | { |
||
16 | /** |
||
17 | * @param ParameterBag $query |
||
18 | * @param string $scheme |
||
19 | * @param string $host |
||
20 | * @param int|null $port |
||
21 | * @param string|null $path |
||
22 | * @param string|null $fragment |
||
23 | * @return void |
||
24 | */ |
||
25 | private function __construct( |
||
26 | private ParameterBag $query, |
||
27 | private string $scheme = '', |
||
28 | private string $host = '', |
||
29 | private null|int $port = null, |
||
30 | private null|string $path = null, |
||
31 | private null|string $fragment = null, |
||
32 | ) { |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return Uri |
||
37 | */ |
||
38 | public static function build(): Uri |
||
39 | { |
||
40 | return new Uri( |
||
41 | query: new ParameterBag(), |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param string $uri |
||
47 | * @return Uri |
||
48 | * @throws UrlException |
||
49 | * @throws InvalidArgumentException |
||
50 | */ |
||
51 | public static function fromString(string $uri): Uri |
||
52 | { |
||
53 | $original = $uri; |
||
54 | |||
55 | $uri = parse_url($uri); |
||
56 | |||
57 | if ( |
||
58 | ! is_array($uri) |
||
|
|||
59 | || ! isset($uri['scheme'], $uri['host']) |
||
60 | ) { |
||
61 | throw new InvalidArgumentException( |
||
62 | message: "URI failed to parse using parse_url, please ensure is valid URL. Passed in [$uri]", |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | $url = Uri::build() |
||
67 | ->addScheme( |
||
68 | scheme: $uri['scheme'], |
||
69 | )->addHost( |
||
70 | host: $uri['host'], |
||
71 | ); |
||
72 | |||
73 | if (isset($uri['port'])) { |
||
74 | $url->addPort( |
||
75 | port: $uri['port'], |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | if (isset($uri['path'])) { |
||
80 | $url->addPath( |
||
81 | path: $uri['path'], |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | if (isset($uri['query'])) { |
||
86 | $url->addQuery( |
||
87 | query: $uri['query'], |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | $fragment = parse_url($original, PHP_URL_FRAGMENT); |
||
92 | |||
93 | if (! is_null($fragment)) { |
||
94 | $url->addFragment( |
||
95 | fragment: $fragment, |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | return $url; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param string $scheme |
||
104 | * @return Uri |
||
105 | */ |
||
106 | public function addScheme(string $scheme): Uri |
||
107 | { |
||
108 | $this->scheme = $scheme; |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | public function scheme(): string |
||
117 | { |
||
118 | return $this->scheme; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $host |
||
123 | * @return Uri |
||
124 | */ |
||
125 | public function addHost(string $host): Uri |
||
126 | { |
||
127 | $this->host = $host; |
||
128 | |||
129 | return $this; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function host(): string |
||
136 | { |
||
137 | return $this->host; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param null|int $port |
||
142 | * @throws InvalidArgumentException |
||
143 | * @return Uri |
||
144 | */ |
||
145 | public function addPort(null|int $port = null): Uri |
||
146 | { |
||
147 | if (is_null($port)) { |
||
148 | throw new InvalidArgumentException( |
||
149 | message: 'Cannot set port to a null value.', |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | $this->port = $port; |
||
154 | |||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return null|int |
||
160 | */ |
||
161 | public function port(): null|int |
||
162 | { |
||
163 | return $this->port; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param null|string $path |
||
168 | * @return Uri |
||
169 | */ |
||
170 | public function addPath(null|string $path = null): Uri |
||
171 | { |
||
172 | if (is_null($path)) { |
||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | $this->path = str_starts_with( |
||
177 | haystack: $path, |
||
178 | needle: '/', |
||
179 | ) ? $path : "/$path"; |
||
180 | |||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @return null|string |
||
186 | */ |
||
187 | public function path(): null|string |
||
188 | { |
||
189 | return $this->path; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param null|string $query |
||
194 | * @throws InvalidArgumentException |
||
195 | * @return Uri |
||
196 | */ |
||
197 | public function addQuery(null|string $query = null): Uri |
||
198 | { |
||
199 | if (is_null($query)) { |
||
200 | throw new InvalidArgumentException( |
||
201 | message: 'Cannot set query to a null value.', |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | $this->query = ParameterBag::fromString( |
||
206 | attributes: $query, |
||
207 | ); |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @return ParameterBag |
||
214 | */ |
||
215 | public function query(): ParameterBag |
||
216 | { |
||
217 | return $this->query; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string $key |
||
222 | * @param int|string|bool|null|array $value |
||
223 | * @param bool $covertBoolToString |
||
224 | * @throws InvalidArgumentException |
||
225 | * @return Uri |
||
226 | */ |
||
227 | public function addQueryParam(string $key, int|string|bool|null|array $value, bool $covertBoolToString = false): Uri |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return array<string,mixed> |
||
249 | */ |
||
250 | public function queryParams(): array |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param string $fragment |
||
257 | * @return Uri |
||
258 | */ |
||
259 | public function addFragment(string $fragment): Uri |
||
260 | { |
||
261 | if ($fragment === '') { |
||
262 | return $this; |
||
263 | } |
||
264 | |||
265 | $this->fragment = str_starts_with( |
||
266 | haystack: $fragment, |
||
267 | needle:'#', |
||
268 | ) ? $fragment : "#{$fragment}"; |
||
269 | |||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return null|string |
||
275 | */ |
||
276 | public function fragment(): null|string |
||
277 | { |
||
278 | return $this->fragment; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @return string |
||
283 | */ |
||
284 | public function __toString(): string |
||
285 | { |
||
286 | return $this->toString(); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param bool $encode |
||
291 | * @return string |
||
292 | */ |
||
293 | public function toString(bool $encode = false): string |
||
325 | } |
||
326 | } |
||
327 |