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 | * {@inheritdoc} |
||
32 | */ |
||
33 | 10 | public function listAction(Request $request) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 14 | public function postAction(Request $request) |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 32 | public function getAction(Request $request, $id) |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 12 | public function putAction(Request $request, $id) |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 4 | public function deleteAction(Request $request, $id) |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 6 | public function listSubresourceAction(Request $request, $id, string $subresource) |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 4 | public function postSubresourceAction(Request $request, $id, string $subresource) |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 12 | public function putSubresourceAction(Request $request, $id, string $subresource, $subId) |
|
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | 12 | public function deleteSubresourceAction(Request $request, $id, string $subresource, $subId = null) |
|
207 | |||
208 | /** |
||
209 | * @param object $entity |
||
210 | * |
||
211 | * @return object |
||
212 | */ |
||
213 | 12 | protected function postProcessPostedEntity($entity) |
|
217 | |||
218 | /** |
||
219 | * @param object $entity |
||
220 | * |
||
221 | * @return object |
||
222 | */ |
||
223 | 10 | protected function postProcessPuttedEntity($entity) |
|
227 | |||
228 | /** |
||
229 | * @param object $parent |
||
230 | * @param string $subresource |
||
231 | * @param object $entity |
||
232 | * |
||
233 | * @return object |
||
234 | */ |
||
235 | 2 | protected function postProcessSubResourcePostedEntity($parent, $subresource, $entity) |
|
239 | |||
240 | 80 | protected function getEntityClass() |
|
244 | |||
245 | 2 | protected function getSubResourceEntityClass($subresource) |
|
252 | |||
253 | 80 | protected function getCurrentRequest() |
|
257 | |||
258 | 70 | protected function assertMethodGranted(string $methodName, $entity = null) |
|
265 | |||
266 | /** |
||
267 | * @param string $methodName |
||
268 | * @param object $entity |
||
269 | * @param string $subresource |
||
270 | */ |
||
271 | 30 | protected function assertSubResourceMethodGranted($methodName, $entity, string $subresource): void |
|
281 | |||
282 | /** |
||
283 | * @return ClassMetadata |
||
284 | */ |
||
285 | 80 | protected function getClassMetadata() |
|
293 | |||
294 | protected function resolveSubject($entity, $propertyPath) |
||
303 | |||
304 | /** |
||
305 | * @param Right $right |
||
306 | * @param object $entity |
||
307 | */ |
||
308 | 46 | protected function assertRightGranted(Right $right, $entity = null) |
|
318 | |||
319 | 62 | protected function parseIncludes(Request $request) |
|
335 | |||
336 | 46 | protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.') |
|
347 | |||
348 | 2 | protected function parseConstraintViolations(ConstraintViolationListInterface $errors) |
|
362 | |||
363 | 12 | protected function addPaginationHeaders(Response $response, int $page, int $perPage, int $total) |
|
374 | |||
375 | /** |
||
376 | * @param int $page |
||
377 | * @param int $perPage |
||
378 | * |
||
379 | * @return Paginator|array |
||
380 | */ |
||
381 | abstract protected function listEntities(int $page = 1, int $perPage = 50); |
||
382 | |||
383 | /** |
||
384 | * @param int|string $id |
||
385 | * |
||
386 | * @return object |
||
387 | * |
||
388 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
389 | */ |
||
390 | abstract protected function fetchEntity($id); |
||
391 | |||
392 | /** |
||
393 | * @param object $entity |
||
394 | * |
||
395 | * @return object |
||
396 | */ |
||
397 | abstract protected function createEntity($entity); |
||
398 | |||
399 | /** |
||
400 | * @param object $entity |
||
401 | * |
||
402 | * @return object |
||
403 | * |
||
404 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
405 | */ |
||
406 | abstract protected function updateEntity($entity); |
||
407 | |||
408 | /** |
||
409 | * @param $entity |
||
410 | * |
||
411 | * @throws NotFoundHttpException Thrown if entity with the given id could not be found. |
||
412 | */ |
||
413 | abstract protected function removeEntity($entity); |
||
414 | |||
415 | /** |
||
416 | * @param object $entity |
||
417 | * @param string $subresource |
||
418 | * @param int $page |
||
419 | * @param int $perPage |
||
420 | * |
||
421 | * @return Paginator|array |
||
422 | */ |
||
423 | abstract protected function listSubresource($entity, string $subresource, int $page = 1, int $perPage = 50); |
||
424 | |||
425 | /** |
||
426 | * @param object $parent |
||
427 | * @param string $subresource |
||
428 | * |
||
429 | * @return object |
||
430 | */ |
||
431 | abstract protected function createAssociation($parent, string $subresource, $entity); |
||
432 | |||
433 | /** |
||
434 | * @param object $parent |
||
435 | * @param string $subresource |
||
436 | * @param int|string $subId |
||
437 | * |
||
438 | * @return object |
||
439 | */ |
||
440 | abstract protected function addAssociation($parent, string $subresource, $subId); |
||
441 | |||
442 | /** |
||
443 | * @param object $parent |
||
444 | * @param string $subresource |
||
445 | * @param int|string|null $subId |
||
446 | * |
||
447 | * @return mixed |
||
448 | */ |
||
449 | abstract protected function removeAssociation($parent, string $subresource, $subId = null); |
||
450 | |||
451 | /** |
||
452 | * @return Normalizer |
||
453 | */ |
||
454 | abstract protected function getNormalizer(); |
||
455 | |||
456 | /** |
||
457 | * @return ValidatorInterface |
||
458 | */ |
||
459 | abstract protected function getValidator(); |
||
460 | |||
461 | /** |
||
462 | * @return RestRequestParser |
||
463 | */ |
||
464 | abstract protected function getRequestParser(); |
||
465 | |||
466 | /** |
||
467 | * @return RequestStack |
||
468 | */ |
||
469 | abstract protected function getRequestStack(); |
||
470 | |||
471 | /** |
||
472 | * @return MetadataFactoryInterface |
||
473 | */ |
||
474 | abstract protected function getMetadataFactory(); |
||
475 | |||
476 | /** |
||
477 | * @return PropertyAccessorInterface |
||
478 | */ |
||
479 | abstract protected function getPropertyAccessor(); |
||
480 | |||
481 | /** |
||
482 | * @return AuthorizationCheckerInterface |
||
483 | */ |
||
484 | abstract protected function getAuthorizationChecker(); |
||
485 | } |
||
486 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.