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 |
||
| 21 | class RestResourceController extends Controller |
||
| 22 | { |
||
| 23 | 4 | public function listAction(Request $request) |
|
| 49 | |||
| 50 | 1 | public function postAction(Request $request) |
|
| 70 | |||
| 71 | 5 | public function getAction(Request $request, $id) |
|
| 81 | |||
| 82 | 3 | public function putAction(Request $request, $id) |
|
| 103 | |||
| 104 | 1 | public function deleteAction(Request $request, $id) |
|
| 112 | |||
| 113 | 3 | public function listSubresourceAction(Request $request, $id) |
|
| 146 | |||
| 147 | 2 | public function postSubresourceAction(Request $request, $id) |
|
| 148 | { |
||
| 149 | 2 | $subresource = $this->getSubresource(); |
|
| 150 | 2 | $parent = $this->fetchEntity($id); |
|
| 151 | 2 | $this->assertSubresourcePostGranted($parent, $subresource); |
|
| 152 | 1 | $entity = $this->parseRequest($request, null, $this->getSubResourceEntityClass($subresource)); |
|
| 153 | 1 | $entity = $this->postProcessSubResourcePostedEntity($subresource, $entity, $parent); |
|
| 154 | |||
| 155 | /** @var ValidatorInterface $validator */ |
||
| 156 | 1 | $validator = $this->get('validator'); |
|
| 157 | 1 | $errors = $validator->validate($entity); |
|
| 158 | |||
| 159 | 1 | if ($errors->count() > 0) { |
|
| 160 | return new JsonResponse($this->parseConstraintViolations($errors), Response::HTTP_BAD_REQUEST); |
||
| 161 | } |
||
| 162 | |||
| 163 | 1 | $entity = $this->createSubResource($parent, $subresource, $entity); |
|
| 164 | |||
| 165 | 1 | $normalizer = $this->get('ddr_rest.normalizer'); |
|
| 166 | 1 | $content = $normalizer->normalize($entity, ['details']); |
|
| 167 | |||
| 168 | 1 | return new JsonResponse($content, Response::HTTP_CREATED); |
|
| 169 | } |
||
| 170 | |||
| 171 | 1 | public function putSubresourceAction(Request $request, $id, $subId) |
|
| 180 | |||
| 181 | 1 | public function deleteSubresourceAction(Request $request, $id, $subId) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return CrudServiceInterface |
||
| 193 | */ |
||
| 194 | 17 | protected function getService(): CrudServiceInterface |
|
| 219 | |||
| 220 | 2 | protected function parseRequest(Request $request, $entity = null, $entityClass = null) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param object $entity |
||
| 227 | * |
||
| 228 | * @return object |
||
| 229 | */ |
||
| 230 | protected function postProcessPostedEntity($entity) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param object $entity |
||
| 237 | * |
||
| 238 | * @return object |
||
| 239 | */ |
||
| 240 | 1 | protected function postProcessPuttedEntity($entity) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $subresource |
||
| 247 | * @param object $parent |
||
| 248 | * @param object $entity |
||
| 249 | * |
||
| 250 | * @return object |
||
| 251 | */ |
||
| 252 | 1 | protected function postProcessSubResourcePostedEntity($subresource, $entity, $parent) |
|
|
2 ignored issues
–
show
|
|||
| 253 | { |
||
| 254 | 1 | return $entity; |
|
| 255 | } |
||
| 256 | |||
| 257 | 14 | protected function fetchEntity($id) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param int $page |
||
| 269 | * @param int $perPage |
||
| 270 | * |
||
| 271 | * @return Paginator|array |
||
| 272 | */ |
||
| 273 | 3 | protected function listEntities(int $page = 1, int $perPage = 50) |
|
| 277 | |||
| 278 | protected function createEntity($entity) |
||
| 282 | |||
| 283 | 1 | protected function updateEntity($entity) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param object $entity |
||
| 290 | * @param string $property |
||
| 291 | * @param int $page |
||
| 292 | * @param int $perPage |
||
| 293 | * |
||
| 294 | * @return Paginator|array |
||
| 295 | */ |
||
| 296 | 3 | protected function listSubresource($entity, string $property, int $page = 1, int $perPage = 50) |
|
| 300 | |||
| 301 | 19 | protected function getEntityClass() |
|
| 305 | |||
| 306 | protected function getShortName() |
||
| 310 | |||
| 311 | 17 | protected function getServiceId() |
|
| 315 | |||
| 316 | 19 | protected function getCurrentRequest() |
|
| 320 | |||
| 321 | 4 | protected function assertListGranted() |
|
| 331 | |||
| 332 | 1 | protected function assertPostGranted() |
|
| 342 | |||
| 343 | 5 | protected function assertGetGranted($entity) |
|
| 353 | |||
| 354 | 3 | protected function assertPutGranted($entity) |
|
| 364 | |||
| 365 | 1 | protected function assertDeleteGranted($entity) |
|
| 375 | |||
| 376 | 3 | protected function assertSubresourceListGranted($entity, $subresource) |
|
| 388 | |||
| 389 | 2 | protected function assertSubresourcePostGranted($entity, $subresource) |
|
| 390 | { |
||
| 391 | 2 | $classMetadata = $this->getClassMetadata(); |
|
| 392 | /** @var PropertyMetadata $propertyMetadata */ |
||
| 393 | 2 | $propertyMetadata = $classMetadata->propertyMetadata[$subresource]; |
|
| 394 | 2 | $right = $propertyMetadata->getSubResourcePostRight(); |
|
| 395 | 2 | if (null === $right) { |
|
| 396 | throw $this->createAccessDeniedException(); |
||
| 397 | } |
||
| 398 | |||
| 399 | 2 | $this->assertRightGranted($entity, $right); |
|
| 400 | 1 | } |
|
| 401 | |||
| 402 | 1 | protected function assertSubresourcePutGranted($entity, $subresource) |
|
| 414 | |||
| 415 | 1 | protected function assertSubresourceDeleteGranted($entity, $subresource) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @return ClassMetadata |
||
| 430 | */ |
||
| 431 | 19 | protected function getClassMetadata() |
|
| 439 | |||
| 440 | 1 | protected function getSubResourceEntityClass($subresource) |
|
| 441 | { |
||
| 442 | /** @var PropertyMetadata $propertyMetadata */ |
||
| 443 | 1 | $propertyMetadata = $this->getClassMetadata()->propertyMetadata[$subresource]; |
|
| 444 | |||
| 445 | 1 | return $propertyMetadata->getTargetClass(); |
|
| 446 | } |
||
| 447 | |||
| 448 | protected function resolveSubject($entity, $propertyPath) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param object $entity |
||
| 460 | * @param Right $right |
||
| 461 | */ |
||
| 462 | 9 | protected function assertRightGranted($entity, Right $right) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * @return string[] |
||
| 475 | */ |
||
| 476 | protected function getSubresourceSerializationGroups($subresource) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param object $parent |
||
| 483 | * @param string $subresource |
||
| 484 | * @param object $entity |
||
| 485 | * |
||
| 486 | * @return |
||
| 487 | */ |
||
| 488 | 1 | protected function createSubResource($parent, $subresource, $entity) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * @return string|null |
||
| 495 | */ |
||
| 496 | 5 | protected function getSubresource() |
|
| 500 | |||
| 501 | 10 | protected function parseIncludes(Request $request, array $defaultIncludes = []) |
|
| 510 | |||
| 511 | private function parseConstraintViolations(ConstraintViolationListInterface $errors) |
||
| 525 | |||
| 526 | 6 | private function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
| 537 | } |
||
| 538 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: