Complex classes like ParametersMapper 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 ParametersMapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Http\Query; |
||
| 38 | class ParametersMapper implements ParametersMapperInterface |
||
| 39 | { |
||
| 40 | /** Message */ |
||
| 41 | public const MSG_ERR_INVALID_OPERATION = 'Invalid Operation.'; |
||
| 42 | |||
| 43 | /** Message */ |
||
| 44 | public const MSG_ERR_INVALID_FIELD = 'Invalid field.'; |
||
| 45 | |||
| 46 | /** Message */ |
||
| 47 | public const MSG_ERR_ROOT_SCHEME_IS_NOT_SET = 'Root Scheme is not set.'; |
||
| 48 | |||
| 49 | /** Message */ |
||
| 50 | private const MSG_PARAM_INCLUDE = QueryParserInterface::PARAM_INCLUDE; |
||
| 51 | |||
| 52 | /** Message */ |
||
| 53 | private const MSG_PARAM_FILTER = QueryParserInterface::PARAM_FILTER; |
||
| 54 | |||
| 55 | /** internal constant */ |
||
| 56 | private const REL_FILTER_INDEX = 0; |
||
| 57 | |||
| 58 | /** internal constant */ |
||
| 59 | private const REL_SORT_INDEX = 1; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var SchemaInterface |
||
| 63 | */ |
||
| 64 | private $rootScheme; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var JsonSchemesInterface |
||
| 68 | */ |
||
| 69 | private $jsonSchemes; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array|null |
||
| 73 | */ |
||
| 74 | private $messages; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var iterable |
||
| 78 | */ |
||
| 79 | private $filters; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var iterable |
||
| 83 | */ |
||
| 84 | private $sorts; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var iterable |
||
| 88 | */ |
||
| 89 | private $includes; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param JsonSchemesInterface $jsonSchemes |
||
| 93 | * @param array|null $messages |
||
| 94 | */ |
||
| 95 | 19 | public function __construct(JsonSchemesInterface $jsonSchemes, array $messages = null) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritdoc |
||
| 105 | */ |
||
| 106 | 18 | public function selectRootSchemeByResourceType(string $resourceType): ParametersMapperInterface |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @inheritdoc |
||
| 115 | */ |
||
| 116 | 19 | public function withFilters(iterable $filters): ParametersMapperInterface |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | 19 | public function withoutFilters(): self |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | 19 | public function withSorts(iterable $sorts): ParametersMapperInterface |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return self |
||
| 168 | */ |
||
| 169 | 19 | public function withoutSorts(): self |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | 19 | public function withIncludes(iterable $includes): ParametersMapperInterface |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | 19 | public function withoutIncludes(): self |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritdoc |
||
| 204 | */ |
||
| 205 | 16 | public function getMappedFilters(): iterable |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @inheritdoc |
||
| 226 | */ |
||
| 227 | 16 | public function getMappedSorts(): iterable |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @inheritdoc |
||
| 244 | */ |
||
| 245 | 16 | public function getMappedIncludes(): iterable |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @inheritdoc |
||
| 274 | */ |
||
| 275 | 15 | public function applyQueryParameters(QueryParserInterface $parser, CrudInterface $api): CrudInterface |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $field |
||
| 366 | * |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | 9 | private function mapToRelationshipAndAttribute(string $field): array |
|
| 370 | { |
||
| 371 | 9 | $rootSchema = $this->getRootScheme(); |
|
| 372 | 9 | if ($rootSchema->hasAttributeMapping($field) === true) { |
|
| 373 | 6 | $relationship = null; |
|
| 374 | 6 | $scheme = $rootSchema; |
|
| 375 | 6 | $attribute = new Attribute($field, $scheme); |
|
| 376 | |||
| 377 | 6 | return [$relationship, $attribute]; |
|
| 378 | 6 | } elseif ($rootSchema->hasRelationshipMapping($field) === true) { |
|
| 379 | 4 | $fromScheme = $rootSchema; |
|
| 380 | 4 | $toScheme = $this->getJsonSchemes()->getRelationshipSchema(get_class($fromScheme), $field); |
|
| 381 | 4 | $relationship = new Relationship($field, $fromScheme, $toScheme); |
|
| 382 | 4 | $attribute = new Attribute($toScheme::RESOURCE_ID, $toScheme); |
|
| 383 | |||
| 384 | 4 | return [$relationship, $attribute]; |
|
| 385 | 4 | } elseif (count($mightBeRelAndAttr = explode('.', $field, 2)) === 2) { |
|
| 386 | // Last chance. It could be a dot ('.') separated relationship with an attribute. |
||
| 387 | |||
| 388 | 4 | $mightBeRel = $mightBeRelAndAttr[0]; |
|
| 389 | 4 | $mightBeAttr = $mightBeRelAndAttr[1]; |
|
| 390 | |||
| 391 | 4 | $fromScheme = $rootSchema; |
|
| 392 | 4 | if ($fromScheme->hasRelationshipMapping($mightBeRel)) { |
|
| 393 | 4 | $toScheme = $this->getJsonSchemes()->getRelationshipSchema(get_class($fromScheme), $mightBeRel); |
|
| 394 | 4 | if ($toScheme::hasAttributeMapping($mightBeAttr) === true) { |
|
| 395 | 4 | $relationship = new Relationship($mightBeRel, $fromScheme, $toScheme); |
|
| 396 | 4 | $attribute = new Attribute($mightBeAttr, $toScheme); |
|
| 397 | |||
| 398 | 4 | return [$relationship, $attribute]; |
|
| 399 | } |
||
| 400 | } |
||
| 401 | } |
||
| 402 | |||
| 403 | $error = $this->createQueryError($field, static::MSG_ERR_INVALID_FIELD); |
||
| 404 | throw new JsonApiException($error); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string $parameterName |
||
| 409 | * @param iterable $value |
||
| 410 | * |
||
| 411 | * @return iterable |
||
| 412 | */ |
||
| 413 | 7 | private function parseOperationsAndArguments(string $parameterName, iterable $value): iterable |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @return SchemaInterface |
||
| 481 | */ |
||
| 482 | 18 | private function getRootScheme(): SchemaInterface |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @return JsonSchemesInterface |
||
| 493 | */ |
||
| 494 | 18 | private function getJsonSchemes(): JsonSchemesInterface |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @return iterable |
||
| 501 | */ |
||
| 502 | 16 | private function getFilters(): iterable |
|
| 506 | |||
| 507 | /** |
||
| 508 | * @return iterable |
||
| 509 | */ |
||
| 510 | 16 | private function getSorts(): iterable |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @return iterable |
||
| 517 | */ |
||
| 518 | 16 | private function getIncludes(): iterable |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $name |
||
| 525 | * @param string $title |
||
| 526 | * |
||
| 527 | * @return Error |
||
| 528 | */ |
||
| 529 | private function createQueryError(string $name, string $title): Error |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $message |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | private function getMessage(string $message): string |
||
| 549 | } |
||
| 550 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.