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 |
||
| 33 | final class RestUtils implements RestUtilsInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var ContainerInterface |
||
| 37 | */ |
||
| 38 | private $container; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Serializer |
||
| 42 | */ |
||
| 43 | private $serializer; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var null|SerializationContext |
||
| 47 | */ |
||
| 48 | private $serializerContext; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Router |
||
| 52 | */ |
||
| 53 | private $router; |
||
| 54 | |||
| 55 | /** |
||
| 56 | 2 | * @var LoggerInterface |
|
| 57 | */ |
||
| 58 | private $logger; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var SchemaUtils |
||
| 62 | */ |
||
| 63 | 2 | private $schemaUtils; |
|
| 64 | 2 | ||
| 65 | 2 | /** |
|
| 66 | 2 | * @var Validator |
|
| 67 | 2 | */ |
|
| 68 | 2 | private $schemaValidator; |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param ContainerInterface $container container |
||
| 72 | * @param Router $router router |
||
| 73 | * @param Serializer $serializer serializer |
||
| 74 | * @param LoggerInterface $logger PSR logger (e.g. Monolog) |
||
| 75 | * @param SerializationContext $serializerContext context for serializer |
||
| 76 | * @param SchemaUtils $schemaUtils schema utils |
||
| 77 | * @param Validator $schemaValidator schema validator |
||
| 78 | */ |
||
| 79 | public function __construct( |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Builds a map of baseroutes (controllers) to its relevant route to the actions. |
||
| 99 | * ignores schema stuff. |
||
| 100 | * |
||
| 101 | * @return array grouped array of basenames and actions.. |
||
| 102 | */ |
||
| 103 | public function getServiceRoutingMap() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Public function to serialize stuff according to the serializer rules. |
||
| 128 | * |
||
| 129 | * @param object $content Any content to serialize |
||
| 130 | * @param string $format Which format to serialize into |
||
| 131 | * |
||
| 132 | * @throws \Exception |
||
| 133 | * |
||
| 134 | * @return string $content Json content |
||
| 135 | */ |
||
| 136 | public function serializeContent($content, $format = 'json') |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Deserialize the given content throw an exception if something went wrong |
||
| 158 | * |
||
| 159 | * @param string $content Request content |
||
| 160 | * @param string $documentClass Document class |
||
| 161 | * @param string $format Which format to deserialize from |
||
| 162 | * |
||
| 163 | * @throws \Exception |
||
| 164 | * |
||
| 165 | * @return object|array|integer|double|string|boolean |
||
| 166 | */ |
||
| 167 | public function deserializeContent($content, $documentClass, $format = 'json') |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Validates content with the given schema, returning an array of errors. |
||
| 180 | * If all is good, you will receive an empty array. |
||
| 181 | * |
||
| 182 | * @param object $content \stdClass of the request content |
||
| 183 | * @param DocumentModel $model the model to check the schema for |
||
| 184 | * |
||
| 185 | * @return \Graviton\JsonSchemaBundle\Exception\ValidationExceptionError[] |
||
| 186 | * @throws \Exception |
||
| 187 | */ |
||
| 188 | public function validateContent($content, DocumentModel $model) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * validate raw json input |
||
| 199 | * |
||
| 200 | * @param Request $request request |
||
| 201 | * @param Response $response response |
||
| 202 | * @param DocumentModel $model model |
||
| 203 | * @param string $content Alternative request content. |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function checkJsonRequest(Request $request, Response $response, DocumentModel $model, $content = '') |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Validate JSON patch for any object |
||
| 260 | * |
||
| 261 | * @param array $jsonPatch json patch as array |
||
| 262 | * |
||
| 263 | * @throws InvalidJsonPatchException |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function checkJsonPatchRequest(array $jsonPatch) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Used for backwards compatibility to PHP 5.4 |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | private function getLastJsonErrorMessage() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the serializer |
||
| 296 | * |
||
| 297 | * @return Serializer |
||
| 298 | */ |
||
| 299 | public function getSerializer() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the serializer context |
||
| 306 | * |
||
| 307 | * @return SerializationContext |
||
| 308 | */ |
||
| 309 | public function getSerializerContext() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * It has been deemed that we search for OPTION routes in order to detect our |
||
| 316 | * service routes and then derive the rest from them. |
||
| 317 | * |
||
| 318 | * @return array An array with option routes |
||
| 319 | */ |
||
| 320 | public function getOptionRoutes() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Based on $baseName, this function returns all routes that match this basename.. |
||
| 347 | * So if you pass graviton.cont.action; it will return all route names that start with the same. |
||
| 348 | * In our routing naming schema, this means all the routes from the same controller. |
||
| 349 | * |
||
| 350 | * @param string $baseName basename |
||
| 351 | * |
||
| 352 | * @return array array with matching routes |
||
| 353 | */ |
||
| 354 | public function getRoutesByBasename($baseName) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Gets the Model assigned to the RestController |
||
| 370 | * |
||
| 371 | * @param Route $route Route |
||
| 372 | * |
||
| 373 | * @return bool|object The model or false |
||
|
|
|||
| 374 | * @throws \Exception |
||
| 375 | */ |
||
| 376 | public function getModelFromRoute(Route $route) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Gets the controller from a Route |
||
| 390 | * |
||
| 391 | * @param Route $route Route |
||
| 392 | * |
||
| 393 | * @return bool|object The controller or false |
||
| 394 | */ |
||
| 395 | public function getControllerFromRoute(Route $route) |
||
| 406 | } |
||
| 407 |
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.