Complex classes like RestController 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 RestController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class RestController |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * @var DocumentModel |
||
| 54 | */ |
||
| 55 | private $model; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ContainerInterface service_container |
||
| 59 | */ |
||
| 60 | private $container; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var Response |
||
| 64 | */ |
||
| 65 | private $response; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var FormFactory |
||
| 69 | */ |
||
| 70 | private $formFactory; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var DocumentType |
||
| 74 | */ |
||
| 75 | private $formType; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var RestUtilsInterface |
||
| 79 | */ |
||
| 80 | private $restUtils; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var SchemaUtils |
||
| 84 | */ |
||
| 85 | private $schemaUtils; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var FormDataMapperInterface |
||
| 89 | */ |
||
| 90 | protected $formDataMapper; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Router |
||
| 94 | */ |
||
| 95 | private $router; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var ValidatorInterface |
||
| 99 | */ |
||
| 100 | private $validator; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var EngineInterface |
||
| 104 | */ |
||
| 105 | private $templating; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var JsonPatchValidator |
||
| 109 | */ |
||
| 110 | private $jsonPatchValidator; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var Form |
||
| 114 | */ |
||
| 115 | protected $formValidator; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var TokenStorage |
||
| 119 | */ |
||
| 120 | protected $tokenStorage; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Response $response Response |
||
| 124 | * @param RestUtilsInterface $restUtils Rest utils |
||
| 125 | * @param Router $router Router |
||
| 126 | * @param ValidatorInterface $validator Validator |
||
| 127 | * @param EngineInterface $templating Templating |
||
| 128 | * @param FormFactory $formFactory form factory |
||
| 129 | * @param DocumentType $formType generic form |
||
| 130 | * @param ContainerInterface $container Container |
||
| 131 | * @param SchemaUtils $schemaUtils Schema utils |
||
| 132 | */ |
||
| 133 | 4 | public function __construct( |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Setter for the tokenStorage |
||
| 157 | * |
||
| 158 | * @param TokenStorage $tokenStorage The token storage |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | 4 | public function setTokenStorage(TokenStorage $tokenStorage) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Set form data mapper |
||
| 168 | * |
||
| 169 | * @param FormDataMapperInterface $formDataMapper Form data mapper |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | 4 | public function setFormDataMapper(FormDataMapperInterface $formDataMapper) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param JsonPatchValidator $jsonPatchValidator Service for validation json patch |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | 4 | public function setJsonPatchValidator(JsonPatchValidator $jsonPatchValidator) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Defines the Form validator to be used. |
||
| 188 | * |
||
| 189 | * @param Form $validator Validator to be used |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | 4 | public function setFormValidator(Form $validator) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Get the container object |
||
| 200 | * |
||
| 201 | * @return \Symfony\Component\DependencyInjection\ContainerInterface |
||
| 202 | * |
||
| 203 | * @obsolete |
||
| 204 | */ |
||
| 205 | public function getContainer() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns a single record |
||
| 212 | * |
||
| 213 | * @param Request $request Current http request |
||
| 214 | * @param string $id ID of record |
||
| 215 | * |
||
| 216 | * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error |
||
| 217 | */ |
||
| 218 | 4 | public function getAction(Request $request, $id) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get the response object |
||
| 234 | * |
||
| 235 | * @return \Symfony\Component\HttpFoundation\Response $response Response object |
||
| 236 | */ |
||
| 237 | 4 | public function getResponse() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Get a single record from database or throw an exception if it doesn't exist |
||
| 244 | * |
||
| 245 | * @param mixed $id Record id |
||
| 246 | * |
||
| 247 | * @throws \Graviton\ExceptionBundle\Exception\NotFoundException |
||
| 248 | * |
||
| 249 | * @return object $record Document object |
||
| 250 | */ |
||
| 251 | 4 | protected function findRecord($id) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Return the model |
||
| 266 | * |
||
| 267 | * @throws \Exception in case no model was defined. |
||
| 268 | * |
||
| 269 | * @return DocumentModel $model Model |
||
| 270 | */ |
||
| 271 | 4 | public function getModel() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Set the model class |
||
| 282 | * |
||
| 283 | * @param DocumentModel $model Model class |
||
| 284 | * |
||
| 285 | * @return self |
||
| 286 | */ |
||
| 287 | 4 | public function setModel(DocumentModel $model) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Serialize the given record and throw an exception if something went wrong |
||
| 296 | * |
||
| 297 | * @param object|object[] $result Record(s) |
||
| 298 | * |
||
| 299 | * @throws \Graviton\ExceptionBundle\Exception\SerializationException |
||
| 300 | * |
||
| 301 | * @return string $content Json content |
||
| 302 | */ |
||
| 303 | 4 | protected function serialize($result) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get RestUtils service |
||
| 338 | * |
||
| 339 | * @return \Graviton\RestBundle\Service\RestUtils |
||
| 340 | */ |
||
| 341 | 4 | public function getRestUtils() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Returns all records |
||
| 348 | * |
||
| 349 | * @param Request $request Current http request |
||
| 350 | * |
||
| 351 | * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error |
||
| 352 | */ |
||
| 353 | public function allAction(Request $request) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Writes a new Entry to the database |
||
| 386 | * |
||
| 387 | * @param Request $request Current http request |
||
| 388 | * |
||
| 389 | * @return \Symfony\Component\HttpFoundation\Response $response Result of action with data (if successful) |
||
| 390 | */ |
||
| 391 | public function postAction(Request $request) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Validates the current request on schema violations. If there are errors, |
||
| 420 | * the exception is thrown. If not, the deserialized record is returned. |
||
| 421 | * |
||
| 422 | * @param object $content \stdClass of the request content |
||
| 423 | * @param DocumentModel $model the model to check the schema for |
||
| 424 | * |
||
| 425 | * @return \Graviton\JsonSchemaBundle\Exception\ValidationExceptionError[] |
||
| 426 | * @throws \Exception |
||
| 427 | */ |
||
| 428 | 4 | protected function validateRequest($content, DocumentModel $model) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Deserialize the given content throw an exception if something went wrong |
||
| 439 | * |
||
| 440 | * @param string $content Request content |
||
| 441 | * @param string $documentClass Document class |
||
| 442 | * |
||
| 443 | * @throws DeserializationException |
||
| 444 | * |
||
| 445 | * @return object $record Document |
||
| 446 | */ |
||
| 447 | 2 | protected function deserialize($content, $documentClass) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get the router from the dic |
||
| 472 | * |
||
| 473 | * @return Router |
||
| 474 | */ |
||
| 475 | public function getRouter() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Update a record |
||
| 482 | * |
||
| 483 | * @param Number $id ID of record |
||
| 484 | * @param Request $request Current http request |
||
| 485 | * |
||
| 486 | * @throws MalformedInputException |
||
| 487 | * |
||
| 488 | * @return Response $response Result of action with data (if successful) |
||
| 489 | */ |
||
| 490 | 4 | public function putAction($id, Request $request) |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Patch a record |
||
| 528 | * |
||
| 529 | * @param Number $id ID of record |
||
| 530 | * @param Request $request Current http request |
||
| 531 | * |
||
| 532 | * @throws MalformedInputException |
||
| 533 | * |
||
| 534 | * @return Response $response Result of action with data (if successful) |
||
| 535 | */ |
||
| 536 | public function patchAction($id, Request $request) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Deletes a record |
||
| 588 | * |
||
| 589 | * @param Number $id ID of record |
||
| 590 | * |
||
| 591 | * @return Response $response Result of the action |
||
| 592 | */ |
||
| 593 | public function deleteAction($id) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Return OPTIONS results. |
||
| 608 | * |
||
| 609 | * @param Request $request Current http request |
||
| 610 | * |
||
| 611 | * @throws SerializationException |
||
| 612 | * @return \Symfony\Component\HttpFoundation\Response $response Result of the action |
||
| 613 | */ |
||
| 614 | public function optionsAction(Request $request) |
||
| 635 | |||
| 636 | |||
| 637 | /** |
||
| 638 | * Return schema GET results. |
||
| 639 | * |
||
| 640 | * @param Request $request Current http request |
||
| 641 | * @param string $id ID of record |
||
| 642 | * |
||
| 643 | * @throws SerializationException |
||
| 644 | * @return \Symfony\Component\HttpFoundation\Response $response Result of the action |
||
| 645 | */ |
||
| 646 | public function schemaAction(Request $request, $id = null) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Get the validator |
||
| 684 | * |
||
| 685 | * @return ValidatorInterface |
||
| 686 | */ |
||
| 687 | public function getValidator() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Renders a view. |
||
| 694 | * |
||
| 695 | * @param string $view The view name |
||
| 696 | * @param array $parameters An array of parameters to pass to the view |
||
| 697 | * @param Response $response A response instance |
||
| 698 | * |
||
| 699 | * @return Response A Response instance |
||
| 700 | */ |
||
| 701 | 4 | public function render($view, array $parameters = array(), Response $response = null) |
|
| 705 | |||
| 706 | /** |
||
| 707 | * @param Request $request request |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | private function getRouteName(Request $request) |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Security needs to be enabled to get Object. |
||
| 725 | * |
||
| 726 | * @return SecurityUser |
||
| 727 | * @throws PreconditionRequiredHttpException |
||
| 728 | */ |
||
| 729 | public function getSecurityUser() |
||
| 739 | } |
||
| 740 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.