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 | 82 | 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 | 6 | public function postSubresourceAction(Request $request, $id, string $subresource) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | 12 | public function putSubresourceAction(Request $request, $id, string $subresource, $subId) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | 12 | public function deleteSubresourceAction(Request $request, $id, string $subresource, $subId = null) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param object $entity |
||
| 322 | * |
||
| 323 | * @return object |
||
| 324 | */ |
||
| 325 | 12 | protected function postProcessPostedEntity($entity) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param object $entity |
||
| 332 | * |
||
| 333 | * @return object |
||
| 334 | */ |
||
| 335 | 10 | protected function postProcessPuttedEntity($entity) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param object $parent |
||
| 342 | * @param string $subresource |
||
| 343 | * @param object $entity |
||
| 344 | * |
||
| 345 | * @return object |
||
| 346 | */ |
||
| 347 | 4 | protected function postProcessSubResourcePostedEntity($parent, $subresource, $entity) |
|
| 351 | |||
| 352 | 82 | protected function getEntityClass() |
|
| 356 | |||
| 357 | 4 | protected function getSubResourceEntityClass($subresource) |
|
| 364 | |||
| 365 | 82 | protected function getCurrentRequest() |
|
| 369 | |||
| 370 | 70 | protected function assertMethodGranted(string $methodName, $entity = null) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $methodName |
||
| 380 | * @param object $entity |
||
| 381 | * @param string $subresource |
||
| 382 | */ |
||
| 383 | 32 | protected function assertSubResourceMethodGranted($methodName, $entity, string $subresource): void |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @return ClassMetadata |
||
| 396 | */ |
||
| 397 | 82 | protected function getClassMetadata() |
|
| 405 | |||
| 406 | protected function resolveSubject($entity, $propertyPath) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param Right $right |
||
| 418 | * @param object $entity |
||
| 419 | */ |
||
| 420 | 48 | protected function assertRightGranted(Right $right, $entity = null) |
|
| 430 | |||
| 431 | 64 | protected function parseIncludes(Request $request) |
|
| 447 | |||
| 448 | 48 | protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
| 459 | |||
| 460 | 2 | protected function parseConstraintViolations(ConstraintViolationListInterface $errors) |
|
| 474 | |||
| 475 | 12 | protected function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
| 486 | |||
| 487 | 26 | protected function getValidator() |
|
| 491 | |||
| 492 | 82 | protected function getRequestStack() |
|
| 496 | |||
| 497 | 82 | protected function getMetadataFactory() |
|
| 501 | |||
| 502 | protected function getPropertyAccessor() |
||
| 506 | |||
| 507 | 48 | protected function getAuthorizationChecker(): ?AuthorizationCheckerInterface |
|
| 511 | |||
| 512 | 64 | protected function getSerializer(): SerializerInterface |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
| 519 | */ |
||
| 520 | 78 | public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): void |
|
| 524 | |||
| 525 | /** |
||
| 526 | * @param int $page |
||
| 527 | * @param int $perPage |
||
| 528 | * |
||
| 529 | * @return Paginator|array |
||
| 530 | */ |
||
| 531 | abstract protected function listEntities(int $page = 1, int $perPage = 50); |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @param int|string $id |
||
| 535 | * |
||
| 536 | * @return object |
||
| 537 | * |
||
| 538 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
| 539 | */ |
||
| 540 | abstract protected function fetchEntity($id); |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @param object $entity |
||
| 544 | * |
||
| 545 | * @return object |
||
| 546 | */ |
||
| 547 | abstract protected function createEntity($entity); |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param object $entity |
||
| 551 | * |
||
| 552 | * @return object |
||
| 553 | * |
||
| 554 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
| 555 | */ |
||
| 556 | abstract protected function updateEntity($entity); |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @param $entity |
||
| 560 | * |
||
| 561 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
| 562 | */ |
||
| 563 | abstract protected function removeEntity($entity); |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param object $entity |
||
| 567 | * @param string $subresource |
||
| 568 | * @param int $page |
||
| 569 | * @param int $perPage |
||
| 570 | * |
||
| 571 | * @return Paginator|array |
||
| 572 | */ |
||
| 573 | abstract protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50); |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param object $parent |
||
| 577 | * @param string $subresource |
||
| 578 | * |
||
| 579 | * @return object |
||
| 580 | */ |
||
| 581 | abstract protected function buildAssociation($parent, string $subresource, $entity); |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @param object $associatedEntity |
||
| 585 | * |
||
| 586 | * @return object |
||
| 587 | */ |
||
| 588 | abstract protected function createAssociation($associatedEntity); |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param object $parent |
||
| 592 | * @param string $subresource |
||
| 593 | * @param int|string $subId |
||
| 594 | * |
||
| 595 | * @return object |
||
| 596 | */ |
||
| 597 | abstract protected function addAssociation($parent, string $subresource, $subId); |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param object $parent |
||
| 601 | * @param string $subresource |
||
| 602 | * @param int|string|null $subId |
||
| 603 | * |
||
| 604 | * @return mixed |
||
| 605 | */ |
||
| 606 | abstract protected function removeAssociation($parent, string $subresource, $subId = null); |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param Request $request |
||
| 610 | * @param string $subresource |
||
| 611 | * |
||
| 612 | * @return mixed |
||
| 613 | */ |
||
| 614 | 4 | protected function getSubresourcePostedEntity(Request $request, string $subresource) |
|
| 637 | } |
||
| 638 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.