Complex classes like RestResourceController 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 RestResourceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class RestResourceController implements ContainerAwareInterface, RestResourceControllerInterface |
||
| 31 | { |
||
| 32 | use ContainerAwareTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | 10 | public function listAction(Request $request) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 4 | public function postAction(Request $request) |
|
| 67 | { |
||
| 68 | 4 | $this->assertPostGranted(); |
|
| 69 | 2 | $entity = $this->getRequestParser()->parseEntity($request, $this->getEntityClass()); |
|
| 70 | 2 | $entity = $this->postProcessPostedEntity($entity); |
|
| 71 | |||
| 72 | 2 | $errors = $this->getValidator()->validate($entity); |
|
| 73 | 2 | if ($errors->count() > 0) { |
|
| 74 | 2 | return new JsonResponse($this->parseConstraintViolations($errors), Response::HTTP_BAD_REQUEST); |
|
| 75 | } |
||
| 76 | |||
| 77 | $entity = $this->createEntity($entity); |
||
| 78 | |||
| 79 | $content = $this->getNormalizer()->normalize($entity, $this->parseIncludes($request)); |
||
| 80 | |||
| 81 | return new JsonResponse($content, Response::HTTP_CREATED); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 32 | public function getAction(Request $request, $id) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | 4 | public function putAction(Request $request, $id) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritdoc} |
||
| 121 | */ |
||
| 122 | 4 | public function deleteAction(Request $request, $id) |
|
| 123 | { |
||
| 124 | 4 | $entity = $this->fetchEntity($id); |
|
| 125 | 4 | $this->assertDeleteGranted($entity); |
|
| 126 | 2 | $this->getService()->remove($entity); |
|
| 127 | |||
| 128 | 2 | return new JsonResponse(null, Response::HTTP_NO_CONTENT); |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 6 | public function listSubresourceAction(Request $request, $id) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | 4 | public function postSubresourceAction(Request $request, $id) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 12 | public function putSubresourceAction(Request $request, $id, $subId) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | 12 | public function deleteSubresourceAction(Request $request, $id, $subId = null) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return CrudServiceInterface |
||
| 217 | */ |
||
| 218 | 54 | protected function getService(): CrudServiceInterface |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param object $entity |
||
| 246 | * |
||
| 247 | * @return object |
||
| 248 | */ |
||
| 249 | 2 | protected function postProcessPostedEntity($entity) |
|
| 250 | { |
||
| 251 | 2 | return $entity; |
|
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param object $entity |
||
| 256 | * |
||
| 257 | * @return object |
||
| 258 | */ |
||
| 259 | 2 | protected function postProcessPuttedEntity($entity) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $subresource |
||
| 266 | * @param object $parent |
||
| 267 | * @param object $entity |
||
| 268 | * |
||
| 269 | * @return object |
||
| 270 | */ |
||
| 271 | 2 | protected function postProcessSubResourcePostedEntity($subresource, $entity, $parent) |
|
| 275 | |||
| 276 | 48 | protected function fetchEntity($id) |
|
| 277 | { |
||
| 278 | 48 | $entity = $this->getService()->find($id); |
|
| 279 | 48 | if (null === $entity) { |
|
| 280 | 2 | throw new NotFoundHttpException(); |
|
| 281 | } |
||
| 282 | |||
| 283 | 48 | return $entity; |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param int $page |
||
| 288 | * @param int $perPage |
||
| 289 | * |
||
| 290 | * @return Paginator|array |
||
| 291 | */ |
||
| 292 | 6 | protected function listEntities(int $page = 1, int $perPage = 50) |
|
| 296 | |||
| 297 | protected function createEntity($entity) |
||
| 301 | |||
| 302 | 2 | protected function updateEntity($entity) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param object $entity |
||
| 309 | * @param string $property |
||
| 310 | * @param int $page |
||
| 311 | * @param int $perPage |
||
| 312 | * |
||
| 313 | * @return Paginator|array |
||
| 314 | */ |
||
| 315 | 6 | protected function listSubresource($entity, string $property, int $page = 1, int $perPage = 50) |
|
| 319 | |||
| 320 | 62 | protected function getEntityClass() |
|
| 324 | |||
| 325 | protected function getShortName() |
||
| 329 | |||
| 330 | 54 | protected function getServiceId() |
|
| 334 | |||
| 335 | 62 | protected function getCurrentRequest() |
|
| 339 | |||
| 340 | 10 | protected function assertListGranted() |
|
| 347 | |||
| 348 | 4 | protected function assertPostGranted() |
|
| 349 | { |
||
| 350 | 4 | $method = $this->getClassMetadata()->getMethod(Method::POST); |
|
| 351 | 4 | $right = $method->right; |
|
| 352 | 4 | if (null === $right) { |
|
| 353 | throw new AccessDeniedException(); |
||
| 354 | } |
||
| 355 | |||
| 356 | 4 | $this->denyAccessUnlessGranted($right->attributes); |
|
| 357 | 2 | } |
|
| 358 | |||
| 359 | 30 | protected function assertGetGranted($entity) |
|
| 366 | |||
| 367 | 4 | protected function assertPutGranted($entity) |
|
| 368 | { |
||
| 369 | 4 | $method = $this->getClassMetadata()->getMethod(Method::PUT); |
|
| 370 | 4 | if ($method !== null && null !== $right = $method->right) { |
|
| 371 | 4 | $this->assertRightGranted($entity, $right); |
|
| 374 | |||
| 375 | 4 | protected function assertDeleteGranted($entity) |
|
| 382 | |||
| 383 | 6 | protected function assertSubresourceListGranted($entity, $subresource) |
|
| 396 | |||
| 397 | 4 | protected function assertSubresourcePostGranted($entity, $subresource) |
|
| 410 | |||
| 411 | 12 | protected function assertSubresourcePutGranted($entity, $subresource) |
|
| 421 | |||
| 422 | 12 | protected function assertSubresourceDeleteGranted($entity, $subresource) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @return ClassMetadata |
||
| 435 | */ |
||
| 436 | 62 | protected function getClassMetadata() |
|
| 444 | |||
| 445 | 2 | protected function getSubResourceEntityClass($subresource) |
|
| 452 | |||
| 453 | protected function resolveSubject($entity, $propertyPath) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param object $entity |
||
| 465 | * @param Right $right |
||
| 466 | */ |
||
| 467 | 28 | protected function assertRightGranted($entity, Right $right) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @param object $parent |
||
| 480 | * @param string $subresource |
||
| 481 | * @param object $entity |
||
| 482 | * |
||
| 483 | * @return |
||
| 484 | */ |
||
| 485 | 2 | protected function createSubResource($parent, $subresource, $entity) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @return string|null |
||
| 492 | */ |
||
| 493 | 30 | protected function getSubresource() |
|
| 497 | |||
| 498 | 44 | protected function parseIncludes(Request $request) |
|
| 514 | |||
| 515 | 2 | private function parseConstraintViolations(ConstraintViolationListInterface $errors) |
|
| 529 | |||
| 530 | 12 | private function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
| 541 | |||
| 542 | 40 | protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
| 548 | |||
| 549 | /** |
||
| 550 | * @return Normalizer |
||
| 551 | */ |
||
| 552 | 44 | protected function getNormalizer() |
|
| 556 | |||
| 557 | /** |
||
| 558 | * @return ValidatorInterface |
||
| 559 | */ |
||
| 560 | 6 | protected function getValidator() |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @return RestRequestParser |
||
| 567 | */ |
||
| 568 | 6 | protected function getRequestParser() |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @return RequestStack |
||
| 575 | */ |
||
| 576 | 62 | protected function getRequestStack() |
|
| 580 | |||
| 581 | /** |
||
| 582 | * @return MetadataFactoryInterface |
||
| 583 | */ |
||
| 584 | 62 | protected function getMetadataFactory() |
|
| 588 | |||
| 589 | /** |
||
| 590 | * @return PropertyAccessorInterface |
||
| 591 | */ |
||
| 592 | protected function getPropertyAccessor() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @return AuthorizationCheckerInterface |
||
| 599 | */ |
||
| 600 | 40 | protected function getAuthorizationChecker() |
|
| 604 | } |
||
| 605 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.