Complex classes like RestUtils 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 RestUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | final class RestUtils implements RestUtilsInterface |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var ContainerInterface |
||
| 42 | */ |
||
| 43 | private $container; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Serializer |
||
| 47 | */ |
||
| 48 | private $serializer; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var null|SerializationContext |
||
| 52 | */ |
||
| 53 | private $serializerContext; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Router |
||
| 57 | */ |
||
| 58 | private $router; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var LoggerInterface |
||
| 62 | */ |
||
| 63 | private $logger; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var SchemaUtils |
||
| 67 | */ |
||
| 68 | private $schemaUtils; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Validator |
||
| 72 | */ |
||
| 73 | private $schemaValidator; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var CacheProvider |
||
| 77 | */ |
||
| 78 | private $cacheProvider; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param ContainerInterface $container container |
||
| 82 | * @param Router $router router |
||
| 83 | * @param Serializer $serializer serializer |
||
| 84 | * @param LoggerInterface $logger PSR logger (e.g. Monolog) |
||
| 85 | * @param SerializationContext $serializerContext context for serializer |
||
| 86 | * @param SchemaUtils $schemaUtils schema utils |
||
| 87 | * @param Validator $schemaValidator schema validator |
||
| 88 | * @param CacheProvider $cacheProvider Cache service |
||
| 89 | */ |
||
| 90 | 4 | public function __construct( |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Builds a map of baseroutes (controllers) to its relevant route to the actions. |
||
| 112 | * ignores schema stuff. |
||
| 113 | * |
||
| 114 | * @return array grouped array of basenames and actions.. |
||
| 115 | */ |
||
| 116 | public function getServiceRoutingMap() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Public function to serialize stuff according to the serializer rules. |
||
| 144 | * |
||
| 145 | * @param object $content Any content to serialize |
||
| 146 | * @param string $format Which format to serialize into |
||
| 147 | * |
||
| 148 | * @throws \Exception |
||
| 149 | * |
||
| 150 | * @return string $content Json content |
||
| 151 | */ |
||
| 152 | public function serializeContent($content, $format = 'json') |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Deserialize the given content throw an exception if something went wrong |
||
| 174 | * |
||
| 175 | * @param string $content Request content |
||
| 176 | * @param string $documentClass Document class |
||
| 177 | * @param string $format Which format to deserialize from |
||
| 178 | * |
||
| 179 | * @throws \Exception |
||
| 180 | * |
||
| 181 | * @return object|array|integer|double|string|boolean |
||
| 182 | */ |
||
| 183 | public function deserializeContent($content, $documentClass, $format = 'json') |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Validates content with the given schema, returning an array of errors. |
||
| 196 | * If all is good, you will receive an empty array. |
||
| 197 | * |
||
| 198 | * @param object $content \stdClass of the request content |
||
| 199 | * @param DocumentModel $model the model to check the schema for |
||
| 200 | * |
||
| 201 | * @return \Graviton\JsonSchemaBundle\Exception\ValidationExceptionError[] |
||
| 202 | * @throws \Exception |
||
| 203 | */ |
||
| 204 | public function validateContent($content, DocumentModel $model) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * validate raw json input |
||
| 218 | * |
||
| 219 | * @param Request $request request |
||
| 220 | * @param Response $response response |
||
| 221 | * @param DocumentModel $model model |
||
| 222 | * @param string $content Alternative request content. |
||
| 223 | * |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function checkJsonRequest(Request $request, Response $response, DocumentModel $model, $content = '') |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Validate JSON patch for any object |
||
| 279 | * |
||
| 280 | * @param array $jsonPatch json patch as array |
||
| 281 | * |
||
| 282 | * @throws InvalidJsonPatchException |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function checkJsonPatchRequest(array $jsonPatch) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Used for backwards compatibility to PHP 5.4 |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | private function getLastJsonErrorMessage() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the serializer |
||
| 315 | * |
||
| 316 | * @return Serializer |
||
| 317 | */ |
||
| 318 | public function getSerializer() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the serializer context |
||
| 325 | * |
||
| 326 | * @return SerializationContext |
||
| 327 | */ |
||
| 328 | public function getSerializerContext() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * It has been deemed that we search for OPTION routes in order to detect our |
||
| 335 | * service routes and then derive the rest from them. |
||
| 336 | * |
||
| 337 | * @return array An array with option routes |
||
| 338 | */ |
||
| 339 | public function getOptionRoutes() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Based on $baseName, this function returns all routes that match this basename.. |
||
| 368 | * So if you pass graviton.cont.action; it will return all route names that start with the same. |
||
| 369 | * In our routing naming schema, this means all the routes from the same controller. |
||
| 370 | * |
||
| 371 | * @param string $baseName basename |
||
| 372 | * |
||
| 373 | * @return array array with matching routes |
||
| 374 | */ |
||
| 375 | public function getRoutesByBasename($baseName) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Gets the Model assigned to the RestController |
||
| 394 | * |
||
| 395 | * @param Route $route Route |
||
| 396 | * |
||
| 397 | * @return bool|object The model or false |
||
|
|
|||
| 398 | * @throws \Exception |
||
| 399 | */ |
||
| 400 | public function getModelFromRoute(Route $route) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Gets the controller from a Route |
||
| 414 | * |
||
| 415 | * @param Route $route Route |
||
| 416 | * |
||
| 417 | * @return bool|object The controller or false |
||
| 418 | */ |
||
| 419 | public function getControllerFromRoute(Route $route) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param Request $request request |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function getRouteName(Request $request) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Serialize the given record and throw an exception if something went wrong |
||
| 450 | * |
||
| 451 | * @param object|object[] $result Record(s) |
||
| 452 | * |
||
| 453 | * @throws \Graviton\ExceptionBundle\Exception\SerializationException |
||
| 454 | * |
||
| 455 | * @return string $content Json content |
||
| 456 | */ |
||
| 457 | public function serialize($result) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Deserialize the given content throw an exception if something went wrong |
||
| 481 | * |
||
| 482 | * @param string $content Request content |
||
| 483 | * @param string $documentClass Document class |
||
| 484 | * |
||
| 485 | * @throws DeserializationException |
||
| 486 | * |
||
| 487 | * @return object $record Document |
||
| 488 | */ |
||
| 489 | public function deserialize($content, $documentClass) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Validates the current request on schema violations. If there are errors, |
||
| 505 | * the exception is thrown. If not, the deserialized record is returned. |
||
| 506 | * |
||
| 507 | * @param object|string $content \stdClass of the request content |
||
| 508 | * @param DocumentModel $model the model to check the schema for |
||
| 509 | * |
||
| 510 | * @return ValidationExceptionError|Object |
||
| 511 | * @throws \Exception |
||
| 512 | */ |
||
| 513 | public function validateRequest($content, DocumentModel $model) |
||
| 521 | } |
||
| 522 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.