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\Validation\JsonApi; |
||
| 45 | class QueryParser extends BaseQueryParser implements JsonApiQueryValidatingParserInterface |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $filterParameters; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private $areFiltersWithAnd; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int|null |
||
| 59 | */ |
||
| 60 | private $pagingOffset; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var int|null |
||
| 64 | */ |
||
| 65 | private $pagingLimit; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * NOTE: Despite the type it is just a string so only static methods can be called from the interface. |
||
| 69 | * |
||
| 70 | * @var JsonApiQueryRulesSerializerInterface|string |
||
| 71 | */ |
||
| 72 | private $serializerClass; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $serializedRuleSet; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $validationBlocks; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var ContextStorageInterface |
||
| 86 | */ |
||
| 87 | private $context; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var CaptureAggregatorInterface |
||
| 91 | */ |
||
| 92 | private $captures; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var ErrorAggregatorInterface |
||
| 96 | */ |
||
| 97 | private $validationErrors; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var JsonApiErrorCollection |
||
| 101 | */ |
||
| 102 | private $jsonErrors; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var null|array |
||
| 106 | */ |
||
| 107 | private $cachedFilters = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var null|array |
||
| 111 | */ |
||
| 112 | private $cachedFields = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var null|array |
||
| 116 | */ |
||
| 117 | private $cachedSorts = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var null|array |
||
| 121 | */ |
||
| 122 | private $cachedIncludes = null; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var FormatterFactoryInterface |
||
| 126 | */ |
||
| 127 | private $formatterFactory; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var FormatterInterface|null |
||
| 131 | */ |
||
| 132 | private $formatter; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $rulesClass |
||
| 136 | * @param string $serializerClass |
||
| 137 | * @param array $serializedData |
||
| 138 | * @param ContextStorageInterface $context |
||
| 139 | * @param CaptureAggregatorInterface $captures |
||
| 140 | * @param ErrorAggregatorInterface $validationErrors |
||
| 141 | * @param JsonApiErrorCollection $jsonErrors |
||
| 142 | * @param FormatterFactoryInterface $formatterFactory |
||
| 143 | * @param string[]|null $messages |
||
| 144 | */ |
||
| 145 | 32 | public function __construct( |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | 31 | public function parse(array $parameters): JsonApiQueryValidatingParserInterface |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @inheritdoc |
||
| 193 | */ |
||
| 194 | 17 | public function areFiltersWithAnd(): bool |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @inheritdoc |
||
| 201 | */ |
||
| 202 | 1 | public function hasFilters(): bool |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @inheritdoc |
||
| 209 | */ |
||
| 210 | 14 | public function hasFields(): bool |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @inheritdoc |
||
| 217 | */ |
||
| 218 | 14 | public function hasIncludes(): bool |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @inheritdoc |
||
| 225 | */ |
||
| 226 | 1 | public function hasSorts(): bool |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritdoc |
||
| 233 | */ |
||
| 234 | 1 | public function hasPaging(): bool |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @inheritdoc |
||
| 241 | */ |
||
| 242 | 22 | public function getFilters(): array |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @inheritdoc |
||
| 253 | */ |
||
| 254 | 2 | public function getFields(): array |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @inheritdoc |
||
| 265 | */ |
||
| 266 | 16 | public function getSorts(): array |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | 16 | public function getIncludes(): iterable |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @inheritdoc |
||
| 289 | */ |
||
| 290 | 17 | public function getPagingOffset(): ?int |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @inheritdoc |
||
| 297 | */ |
||
| 298 | 17 | public function getPagingLimit(): ?int |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return JsonApiErrorCollection |
||
| 305 | */ |
||
| 306 | 6 | protected function getJsonErrors(): JsonApiErrorCollection |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @return ErrorAggregatorInterface |
||
| 313 | */ |
||
| 314 | 32 | protected function getValidationErrors(): ErrorAggregatorInterface |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @return CaptureAggregatorInterface |
||
| 321 | */ |
||
| 322 | 32 | protected function getCaptures(): CaptureAggregatorInterface |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @return ContextStorageInterface |
||
| 329 | */ |
||
| 330 | 30 | protected function getContext(): ContextStorageInterface |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | 30 | protected function getValidationBlocks(): array |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | 31 | protected function getSerializedRuleSet(): array |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @return FormatterFactoryInterface |
||
| 353 | */ |
||
| 354 | 1 | protected function getFormatterFactory(): FormatterFactoryInterface |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @return FormatterInterface |
||
| 361 | */ |
||
| 362 | 1 | protected function getFormatter(): FormatterInterface |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $name |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 17 | protected function hasParameter(string $name): bool |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @return iterable |
||
| 383 | * |
||
| 384 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 385 | */ |
||
| 386 | 22 | private function getValidatedFilters(): iterable |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @param iterable $fieldsFromParent |
||
| 425 | * |
||
| 426 | * @return iterable |
||
| 427 | * |
||
| 428 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 429 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 430 | */ |
||
| 431 | 2 | private function getValidatedFields(iterable $fieldsFromParent): iterable |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @param iterable $sortsFromParent |
||
| 460 | * |
||
| 461 | * @return iterable |
||
| 462 | * |
||
| 463 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 464 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 465 | */ |
||
| 466 | 16 | private function getValidatedSorts(iterable $sortsFromParent): iterable |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param iterable $includesFromParent |
||
| 492 | * |
||
| 493 | * @return iterable |
||
| 494 | * |
||
| 495 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 496 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 497 | */ |
||
| 498 | 16 | private function getValidatedIncludes(iterable $includesFromParent): iterable |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param iterable $iterable |
||
| 524 | * |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | 25 | private function iterableToArray(iterable $iterable): array |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param mixed $value |
||
| 540 | * |
||
| 541 | * @return int |
||
| 542 | */ |
||
| 543 | 31 | private function validatePageOffset($value): int |
|
| 550 | |||
| 551 | /** |
||
| 552 | * @param mixed $value |
||
| 553 | * |
||
| 554 | * @return int |
||
| 555 | */ |
||
| 556 | 31 | private function validatePageLimit($value): int |
|
| 563 | |||
| 564 | /** |
||
| 565 | * @param mixed $value |
||
| 566 | * @param array $ruleIndexes |
||
| 567 | * |
||
| 568 | * @return int |
||
| 569 | */ |
||
| 570 | 31 | private function validatePaginationValue($value, ?array $ruleIndexes): int |
|
| 587 | |||
| 588 | /** |
||
| 589 | * @param string $paramName |
||
| 590 | * @param array $ruleIndexes |
||
| 591 | * |
||
| 592 | * @return void |
||
| 593 | * |
||
| 594 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 595 | */ |
||
| 596 | 30 | private function validationStarts(string $paramName, array $ruleIndexes): void |
|
| 609 | |||
| 610 | /** |
||
| 611 | * @param string $paramName |
||
| 612 | * @param mixed $value |
||
| 613 | * @param int $ruleIndex |
||
| 614 | * |
||
| 615 | * @return void |
||
| 616 | * |
||
| 617 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 618 | */ |
||
| 619 | 30 | private function validateAndThrowOnError(string $paramName, $value, int $ruleIndex): void |
|
| 631 | |||
| 632 | /** |
||
| 633 | * @param mixed $value |
||
| 634 | * @param int $ruleIndex |
||
| 635 | * |
||
| 636 | * @return bool |
||
| 637 | * |
||
| 638 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 639 | */ |
||
| 640 | 16 | private function validateAndAccumulateError($value, int $ruleIndex): bool |
|
| 651 | |||
| 652 | /** |
||
| 653 | * @param string $paramName |
||
| 654 | * @param array $ruleIndexes |
||
| 655 | * |
||
| 656 | * @return void |
||
| 657 | * |
||
| 658 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 659 | */ |
||
| 660 | 30 | private function validateEnds(string $paramName, array $ruleIndexes): void |
|
| 670 | |||
| 671 | /** |
||
| 672 | * @return mixed |
||
| 673 | */ |
||
| 674 | 30 | private function readSingleCapturedValue() |
|
| 681 | |||
| 682 | /** |
||
| 683 | * @param int $ruleIndex |
||
| 684 | * @param iterable $values |
||
| 685 | * |
||
| 686 | * @return iterable |
||
| 687 | */ |
||
| 688 | 10 | private function validateValues(int $ruleIndex, iterable $values): iterable |
|
| 698 | |||
| 699 | /** |
||
| 700 | * @param int $ruleIndex |
||
| 701 | * @param iterable $opsAndArgs |
||
| 702 | * |
||
| 703 | * @return iterable |
||
| 704 | */ |
||
| 705 | 10 | private function validateFilterArguments(int $ruleIndex, iterable $opsAndArgs): iterable |
|
| 711 | |||
| 712 | /** |
||
| 713 | * @return self |
||
| 714 | */ |
||
| 715 | 31 | private function parsePagingParameters(): self |
|
| 729 | |||
| 730 | /** |
||
| 731 | * @param string $paramName |
||
| 732 | * |
||
| 733 | * @return void |
||
| 734 | * |
||
| 735 | * @throws JsonApiException |
||
| 736 | */ |
||
| 737 | 30 | private function checkValidationQueueErrors(string $paramName): void |
|
| 747 | |||
| 748 | /** |
||
| 749 | * @param array $values |
||
| 750 | * |
||
| 751 | * @return self |
||
| 752 | */ |
||
| 753 | 27 | private function setFilterParameters(array $values): self |
|
| 759 | |||
| 760 | /** |
||
| 761 | * @return array |
||
| 762 | */ |
||
| 763 | 22 | private function getFilterParameters(): array |
|
| 767 | |||
| 768 | /** |
||
| 769 | * @return self |
||
| 770 | */ |
||
| 771 | 25 | private function setFiltersWithAnd(): self |
|
| 777 | |||
| 778 | /** |
||
| 779 | * @return self |
||
| 780 | */ |
||
| 781 | 2 | private function setFiltersWithOr(): self |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @return self |
||
| 790 | */ |
||
| 791 | 32 | private function clear(): self |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Pre-parsing for filter parameters. |
||
| 811 | * |
||
| 812 | * @return self |
||
| 813 | * |
||
| 814 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 815 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 816 | */ |
||
| 817 | 31 | private function parseFilterLink(): self |
|
| 856 | |||
| 857 | /** |
||
| 858 | * @param string $parameterName |
||
| 859 | * @param array $value |
||
| 860 | * |
||
| 861 | * @return iterable |
||
| 862 | * |
||
| 863 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 864 | */ |
||
| 865 | 11 | private function parseOperationsAndArguments(string $parameterName, array $value): iterable |
|
| 884 | } |
||
| 885 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.