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 | * @param PaginationStrategyInterface $paginationStrategy |
||
| 61 | * @param string[]|null $messages |
||
| 62 | */ |
||
| 63 | 25 | public function __construct(PaginationStrategyInterface $paginationStrategy, array $messages = null) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @inheritdoc |
||
| 75 | */ |
||
| 76 | 24 | public function parse(array $parameters): QueryParserInterface |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | */ |
||
| 90 | 18 | public function areFiltersWithAnd(): bool |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | */ |
||
| 98 | 17 | public function getFilters(): iterable |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @inheritdoc |
||
| 113 | */ |
||
| 114 | 15 | public function getPagingOffset(): ?int |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @inheritdoc |
||
| 121 | */ |
||
| 122 | 15 | public function getPagingLimit(): ?int |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
| 130 | 14 | public function createEncodingParameters(): EncodingParametersInterface |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @return self |
||
| 145 | */ |
||
| 146 | 24 | private function parsePagingParameters(): self |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Pre-parsing for filter parameters. |
||
| 159 | * |
||
| 160 | * @return self |
||
| 161 | */ |
||
| 162 | 24 | private function parseFilterLink(): self |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param array $values |
||
| 204 | * |
||
| 205 | * @return self |
||
| 206 | */ |
||
| 207 | 20 | private function setFilterParameters(array $values): self |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | 17 | private function getFilterParameters(): array |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return self |
||
| 224 | */ |
||
| 225 | 18 | private function setFiltersWithAnd(): self |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | 2 | private function setFiltersWithOr(): self |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return PaginationStrategyInterface |
||
| 244 | */ |
||
| 245 | 24 | private function getPaginationStrategy(): PaginationStrategyInterface |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | 25 | private function clear(): self |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $parameterName |
||
| 265 | * @param array $value |
||
| 266 | * |
||
| 267 | * @return iterable |
||
| 268 | */ |
||
| 269 | 8 | private function parseOperationsAndArguments(string $parameterName, array $value): iterable |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param iterable $input |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | 14 | private function deepReadIterable(iterable $input): array |
|
| 300 | } |
||
| 301 |
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.