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 |
||
| 11 | final class QueryString |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | private $params = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var QueryStringRendererInterface |
||
| 20 | */ |
||
| 21 | private $renderer; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var QueryStringRendererInterface |
||
| 25 | */ |
||
| 26 | private static $defaultRenderer; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var QueryStringParserInterface |
||
| 30 | */ |
||
| 31 | private static $defaultParser; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * QueryString constructor. |
||
| 35 | * @param array|null $params |
||
| 36 | * @throws \InvalidArgumentException |
||
| 37 | */ |
||
| 38 | protected function __construct(?array $params = []) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param array $params |
||
| 49 | * @return QueryString |
||
| 50 | */ |
||
| 51 | private static function createFromParams(array $params): self |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param \Psr\Http\Message\UriInterface $uri |
||
| 58 | * @param QueryStringParserInterface $queryStringParser |
||
| 59 | * @return QueryString |
||
| 60 | */ |
||
| 61 | private static function createFromUri($uri, QueryStringParserInterface $queryStringParser): self |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $string |
||
| 68 | * @param QueryStringParserInterface $queryStringParser |
||
| 69 | * @return QueryString |
||
| 70 | */ |
||
| 71 | private static function createFromString(string $string, QueryStringParserInterface $queryStringParser): self |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param QueryStringParserInterface|null $queryStringParser |
||
| 78 | * @return QueryString |
||
| 79 | * @throws \RuntimeException |
||
| 80 | */ |
||
| 81 | public static function createFromCurrentLocation(QueryStringParserInterface $queryStringParser = null): self |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return QueryString |
||
| 91 | * @throws \RuntimeException |
||
| 92 | */ |
||
| 93 | public function withCurrentLocation(): self |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param $input |
||
| 100 | * @param QueryStringParserInterface|null $queryStringParser |
||
| 101 | * @return QueryString |
||
| 102 | * @throws \InvalidArgumentException |
||
| 103 | * @throws \TypeError |
||
| 104 | */ |
||
| 105 | public static function factory($input = null, QueryStringParserInterface $queryStringParser = null): self |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getParams(): ?array |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $key |
||
| 129 | * @param array ...$deepKeys |
||
| 130 | * @return mixed|null |
||
| 131 | */ |
||
| 132 | public function getParam(string $key, ...$deepKeys) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $key |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function hasParam(string $key, ...$deepKeys): bool |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Yield key => value pairs. |
||
| 155 | * |
||
| 156 | * @param bool $decodeKeys |
||
| 157 | * @param bool $decodeValues |
||
| 158 | * @return Traversable |
||
| 159 | */ |
||
| 160 | public function getPairs(bool $decodeKeys = false, bool $decodeValues = false): Traversable |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $key |
||
| 167 | * @param $value |
||
| 168 | * @return QueryString |
||
| 169 | */ |
||
| 170 | public function withParam(string $key, $value): self |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param array $params |
||
| 179 | * @return QueryString |
||
| 180 | */ |
||
| 181 | public function withParams(array $params): self |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $key |
||
| 193 | * @param array ...$deepKeys |
||
| 194 | * @return QueryString |
||
| 195 | */ |
||
| 196 | public function withoutParam(string $key, ...$deepKeys): self |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return QueryStringRendererInterface |
||
| 218 | */ |
||
| 219 | public function getRenderer(): QueryStringRendererInterface |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param QueryStringRendererInterface $renderer |
||
| 226 | * @return QueryString |
||
| 227 | */ |
||
| 228 | public function withRenderer(QueryStringRendererInterface $renderer): self |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function __toString(): string |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param array $array |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | private function isAnIndexedArray(array $array): bool |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param array $params |
||
| 255 | * @param array ...$keys |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | private function removeFromPath(array $params, ...$keys): array |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns the default renderer. |
||
| 285 | * |
||
| 286 | * @return QueryStringRendererInterface |
||
| 287 | */ |
||
| 288 | public static function getDefaultRenderer(): QueryStringRendererInterface |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Changes default renderer. |
||
| 298 | * |
||
| 299 | * @param QueryStringRendererInterface $defaultRenderer |
||
| 300 | */ |
||
| 301 | public static function setDefaultRenderer(QueryStringRendererInterface $defaultRenderer): void |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Restores the default renderer. |
||
| 308 | */ |
||
| 309 | public static function restoreDefaultRenderer(): void |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns the default parser. |
||
| 316 | * |
||
| 317 | * @return QueryStringParserInterface |
||
| 318 | */ |
||
| 319 | public static function getDefaultParser(): QueryStringParserInterface |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Changes default parser. |
||
| 329 | * |
||
| 330 | * @param QueryStringParserInterface $defaultParser |
||
| 331 | */ |
||
| 332 | public static function setDefaultParser(QueryStringParserInterface $defaultParser): void |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Restores the default parser. |
||
| 339 | */ |
||
| 340 | public static function restoreDefaultParser(): void |
||
| 344 | } |
||
| 345 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..