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 |
||
28 | abstract class AbstractRestResourceController implements RestResourceControllerInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var Normalizer |
||
32 | */ |
||
33 | private $normalizer; |
||
34 | |||
35 | /** |
||
36 | * @var ValidatorInterface |
||
37 | */ |
||
38 | private $validator; |
||
39 | |||
40 | /** |
||
41 | * @var RestRequestParserInterface |
||
42 | */ |
||
43 | private $requestParser; |
||
44 | |||
45 | /** |
||
46 | * @var RequestStack |
||
47 | */ |
||
48 | private $requestStack; |
||
49 | |||
50 | /** |
||
51 | * @var MetadataFactoryInterface |
||
52 | */ |
||
53 | private $metadataFactory; |
||
54 | |||
55 | /** |
||
56 | * @var PropertyAccessorInterface |
||
57 | */ |
||
58 | private $propertyAccessor; |
||
59 | |||
60 | /** |
||
61 | * @var AuthorizationCheckerInterface |
||
62 | */ |
||
63 | private $authorizationChecker; |
||
64 | |||
65 | 80 | public function __construct( |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 10 | public function listAction(Request $request) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 14 | public function postAction(Request $request) |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | 32 | public function getAction(Request $request, $id) |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 12 | public function putAction(Request $request, $id) |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 4 | public function deleteAction(Request $request, $id) |
|
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | 6 | public function listSubresourceAction(Request $request, $id, string $subresource) |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 4 | public function postSubresourceAction(Request $request, $id, string $subresource) |
|
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | 12 | public function putSubresourceAction(Request $request, $id, string $subresource, $subId) |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 12 | public function deleteSubresourceAction(Request $request, $id, string $subresource, $subId = null) |
|
258 | |||
259 | /** |
||
260 | * @param object $entity |
||
261 | * |
||
262 | * @return object |
||
263 | */ |
||
264 | 12 | protected function postProcessPostedEntity($entity) |
|
268 | |||
269 | /** |
||
270 | * @param object $entity |
||
271 | * |
||
272 | * @return object |
||
273 | */ |
||
274 | 10 | protected function postProcessPuttedEntity($entity) |
|
278 | |||
279 | /** |
||
280 | * @param object $parent |
||
281 | * @param string $subresource |
||
282 | * @param object $entity |
||
283 | * |
||
284 | * @return object |
||
285 | */ |
||
286 | 2 | protected function postProcessSubResourcePostedEntity($parent, $subresource, $entity) |
|
290 | |||
291 | 80 | protected function getEntityClass() |
|
295 | |||
296 | 2 | protected function getSubResourceEntityClass($subresource) |
|
303 | |||
304 | 80 | protected function getCurrentRequest() |
|
308 | |||
309 | 70 | protected function assertMethodGranted(string $methodName, $entity = null) |
|
316 | |||
317 | /** |
||
318 | * @param string $methodName |
||
319 | * @param object $entity |
||
320 | * @param string $subresource |
||
321 | */ |
||
322 | 30 | protected function assertSubResourceMethodGranted($methodName, $entity, string $subresource): void |
|
332 | |||
333 | /** |
||
334 | * @return ClassMetadata |
||
335 | */ |
||
336 | 80 | protected function getClassMetadata() |
|
344 | |||
345 | protected function resolveSubject($entity, $propertyPath) |
||
354 | |||
355 | /** |
||
356 | * @param Right $right |
||
357 | * @param object $entity |
||
358 | */ |
||
359 | 46 | protected function assertRightGranted(Right $right, $entity = null) |
|
369 | |||
370 | 62 | protected function parseIncludes(Request $request) |
|
386 | |||
387 | 46 | protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
398 | |||
399 | 2 | protected function parseConstraintViolations(ConstraintViolationListInterface $errors) |
|
413 | |||
414 | 12 | protected function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | */ |
||
429 | 62 | protected function getNormalizer() |
|
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | */ |
||
437 | 24 | protected function getValidator() |
|
441 | |||
442 | /** |
||
443 | * {@inheritdoc} |
||
444 | */ |
||
445 | 24 | protected function getRequestParser() |
|
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | 80 | protected function getRequestStack() |
|
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | 80 | protected function getMetadataFactory() |
|
465 | |||
466 | /** |
||
467 | * {@inheritdoc} |
||
468 | */ |
||
469 | protected function getPropertyAccessor() |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | 46 | protected function getAuthorizationChecker(): ?AuthorizationCheckerInterface |
|
481 | |||
482 | /** |
||
483 | * @param AuthorizationCheckerInterface $authorizationChecker |
||
484 | */ |
||
485 | 76 | public function setAuthorizationChecker(AuthorizationCheckerInterface $authorizationChecker): void |
|
489 | |||
490 | /** |
||
491 | * @param int $page |
||
492 | * @param int $perPage |
||
493 | * |
||
494 | * @return Paginator|array |
||
495 | */ |
||
496 | abstract protected function listEntities(int $page = 1, int $perPage = 50); |
||
497 | |||
498 | /** |
||
499 | * @param int|string $id |
||
500 | * |
||
501 | * @return object |
||
502 | * |
||
503 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
504 | */ |
||
505 | abstract protected function fetchEntity($id); |
||
506 | |||
507 | /** |
||
508 | * @param object $entity |
||
509 | * |
||
510 | * @return object |
||
511 | */ |
||
512 | abstract protected function createEntity($entity); |
||
513 | |||
514 | /** |
||
515 | * @param object $entity |
||
516 | * |
||
517 | * @return object |
||
518 | * |
||
519 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
520 | */ |
||
521 | abstract protected function updateEntity($entity); |
||
522 | |||
523 | /** |
||
524 | * @param $entity |
||
525 | * |
||
526 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
527 | */ |
||
528 | abstract protected function removeEntity($entity); |
||
529 | |||
530 | /** |
||
531 | * @param object $entity |
||
532 | * @param string $subresource |
||
533 | * @param int $page |
||
534 | * @param int $perPage |
||
535 | * |
||
536 | * @return Paginator|array |
||
537 | */ |
||
538 | abstract protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50); |
||
539 | |||
540 | /** |
||
541 | * @param object $parent |
||
542 | * @param string $subresource |
||
543 | * |
||
544 | * @return object |
||
545 | */ |
||
546 | abstract protected function createAssociation($parent, string $subresource, $entity); |
||
547 | |||
548 | /** |
||
549 | * @param object $parent |
||
550 | * @param string $subresource |
||
551 | * @param int|string $subId |
||
552 | * |
||
553 | * @return object |
||
554 | */ |
||
555 | abstract protected function addAssociation($parent, string $subresource, $subId); |
||
556 | |||
557 | /** |
||
558 | * @param object $parent |
||
559 | * @param string $subresource |
||
560 | * @param int|string|null $subId |
||
561 | * |
||
562 | * @return mixed |
||
563 | */ |
||
564 | abstract protected function removeAssociation($parent, string $subresource, $subId = null); |
||
565 | } |
||
566 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.