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 | 8 | public function listAction(Request $request) |
|
49 | |||
50 | 2 | public function postAction(Request $request) |
|
70 | |||
71 | 10 | public function getAction(Request $request, $id) |
|
81 | |||
82 | 6 | public function putAction(Request $request, $id) |
|
103 | |||
104 | 2 | public function deleteAction(Request $request, $id) |
|
112 | |||
113 | 6 | public function listSubresourceAction(Request $request, $id) |
|
146 | |||
147 | 4 | public function postSubresourceAction(Request $request, $id) |
|
148 | { |
||
149 | 4 | $subresource = $this->getSubresource(); |
|
150 | 4 | $parent = $this->fetchEntity($id); |
|
151 | 4 | $this->assertSubresourcePostGranted($parent, $subresource); |
|
152 | 2 | $entity = $this->parseRequest($request, null, $this->getSubResourceEntityClass($subresource)); |
|
153 | 2 | $entity = $this->postProcessSubResourcePostedEntity($subresource, $entity, $parent); |
|
154 | |||
155 | /** @var ValidatorInterface $validator */ |
||
156 | 2 | $validator = $this->get('validator'); |
|
157 | 2 | $errors = $validator->validate($entity); |
|
158 | |||
159 | 2 | if ($errors->count() > 0) { |
|
160 | return new JsonResponse($this->parseConstraintViolations($errors), Response::HTTP_BAD_REQUEST); |
||
161 | } |
||
162 | |||
163 | 2 | $entity = $this->createSubResource($parent, $subresource, $entity); |
|
164 | |||
165 | 2 | $normalizer = $this->get('ddr_rest.normalizer'); |
|
166 | 2 | $content = $normalizer->normalize($entity, ['details']); |
|
167 | |||
168 | 2 | return new JsonResponse($content, Response::HTTP_CREATED); |
|
169 | } |
||
170 | |||
171 | 2 | public function putSubresourceAction(Request $request, $id, $subId) |
|
180 | |||
181 | 2 | public function deleteSubresourceAction(Request $request, $id, $subId) |
|
190 | |||
191 | /** |
||
192 | * @return CrudServiceInterface |
||
193 | */ |
||
194 | 34 | protected function getService(): CrudServiceInterface |
|
219 | |||
220 | 4 | 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 | 2 | protected function postProcessPuttedEntity($entity) |
|
244 | |||
245 | /** |
||
246 | * @param string $subresource |
||
247 | * @param object $parent |
||
248 | * @param object $entity |
||
249 | * |
||
250 | * @return object |
||
251 | */ |
||
252 | 2 | protected function postProcessSubResourcePostedEntity($subresource, $entity, $parent) |
|
2 ignored issues
–
show
|
|||
253 | { |
||
254 | 2 | return $entity; |
|
255 | } |
||
256 | |||
257 | 28 | protected function fetchEntity($id) |
|
266 | |||
267 | /** |
||
268 | * @param int $page |
||
269 | * @param int $perPage |
||
270 | * |
||
271 | * @return Paginator|array |
||
272 | */ |
||
273 | 6 | protected function listEntities(int $page = 1, int $perPage = 50) |
|
277 | |||
278 | protected function createEntity($entity) |
||
282 | |||
283 | 2 | 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 | 6 | protected function listSubresource($entity, string $property, int $page = 1, int $perPage = 50) |
|
300 | |||
301 | 38 | protected function getEntityClass() |
|
305 | |||
306 | protected function getShortName() |
||
310 | |||
311 | 34 | protected function getServiceId() |
|
315 | |||
316 | 38 | protected function getCurrentRequest() |
|
320 | |||
321 | 8 | protected function assertListGranted() |
|
331 | |||
332 | 2 | protected function assertPostGranted() |
|
342 | |||
343 | 10 | protected function assertGetGranted($entity) |
|
353 | |||
354 | 6 | protected function assertPutGranted($entity) |
|
364 | |||
365 | 2 | protected function assertDeleteGranted($entity) |
|
375 | |||
376 | 6 | protected function assertSubresourceListGranted($entity, $subresource) |
|
388 | |||
389 | 4 | protected function assertSubresourcePostGranted($entity, $subresource) |
|
390 | { |
||
391 | 4 | $classMetadata = $this->getClassMetadata(); |
|
392 | /** @var PropertyMetadata $propertyMetadata */ |
||
393 | 4 | $propertyMetadata = $classMetadata->propertyMetadata[$subresource]; |
|
394 | 4 | $right = $propertyMetadata->getSubResourcePostRight(); |
|
395 | 4 | if (null === $right) { |
|
396 | throw $this->createAccessDeniedException(); |
||
397 | } |
||
398 | |||
399 | 4 | $this->assertRightGranted($entity, $right); |
|
400 | 2 | } |
|
401 | |||
402 | 2 | protected function assertSubresourcePutGranted($entity, $subresource) |
|
414 | |||
415 | 2 | protected function assertSubresourceDeleteGranted($entity, $subresource) |
|
427 | |||
428 | /** |
||
429 | * @return ClassMetadata |
||
430 | */ |
||
431 | 38 | protected function getClassMetadata() |
|
439 | |||
440 | 2 | protected function getSubResourceEntityClass($subresource) |
|
441 | { |
||
442 | /** @var PropertyMetadata $propertyMetadata */ |
||
443 | 2 | $propertyMetadata = $this->getClassMetadata()->propertyMetadata[$subresource]; |
|
444 | |||
445 | 2 | return $propertyMetadata->getTargetClass(); |
|
446 | } |
||
447 | |||
448 | protected function resolveSubject($entity, $propertyPath) |
||
457 | |||
458 | /** |
||
459 | * @param object $entity |
||
460 | * @param Right $right |
||
461 | */ |
||
462 | 18 | 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 | 2 | protected function createSubResource($parent, $subresource, $entity) |
|
492 | |||
493 | /** |
||
494 | * @return string|null |
||
495 | */ |
||
496 | 10 | protected function getSubresource() |
|
500 | |||
501 | 20 | protected function parseIncludes(Request $request, array $defaultIncludes = []) |
|
510 | |||
511 | private function parseConstraintViolations(ConstraintViolationListInterface $errors) |
||
525 | |||
526 | 12 | 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: