Complex classes like AbstractRestResourceController 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 AbstractRestResourceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | abstract class AbstractRestResourceController implements RestResourceControllerInterface |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var ValidatorInterface |
||
| 33 | */ |
||
| 34 | private $validator; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var RequestStack |
||
| 38 | */ |
||
| 39 | private $requestStack; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var RestMetadataFactory |
||
| 43 | */ |
||
| 44 | private $metadataFactory; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var PropertyAccessorInterface |
||
| 48 | */ |
||
| 49 | private $propertyAccessor; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var AuthorizationCheckerInterface |
||
| 53 | */ |
||
| 54 | private $authorizationChecker; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var SerializerInterface |
||
| 58 | */ |
||
| 59 | private $serializer; |
||
| 60 | |||
| 61 | 80 | public function __construct( |
|
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 10 | public function listAction(Request $request) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 14 | public function postAction(Request $request) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | 32 | public function getAction(Request $request, $id) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 12 | public function putAction(Request $request, $id) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 4 | public function deleteAction(Request $request, $id) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | 6 | public function listSubresourceAction(Request $request, $id, string $subresource) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | 4 | public function postSubresourceAction(Request $request, $id, string $subresource) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | 12 | public function putSubresourceAction(Request $request, $id, string $subresource, $subId) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | 12 | public function deleteSubresourceAction(Request $request, $id, string $subresource, $subId = null) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param object $entity |
||
| 327 | * |
||
| 328 | * @return object |
||
| 329 | */ |
||
| 330 | 12 | protected function postProcessPostedEntity($entity) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param object $entity |
||
| 337 | * |
||
| 338 | * @return object |
||
| 339 | */ |
||
| 340 | 10 | protected function postProcessPuttedEntity($entity) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param object $parent |
||
| 347 | * @param string $subresource |
||
| 348 | * @param object $entity |
||
| 349 | * |
||
| 350 | * @return object |
||
| 351 | */ |
||
| 352 | 2 | protected function postProcessSubResourcePostedEntity($parent, $subresource, $entity) |
|
| 356 | |||
| 357 | 80 | protected function getEntityClass() |
|
| 361 | |||
| 362 | 2 | protected function getSubResourceEntityClass($subresource) |
|
| 369 | |||
| 370 | 80 | protected function getCurrentRequest() |
|
| 374 | |||
| 375 | 70 | protected function assertMethodGranted(string $methodName, $entity = null) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $methodName |
||
| 385 | * @param object $entity |
||
| 386 | * @param string $subresource |
||
| 387 | */ |
||
| 388 | 30 | protected function assertSubResourceMethodGranted($methodName, $entity, string $subresource): void |
|
| 398 | |||
| 399 | /** |
||
| 400 | * @return ClassMetadata |
||
| 401 | */ |
||
| 402 | 80 | protected function getClassMetadata() |
|
| 410 | |||
| 411 | protected function resolveSubject($entity, $propertyPath) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param Right $right |
||
| 423 | * @param object $entity |
||
| 424 | */ |
||
| 425 | 46 | protected function assertRightGranted(Right $right, $entity = null) |
|
| 435 | |||
| 436 | 62 | protected function parseIncludes(Request $request) |
|
| 452 | |||
| 453 | 46 | protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
| 464 | |||
| 465 | 2 | protected function parseConstraintViolations(ConstraintViolationListInterface $errors) |
|
| 479 | |||
| 480 | 12 | protected function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
| 491 | |||
| 492 | 24 | protected function getValidator() |
|
| 496 | |||
| 497 | 80 | protected function getRequestStack() |
|
| 501 | |||
| 502 | 80 | protected function getMetadataFactory() |
|
| 506 | |||
| 507 | protected function getPropertyAccessor() |
||
| 511 | |||
| 512 | 46 | protected function getAuthorizationChecker(): ?AuthorizationCheckerInterface |
|
| 516 | |||
| 517 | 62 | protected function getSerializer(): SerializerInterface |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 524 | */ |
||
| 525 | 76 | public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): void |
|
| 529 | |||
| 530 | /** |
||
| 531 | * @param int $page |
||
| 532 | * @param int $perPage |
||
| 533 | * |
||
| 534 | * @return Paginator|array |
||
| 535 | */ |
||
| 536 | abstract protected function listEntities(int $page = 1, int $perPage = 50); |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param int|string $id |
||
| 540 | * |
||
| 541 | * @return object |
||
| 542 | * |
||
| 543 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
| 544 | */ |
||
| 545 | abstract protected function fetchEntity($id); |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param object $entity |
||
| 549 | * |
||
| 550 | * @return object |
||
| 551 | */ |
||
| 552 | abstract protected function createEntity($entity); |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param object $entity |
||
| 556 | * |
||
| 557 | * @return object |
||
| 558 | * |
||
| 559 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
| 560 | */ |
||
| 561 | abstract protected function updateEntity($entity); |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param $entity |
||
| 565 | * |
||
| 566 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
| 567 | */ |
||
| 568 | abstract protected function removeEntity($entity); |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param object $entity |
||
| 572 | * @param string $subresource |
||
| 573 | * @param int $page |
||
| 574 | * @param int $perPage |
||
| 575 | * |
||
| 576 | * @return Paginator|array |
||
| 577 | */ |
||
| 578 | abstract protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50); |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @param object $parent |
||
| 582 | * @param string $subresource |
||
| 583 | * |
||
| 584 | * @return object |
||
| 585 | */ |
||
| 586 | abstract protected function buildAssociation($parent, string $subresource, $entity); |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param object $associatedEntity |
||
| 590 | * |
||
| 591 | * @return object |
||
| 592 | */ |
||
| 593 | abstract protected function createAssociation($associatedEntity); |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param object $parent |
||
| 597 | * @param string $subresource |
||
| 598 | * @param int|string $subId |
||
| 599 | * |
||
| 600 | * @return object |
||
| 601 | */ |
||
| 602 | abstract protected function addAssociation($parent, string $subresource, $subId); |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param object $parent |
||
| 606 | * @param string $subresource |
||
| 607 | * @param int|string|null $subId |
||
| 608 | * |
||
| 609 | * @return mixed |
||
| 610 | */ |
||
| 611 | abstract protected function removeAssociation($parent, string $subresource, $subId = null); |
||
| 612 | } |
||
| 613 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.