Complex classes like QueryString 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 QueryString, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | final class QueryString |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $params = []; |
||
14 | |||
15 | /** |
||
16 | * @var QueryStringRendererInterface |
||
17 | */ |
||
18 | private $renderer; |
||
19 | |||
20 | /** |
||
21 | * @var QueryStringRendererInterface |
||
22 | */ |
||
23 | private static $defaultRenderer; |
||
24 | |||
25 | /** |
||
26 | * QueryString constructor. |
||
27 | * @param array|null $params |
||
28 | * @param QueryStringRendererInterface|null $renderer |
||
29 | * @throws \InvalidArgumentException |
||
30 | */ |
||
31 | protected function __construct(?array $params = [], QueryStringRendererInterface $renderer = null) |
||
39 | |||
40 | /** |
||
41 | * @param array $params |
||
42 | * @param QueryStringRendererInterface|null $renderer |
||
43 | * @return QueryString |
||
44 | */ |
||
45 | private static function createFromParams(array $params, QueryStringRendererInterface $renderer = null): self |
||
49 | |||
50 | /** |
||
51 | * @param \Psr\Http\Message\UriInterface $uri |
||
52 | * @param QueryStringRendererInterface|null $renderer |
||
53 | * @return QueryString |
||
54 | * @throws \TypeError |
||
55 | */ |
||
56 | private static function createFromUri($uri, QueryStringRendererInterface $renderer = null): self |
||
72 | |||
73 | /** |
||
74 | * @param string $qs |
||
75 | * @param QueryStringRendererInterface|null $renderer |
||
76 | * @return QueryString |
||
77 | */ |
||
78 | private static function createFromString(string $qs, QueryStringRendererInterface $renderer = null): self |
||
84 | |||
85 | /** |
||
86 | * @param QueryStringRendererInterface|null $renderer |
||
87 | * @return QueryString |
||
88 | * @throws \RuntimeException |
||
89 | */ |
||
90 | public static function createFromCurrentLocation(QueryStringRendererInterface $renderer = null): self |
||
97 | |||
98 | /** |
||
99 | * @return QueryString |
||
100 | * @throws \RuntimeException |
||
101 | */ |
||
102 | public function withCurrentLocation(): self |
||
106 | |||
107 | /** |
||
108 | * @param $input |
||
109 | * @return QueryString |
||
110 | * @throws \InvalidArgumentException |
||
111 | */ |
||
112 | public static function factory($input = null, QueryStringRendererInterface $renderer = null): self |
||
125 | |||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getParams(): ?array |
||
133 | |||
134 | /** |
||
135 | * @param string $key |
||
136 | * @param array ...$deepKeys |
||
137 | * @return mixed|null |
||
138 | */ |
||
139 | public function getParam(string $key, ...$deepKeys) |
||
150 | |||
151 | /** |
||
152 | * @param string $key |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function hasParam(string $key, ...$deepKeys): bool |
||
159 | |||
160 | /** |
||
161 | * @param string $key |
||
162 | * @param $value |
||
163 | * @return QueryString |
||
164 | */ |
||
165 | public function withParam(string $key, $value): self |
||
171 | |||
172 | /** |
||
173 | * @param array $params |
||
174 | * @return QueryString |
||
175 | */ |
||
176 | public function withParams(array $params): self |
||
185 | |||
186 | /** |
||
187 | * @param string $key |
||
188 | * @param array ...$deepKeys |
||
189 | * @return QueryString |
||
190 | */ |
||
191 | public function withoutParam(string $key, ...$deepKeys): self |
||
210 | |||
211 | /** |
||
212 | * @return QueryStringRendererInterface |
||
213 | */ |
||
214 | public function getRenderer(): QueryStringRendererInterface |
||
218 | |||
219 | /** |
||
220 | * @param QueryStringRendererInterface $renderer |
||
221 | * @return QueryString |
||
222 | */ |
||
223 | public function withRenderer(QueryStringRendererInterface $renderer): self |
||
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | public function __toString(): string |
||
237 | |||
238 | /** |
||
239 | * @param array $array |
||
240 | * @return bool |
||
241 | */ |
||
242 | private function isAnIndexedArray(array $array): bool |
||
247 | |||
248 | /** |
||
249 | * @param array $params |
||
250 | * @param array ...$keys |
||
251 | * @return array |
||
252 | */ |
||
253 | private function removeFromPath(array $params, ...$keys): array |
||
277 | |||
278 | /** |
||
279 | * Returns the default renderer. |
||
280 | * |
||
281 | * @return QueryStringRendererInterface |
||
282 | */ |
||
283 | public static function getDefaultRenderer(): QueryStringRendererInterface |
||
290 | |||
291 | /** |
||
292 | * Changes default renderer. |
||
293 | * |
||
294 | * @param QueryStringRendererInterface $defaultRenderer |
||
295 | */ |
||
296 | public static function setDefaultRenderer(QueryStringRendererInterface $defaultRenderer): void |
||
300 | |||
301 | /** |
||
302 | * Restores the default renderer. |
||
303 | */ |
||
304 | public static function restoreDefaultRenderer(): void |
||
308 | } |
||
309 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.