Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BaseController 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 BaseController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Http; |
||
| 47 | abstract class BaseController implements ControllerInterface |
||
| 48 | { |
||
| 49 | use CreateResponsesTrait; |
||
| 50 | |||
| 51 | /** API class name */ |
||
| 52 | const API_CLASS = null; |
||
| 53 | |||
| 54 | /** JSON API Schema class name */ |
||
| 55 | const SCHEMA_CLASS = null; |
||
| 56 | |||
| 57 | /** JSON API validation rules set class */ |
||
| 58 | const ON_CREATE_VALIDATION_RULES_SET_CLASS = null; |
||
| 59 | |||
| 60 | /** JSON API validation rules set class */ |
||
| 61 | const ON_UPDATE_VALIDATION_RULES_SET_CLASS = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritdoc |
||
| 65 | */ |
||
| 66 | 12 | public static function index( |
|
| 67 | array $routeParams, |
||
| 68 | ContainerInterface $container, |
||
| 69 | ServerRequestInterface $request |
||
| 70 | ): ResponseInterface { |
||
| 71 | // By default no filters, sorts or includes are allowed from query. You can override this method to change it. |
||
| 72 | 12 | $parser = static::configureOnIndexParser(static::createQueryParser($container)) |
|
| 73 | 12 | ->parse($request->getQueryParams()); |
|
| 74 | 11 | $mapper = static::createParameterMapper($container); |
|
| 75 | 11 | $api = static::createApi($container); |
|
| 76 | |||
| 77 | 11 | $models = $mapper->applyQueryParameters($parser, $api)->index(); |
|
| 78 | |||
| 79 | 11 | $responses = static::createResponses($container, $request, $parser->createEncodingParameters()); |
|
| 80 | 11 | $response = ($models->getData()) === null ? |
|
| 81 | 11 | $responses->getCodeResponse(404) : $responses->getContentResponse($models); |
|
| 82 | |||
| 83 | 11 | return $response; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @inheritdoc |
||
| 88 | */ |
||
| 89 | 1 | public static function create( |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @inheritdoc |
||
| 112 | */ |
||
| 113 | 1 | public static function read( |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @inheritdoc |
||
| 136 | */ |
||
| 137 | 4 | public static function update( |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | 1 | public static function delete( |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $index |
||
| 172 | * @param string $relationshipName |
||
| 173 | * @param ContainerInterface $container |
||
| 174 | * @param ServerRequestInterface $request |
||
| 175 | * |
||
| 176 | * @return ResponseInterface |
||
| 177 | */ |
||
| 178 | 2 | View Code Duplication | protected static function readRelationship( |
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $index |
||
| 200 | * @param string $relationshipName |
||
| 201 | * @param ContainerInterface $container |
||
| 202 | * @param ServerRequestInterface $request |
||
| 203 | * |
||
| 204 | * @return ResponseInterface |
||
| 205 | */ |
||
| 206 | 1 | View Code Duplication | protected static function readRelationshipIdentifiers( |
| 225 | |||
| 226 | /** |
||
| 227 | * @param ContainerInterface $container |
||
| 228 | * @param string|null $class |
||
| 229 | * |
||
| 230 | * @return CrudInterface |
||
| 231 | */ |
||
| 232 | 21 | protected static function createApi(ContainerInterface $container, string $class = null): CrudInterface |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param ContainerInterface $container |
||
| 243 | * @param ServerRequestInterface $request |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | 7 | protected static function readJsonFromRequest(ContainerInterface $container, ServerRequestInterface $request): array |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param array $routeParams |
||
| 266 | * @param ContainerInterface $container |
||
| 267 | * @param array $jsonData |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | * |
||
| 271 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 272 | */ |
||
| 273 | 3 | protected static function normalizeIndexValueOnUpdate( |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param int|string $parentIndex |
||
| 306 | * @param string $relationshipName |
||
| 307 | * @param int|string $childIndex |
||
| 308 | * @param string $childApiClass |
||
| 309 | * @param ContainerInterface $container |
||
| 310 | * @param ServerRequestInterface $request |
||
| 311 | * |
||
| 312 | * @return ResponseInterface |
||
| 313 | */ |
||
| 314 | 1 | View Code Duplication | protected static function deleteInRelationship( |
| 334 | |||
| 335 | /** @noinspection PhpTooManyParametersInspection |
||
| 336 | * @param int|string $parentIndex |
||
| 337 | * @param string $relationshipName |
||
| 338 | * @param int|string $childIndex |
||
| 339 | * @param array $attributes |
||
| 340 | * @param array $toMany |
||
| 341 | * @param string $childApiClass |
||
| 342 | * @param ContainerInterface $container |
||
| 343 | * @param ServerRequestInterface $request |
||
| 344 | * |
||
| 345 | * @return ResponseInterface |
||
| 346 | */ |
||
| 347 | 2 | View Code Duplication | protected static function updateInRelationship( |
| 369 | |||
| 370 | /** |
||
| 371 | * @param ContainerInterface $container |
||
| 372 | * |
||
| 373 | * @return JsonApiValidatorInterface |
||
| 374 | */ |
||
| 375 | 1 | View Code Duplication | protected static function createOnCreateValidator(ContainerInterface $container): JsonApiValidatorInterface |
| 384 | |||
| 385 | /** |
||
| 386 | * @param ContainerInterface $container |
||
| 387 | * |
||
| 388 | * @return JsonApiValidatorInterface |
||
| 389 | */ |
||
| 390 | 2 | View Code Duplication | protected static function createOnUpdateValidator(ContainerInterface $container): JsonApiValidatorInterface |
| 399 | |||
| 400 | /** |
||
| 401 | * @param ContainerInterface $container |
||
| 402 | * @param string $rulesSetClass |
||
| 403 | * |
||
| 404 | * @return JsonApiValidatorInterface |
||
| 405 | */ |
||
| 406 | 5 | protected static function createJsonApiValidator( |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param ContainerInterface $container |
||
| 419 | * |
||
| 420 | * @return QueryParserInterface |
||
| 421 | */ |
||
| 422 | 16 | protected static function createQueryParser(ContainerInterface $container): QueryParserInterface |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @param QueryParserInterface $parser |
||
| 429 | * |
||
| 430 | * @return QueryParserInterface |
||
| 431 | */ |
||
| 432 | 12 | protected static function configureOnIndexParser(QueryParserInterface $parser): QueryParserInterface |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param QueryParserInterface $parser |
||
| 439 | * |
||
| 440 | * @return QueryParserInterface |
||
| 441 | */ |
||
| 442 | 1 | protected static function configureOnReadParser(QueryParserInterface $parser): QueryParserInterface |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $name |
||
| 449 | * @param QueryParserInterface $parser |
||
| 450 | * |
||
| 451 | * @return QueryParserInterface |
||
| 452 | * |
||
| 453 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 454 | */ |
||
| 455 | 2 | protected static function configureOnReadRelationshipParser(/** @noinspection PhpUnusedParameterInspection */ |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $name |
||
| 464 | * @param QueryParserInterface $parser |
||
| 465 | * |
||
| 466 | * @return QueryParserInterface |
||
| 467 | * |
||
| 468 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
| 469 | */ |
||
| 470 | 1 | protected static function configureOnReadRelationshipIdentifiersParser( |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @param ContainerInterface $container |
||
| 480 | * |
||
| 481 | * @return ParametersMapperInterface |
||
| 482 | */ |
||
| 483 | 15 | protected static function createParameterMapper(ContainerInterface $container): ParametersMapperInterface |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @param ContainerInterface $container |
||
| 497 | * @param array $captures |
||
| 498 | * @param string $schemeClass |
||
| 499 | * |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | 4 | protected static function mapSchemeDataToModelData( |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @param CrudInterface $api |
||
| 545 | * @param ResponsesInterface $responses |
||
| 546 | * @param string|int $index |
||
| 547 | * |
||
| 548 | * @return mixed |
||
| 549 | */ |
||
| 550 | 3 | private static function readImpl( |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @param string $index |
||
| 564 | * @param string $relationshipName |
||
| 565 | * @param ContainerInterface $container |
||
| 566 | * @param QueryParserInterface $parser |
||
| 567 | * |
||
| 568 | * @return PaginatedDataInterface |
||
| 569 | */ |
||
| 570 | 3 | private static function readRelationshipData( |
|
| 585 | |||
| 586 | /** |
||
| 587 | * @param string|int $index |
||
| 588 | * @param array $attributes |
||
| 589 | * @param array $toMany |
||
| 590 | * @param ContainerInterface $container |
||
| 591 | * @param ServerRequestInterface $request |
||
| 592 | * @param CrudInterface $api |
||
| 593 | * |
||
| 594 | * @return ResponseInterface |
||
| 595 | */ |
||
| 596 | 2 | private static function updateImpl( |
|
| 610 | |||
| 611 | /** |
||
| 612 | * @param string|int $index |
||
| 613 | * @param ContainerInterface $container |
||
| 614 | * @param ServerRequestInterface $request |
||
| 615 | * @param CrudInterface $api |
||
| 616 | * |
||
| 617 | * @return ResponseInterface |
||
| 618 | */ |
||
| 619 | 2 | private static function deleteImpl( |
|
| 630 | |||
| 631 | /** |
||
| 632 | * @param ContainerInterface $container |
||
| 633 | * @param string $namespace |
||
| 634 | * |
||
| 635 | * @return FormatterInterface |
||
| 636 | */ |
||
| 637 | 2 | protected static function createMessageFormatter( |
|
| 647 | } |
||
| 648 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: