Complex classes like QueryParser 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 QueryParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Http\Query; |
||
| 29 | class QueryParser extends BaseQueryParser implements QueryParserInterface |
||
| 30 | { |
||
| 31 | /** Message */ |
||
| 32 | public const MSG_ERR_INVALID_OPERATION_ARGUMENTS = 'Invalid Operation Arguments.'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var PaginationStrategyInterface |
||
| 36 | */ |
||
| 37 | private $paginationStrategy; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $filterParameters; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $areFiltersWithAnd; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int|null |
||
| 51 | */ |
||
| 52 | private $pagingOffset; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int|null |
||
| 56 | */ |
||
| 57 | private $pagingLimit; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string[]|null |
||
| 61 | */ |
||
| 62 | private $allowedFilterFields; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string[]|null |
||
| 66 | */ |
||
| 67 | private $allowedSortFields; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string[]|null |
||
| 71 | */ |
||
| 72 | private $allowedIncludePaths; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param PaginationStrategyInterface $paginationStrategy |
||
| 76 | * @param string[]|null $messages |
||
| 77 | */ |
||
| 78 | 28 | public function __construct(PaginationStrategyInterface $paginationStrategy, array $messages = null) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritdoc |
||
| 90 | */ |
||
| 91 | 10 | public function withAllowedFilterFields(array $fields): QueryParserInterface |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @inheritdoc |
||
| 112 | */ |
||
| 113 | 11 | public function withAllAllowedFilterFields(): QueryParserInterface |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | 28 | public function withNoAllowedFilterFields(): QueryParserInterface |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | */ |
||
| 133 | 9 | public function withAllowedSortFields(array $fields): QueryParserInterface |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | */ |
||
| 155 | 11 | public function withAllAllowedSortFields(): QueryParserInterface |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | 28 | public function withNoAllowedSortFields(): QueryParserInterface |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @inheritdoc |
||
| 174 | */ |
||
| 175 | 12 | public function withAllowedIncludePaths(array $paths): QueryParserInterface |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @inheritdoc |
||
| 196 | */ |
||
| 197 | 11 | public function withAllAllowedIncludePaths(): QueryParserInterface |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | 28 | public function withNoAllowedIncludePaths(): QueryParserInterface |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritdoc |
||
| 216 | */ |
||
| 217 | 27 | public function parse(array $parameters): QueryParserInterface |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @inheritdoc |
||
| 228 | */ |
||
| 229 | 18 | public function areFiltersWithAnd(): bool |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | 19 | public function getFilters(): iterable |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @inheritdoc |
||
| 254 | */ |
||
| 255 | 16 | public function getSorts(): iterable |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @inheritdoc |
||
| 266 | */ |
||
| 267 | 17 | public function getIncludes(): iterable |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @inheritdoc |
||
| 278 | */ |
||
| 279 | 15 | public function getPagingOffset(): ?int |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritdoc |
||
| 286 | */ |
||
| 287 | 15 | public function getPagingLimit(): ?int |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @inheritdoc |
||
| 294 | */ |
||
| 295 | 16 | public function createEncodingParameters(): EncodingParametersInterface |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | 27 | private function parsePagingParameters(): self |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Pre-parsing for filter parameters. |
||
| 324 | * |
||
| 325 | * @return self |
||
| 326 | */ |
||
| 327 | 27 | private function parseFilterLink(): self |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @param array $values |
||
| 369 | * |
||
| 370 | * @return self |
||
| 371 | */ |
||
| 372 | 23 | private function setFilterParameters(array $values): self |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | 19 | private function getFilterParameters(): array |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @return self |
||
| 389 | */ |
||
| 390 | 21 | private function setFiltersWithAnd(): self |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @return self |
||
| 399 | */ |
||
| 400 | 2 | private function setFiltersWithOr(): self |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @return PaginationStrategyInterface |
||
| 409 | */ |
||
| 410 | 27 | private function getPaginationStrategy(): PaginationStrategyInterface |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @return self |
||
| 417 | */ |
||
| 418 | 28 | private function clear(): self |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $parameterName |
||
| 432 | * @param array $value |
||
| 433 | * |
||
| 434 | * @return iterable |
||
| 435 | */ |
||
| 436 | 9 | private function parseOperationsAndArguments(string $parameterName, array $value): iterable |
|
| 451 | |||
| 452 | /** |
||
| 453 | * @param iterable $input |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | 16 | private function deepReadIterable(iterable $input): array |
|
| 467 | } |
||
| 468 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.