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 | 25 | public function __construct(PaginationStrategyInterface $paginationStrategy, array $messages = null) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritdoc |
||
| 90 | */ |
||
| 91 | public function withAllowedFilterFields(array $fields): QueryParserInterface |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @inheritdoc |
||
| 110 | */ |
||
| 111 | 8 | public function withAllAllowedFilterFields(): QueryParserInterface |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritdoc |
||
| 120 | */ |
||
| 121 | 25 | public function withNoAllowedFilterFields(): QueryParserInterface |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | public function withAllowedSortFields(array $fields): QueryParserInterface |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | 8 | public function withAllAllowedSortFields(): QueryParserInterface |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | 25 | public function withNoAllowedSortFields(): QueryParserInterface |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritdoc |
||
| 170 | */ |
||
| 171 | public function withAllowedIncludePaths(array $paths): QueryParserInterface |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | 8 | public function withAllAllowedIncludePaths(): QueryParserInterface |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @inheritdoc |
||
| 200 | */ |
||
| 201 | 25 | public function withNoAllowedIncludePaths(): QueryParserInterface |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @inheritdoc |
||
| 210 | */ |
||
| 211 | 24 | public function parse(array $parameters): QueryParserInterface |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @inheritdoc |
||
| 222 | */ |
||
| 223 | 18 | public function areFiltersWithAnd(): bool |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | 17 | public function getFilters(): iterable |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @inheritdoc |
||
| 248 | */ |
||
| 249 | 16 | public function getSorts(): iterable |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @inheritdoc |
||
| 260 | */ |
||
| 261 | 16 | public function getIncludes(): iterable |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @inheritdoc |
||
| 272 | */ |
||
| 273 | 15 | public function getPagingOffset(): ?int |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritdoc |
||
| 280 | */ |
||
| 281 | 15 | public function getPagingLimit(): ?int |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | */ |
||
| 289 | 15 | public function createEncodingParameters(): EncodingParametersInterface |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @return self |
||
| 304 | */ |
||
| 305 | 24 | private function parsePagingParameters(): self |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Pre-parsing for filter parameters. |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | 24 | private function parseFilterLink(): self |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param array $values |
||
| 363 | * |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | 20 | private function setFilterParameters(array $values): self |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | 17 | private function getFilterParameters(): array |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | 18 | private function setFiltersWithAnd(): self |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @return self |
||
| 393 | */ |
||
| 394 | 2 | private function setFiltersWithOr(): self |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @return PaginationStrategyInterface |
||
| 403 | */ |
||
| 404 | 24 | private function getPaginationStrategy(): PaginationStrategyInterface |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @return self |
||
| 411 | */ |
||
| 412 | 25 | private function clear(): self |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $parameterName |
||
| 426 | * @param array $value |
||
| 427 | * |
||
| 428 | * @return iterable |
||
| 429 | */ |
||
| 430 | 8 | private function parseOperationsAndArguments(string $parameterName, array $value): iterable |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @param iterable $input |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | 15 | private function deepReadIterable(iterable $input): array |
|
| 461 | } |
||
| 462 |
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.