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 | 12 | public function postAction(Request $request) |
|
| 67 | { |
||
| 68 | 12 | $this->assertPostGranted(); |
|
| 69 | 10 | $entity = $this->getRequestParser()->parseEntity($request, $this->getEntityClass()); |
|
| 70 | 10 | $entity = $this->postProcessPostedEntity($entity); |
|
| 71 | |||
| 72 | 10 | $errors = $this->getValidator()->validate($entity); |
|
| 73 | 10 | if ($errors->count() > 0) { |
|
| 74 | 2 | return new JsonResponse($this->parseConstraintViolations($errors), Response::HTTP_BAD_REQUEST); |
|
| 75 | } |
||
| 76 | |||
| 77 | 8 | $entity = $this->createEntity($entity); |
|
| 78 | |||
| 79 | 8 | $content = $this->getNormalizer()->normalize($entity, $this->parseIncludes($request)); |
|
| 80 | |||
| 81 | 8 | 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 | 10 | 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 | 68 | protected function getService(): CrudServiceInterface |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param object $entity |
||
| 246 | * |
||
| 247 | * @return object |
||
| 248 | */ |
||
| 249 | 10 | protected function postProcessPostedEntity($entity) |
|
| 250 | { |
||
| 251 | 10 | return $entity; |
|
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param object $entity |
||
| 256 | * |
||
| 257 | * @return object |
||
| 258 | */ |
||
| 259 | 8 | 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 | 54 | protected function fetchEntity($id) |
|
| 277 | { |
||
| 278 | 54 | $entity = $this->getService()->find($id); |
|
| 279 | 54 | if (null === $entity) { |
|
| 280 | 2 | throw new NotFoundHttpException(); |
|
| 281 | } |
||
| 282 | |||
| 283 | 54 | 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 | 8 | protected function createEntity($entity) |
|
| 298 | { |
||
| 299 | 8 | return $this->getService()->create($entity); |
|
| 300 | } |
||
| 301 | |||
| 302 | 8 | 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 | 76 | protected function getEntityClass() |
|
| 324 | |||
| 325 | protected function getShortName() |
||
| 329 | |||
| 330 | 68 | protected function getServiceId() |
|
| 334 | |||
| 335 | 76 | protected function getCurrentRequest() |
|
| 339 | |||
| 340 | 10 | protected function assertListGranted() |
|
| 347 | |||
| 348 | 12 | protected function assertPostGranted() |
|
| 349 | { |
||
| 350 | 12 | $method = $this->getClassMetadata()->getMethod(Method::POST); |
|
| 351 | 12 | if ($method !== null && null !== $right = $method->right) { |
|
| 352 | 6 | $this->denyAccessUnlessGranted($right->attributes); |
|
| 355 | |||
| 356 | 30 | protected function assertGetGranted($entity) |
|
| 363 | |||
| 364 | 10 | protected function assertPutGranted($entity) |
|
| 371 | |||
| 372 | 4 | protected function assertDeleteGranted($entity) |
|
| 379 | |||
| 380 | 6 | protected function assertSubresourceListGranted($entity, $subresource) |
|
| 393 | |||
| 394 | 4 | protected function assertSubresourcePostGranted($entity, $subresource) |
|
| 407 | |||
| 408 | 12 | protected function assertSubresourcePutGranted($entity, $subresource) |
|
| 418 | |||
| 419 | 12 | protected function assertSubresourceDeleteGranted($entity, $subresource) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @return ClassMetadata |
||
| 432 | */ |
||
| 433 | 76 | protected function getClassMetadata() |
|
| 441 | |||
| 442 | 2 | protected function getSubResourceEntityClass($subresource) |
|
| 449 | |||
| 450 | protected function resolveSubject($entity, $propertyPath) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param object $entity |
||
| 462 | * @param Right $right |
||
| 463 | */ |
||
| 464 | 28 | protected function assertRightGranted($entity, Right $right) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * @param object $parent |
||
| 477 | * @param string $subresource |
||
| 478 | * @param object $entity |
||
| 479 | * |
||
| 480 | * @return |
||
| 481 | */ |
||
| 482 | 2 | protected function createSubResource($parent, $subresource, $entity) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @return string|null |
||
| 489 | */ |
||
| 490 | 30 | protected function getSubresource() |
|
| 494 | |||
| 495 | 58 | protected function parseIncludes(Request $request) |
|
| 511 | |||
| 512 | 2 | private function parseConstraintViolations(ConstraintViolationListInterface $errors) |
|
| 526 | |||
| 527 | 12 | private function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
| 538 | |||
| 539 | 42 | protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
| 545 | |||
| 546 | /** |
||
| 547 | * @return Normalizer |
||
| 548 | */ |
||
| 549 | 58 | protected function getNormalizer() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @return ValidatorInterface |
||
| 556 | */ |
||
| 557 | 20 | protected function getValidator() |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @return RestRequestParser |
||
| 564 | */ |
||
| 565 | 20 | protected function getRequestParser() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * @return RequestStack |
||
| 572 | */ |
||
| 573 | 76 | protected function getRequestStack() |
|
| 577 | |||
| 578 | /** |
||
| 579 | * @return MetadataFactoryInterface |
||
| 580 | */ |
||
| 581 | 76 | protected function getMetadataFactory() |
|
| 585 | |||
| 586 | /** |
||
| 587 | * @return PropertyAccessorInterface |
||
| 588 | */ |
||
| 589 | protected function getPropertyAccessor() |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @return AuthorizationCheckerInterface |
||
| 596 | */ |
||
| 597 | 42 | protected function getAuthorizationChecker() |
|
| 601 | } |
||
| 602 |
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.