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 |
||
| 47 | class RestController |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var DocumentModel |
||
| 51 | */ |
||
| 52 | private $model; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ContainerInterface service_container |
||
| 56 | */ |
||
| 57 | private $container; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Response |
||
| 61 | */ |
||
| 62 | private $response; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var RestUtilsInterface |
||
| 66 | */ |
||
| 67 | private $restUtils; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var SchemaUtils |
||
| 71 | */ |
||
| 72 | private $schemaUtils; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Router |
||
| 76 | */ |
||
| 77 | private $router; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var EngineInterface |
||
| 81 | */ |
||
| 82 | private $templating; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var JsonPatchValidator |
||
| 86 | */ |
||
| 87 | private $jsonPatchValidator; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var TokenStorage |
||
| 91 | */ |
||
| 92 | protected $tokenStorage; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param Response $response Response |
||
| 96 | * @param RestUtilsInterface $restUtils Rest utils |
||
| 97 | * @param Router $router Router |
||
| 98 | * @param EngineInterface $templating Templating |
||
| 99 | * @param ContainerInterface $container Container |
||
| 100 | * @param SchemaUtils $schemaUtils Schema utils |
||
| 101 | */ |
||
| 102 | 4 | public function __construct( |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Setter for the tokenStorage |
||
| 120 | * |
||
| 121 | * @param TokenStorage $tokenStorage The token storage |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 4 | public function setTokenStorage(TokenStorage $tokenStorage) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param JsonPatchValidator $jsonPatchValidator Service for validation json patch |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | 4 | public function setJsonPatchValidator(JsonPatchValidator $jsonPatchValidator) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get the container object |
||
| 140 | * |
||
| 141 | * @return \Symfony\Component\DependencyInjection\ContainerInterface |
||
| 142 | * |
||
| 143 | * @obsolete |
||
| 144 | */ |
||
| 145 | public function getContainer() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns a single record |
||
| 152 | * |
||
| 153 | * @param Request $request Current http request |
||
| 154 | * @param string $id ID of record |
||
| 155 | * |
||
| 156 | * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error |
||
| 157 | */ |
||
| 158 | 4 | public function getAction(Request $request, $id) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Get the response object |
||
| 174 | * |
||
| 175 | * @return \Symfony\Component\HttpFoundation\Response $response Response object |
||
| 176 | */ |
||
| 177 | 4 | public function getResponse() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Get a single record from database or throw an exception if it doesn't exist |
||
| 184 | * |
||
| 185 | * @param mixed $id Record id |
||
| 186 | * |
||
| 187 | * @throws \Graviton\ExceptionBundle\Exception\NotFoundException |
||
| 188 | * |
||
| 189 | * @return object $record Document object |
||
| 190 | */ |
||
| 191 | 4 | protected function findRecord($id) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Return the model |
||
| 206 | * |
||
| 207 | * @throws \Exception in case no model was defined. |
||
| 208 | * |
||
| 209 | * @return DocumentModel $model Model |
||
| 210 | */ |
||
| 211 | 4 | public function getModel() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Set the model class |
||
| 222 | * |
||
| 223 | * @param DocumentModel $model Model class |
||
| 224 | * |
||
| 225 | * @return self |
||
| 226 | */ |
||
| 227 | 4 | public function setModel(DocumentModel $model) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Serialize the given record and throw an exception if something went wrong |
||
| 236 | * |
||
| 237 | * @param object|object[] $result Record(s) |
||
| 238 | * |
||
| 239 | * @throws \Graviton\ExceptionBundle\Exception\SerializationException |
||
| 240 | * |
||
| 241 | * @return string $content Json content |
||
| 242 | */ |
||
| 243 | 4 | protected function serialize($result) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Get RestUtils service |
||
| 278 | * |
||
| 279 | * @return \Graviton\RestBundle\Service\RestUtils |
||
| 280 | */ |
||
| 281 | 4 | public function getRestUtils() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Returns all records |
||
| 288 | * |
||
| 289 | * @param Request $request Current http request |
||
| 290 | * |
||
| 291 | * @return \Symfony\Component\HttpFoundation\Response $response Response with result or error |
||
| 292 | */ |
||
| 293 | public function allAction(Request $request) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Writes a new Entry to the database |
||
| 326 | * |
||
| 327 | * @param Request $request Current http request |
||
| 328 | * |
||
| 329 | * @return \Symfony\Component\HttpFoundation\Response $response Result of action with data (if successful) |
||
| 330 | */ |
||
| 331 | public function postAction(Request $request) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Validates the current request on schema violations. If there are errors, |
||
| 360 | * the exception is thrown. If not, the deserialized record is returned. |
||
| 361 | * |
||
| 362 | * @param object|string $content \stdClass of the request content |
||
| 363 | * @param DocumentModel $model the model to check the schema for |
||
| 364 | * |
||
| 365 | * @return \Graviton\JsonSchemaBundle\Exception\ValidationExceptionError[] |
||
| 366 | * @throws \Exception |
||
| 367 | */ |
||
| 368 | 4 | protected function validateRequest($content, DocumentModel $model) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Deserialize the given content throw an exception if something went wrong |
||
| 379 | * |
||
| 380 | * @param string $content Request content |
||
| 381 | * @param string $documentClass Document class |
||
| 382 | * |
||
| 383 | * @throws DeserializationException |
||
| 384 | * |
||
| 385 | * @return object $record Document |
||
| 386 | */ |
||
| 387 | 2 | protected function deserialize($content, $documentClass) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get the router from the dic |
||
| 412 | * |
||
| 413 | * @return Router |
||
| 414 | */ |
||
| 415 | public function getRouter() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Update a record |
||
| 422 | * |
||
| 423 | * @param Number $id ID of record |
||
| 424 | * @param Request $request Current http request |
||
| 425 | * |
||
| 426 | * @throws MalformedInputException |
||
| 427 | * |
||
| 428 | * @return Response $response Result of action with data (if successful) |
||
| 429 | */ |
||
| 430 | 4 | public function putAction($id, Request $request) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Patch a record |
||
| 468 | * |
||
| 469 | * @param Number $id ID of record |
||
| 470 | * @param Request $request Current http request |
||
| 471 | * |
||
| 472 | * @throws MalformedInputException |
||
| 473 | * |
||
| 474 | * @return Response $response Result of action with data (if successful) |
||
| 475 | */ |
||
| 476 | public function patchAction($id, Request $request) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Deletes a record |
||
| 528 | * |
||
| 529 | * @param Number $id ID of record |
||
| 530 | * |
||
| 531 | * @return Response $response Result of the action |
||
| 532 | */ |
||
| 533 | public function deleteAction($id) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Return OPTIONS results. |
||
| 548 | * |
||
| 549 | * @param Request $request Current http request |
||
| 550 | * |
||
| 551 | * @throws SerializationException |
||
| 552 | * @return \Symfony\Component\HttpFoundation\Response $response Result of the action |
||
| 553 | */ |
||
| 554 | public function optionsAction(Request $request) |
||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * Return schema GET results. |
||
| 579 | * |
||
| 580 | * @param Request $request Current http request |
||
| 581 | * @param string $id ID of record |
||
| 582 | * |
||
| 583 | * @throws SerializationException |
||
| 584 | * @return \Symfony\Component\HttpFoundation\Response $response Result of the action |
||
| 585 | */ |
||
| 586 | public function schemaAction(Request $request, $id = null) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Renders a view. |
||
| 624 | * |
||
| 625 | * @param string $view The view name |
||
| 626 | * @param array $parameters An array of parameters to pass to the view |
||
| 627 | * @param Response $response A response instance |
||
| 628 | * |
||
| 629 | * @return Response A Response instance |
||
| 630 | */ |
||
| 631 | 4 | public function render($view, array $parameters = array(), Response $response = null) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * @param Request $request request |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | private function getRouteName(Request $request) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Security needs to be enabled to get Object. |
||
| 655 | * |
||
| 656 | * @return SecurityUser |
||
| 657 | * @throws PreconditionRequiredHttpException |
||
| 658 | */ |
||
| 659 | public function getSecurityUser() |
||
| 669 | } |
||
| 670 |
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.