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 |
||
37 | final class RestUtils implements RestUtilsInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var ContainerInterface |
||
41 | */ |
||
42 | private $container; |
||
43 | |||
44 | /** |
||
45 | * @var Serializer |
||
46 | */ |
||
47 | private $serializer; |
||
48 | |||
49 | /** |
||
50 | * @var Router |
||
51 | */ |
||
52 | private $router; |
||
53 | |||
54 | /** |
||
55 | * @var LoggerInterface |
||
56 | */ |
||
57 | private $logger; |
||
58 | |||
59 | /** |
||
60 | * @var SchemaUtils |
||
61 | */ |
||
62 | private $schemaUtils; |
||
63 | |||
64 | /** |
||
65 | * @var Validator |
||
66 | */ |
||
67 | private $schemaValidator; |
||
68 | |||
69 | /** |
||
70 | * @var CacheProvider |
||
71 | */ |
||
72 | private $cacheProvider; |
||
73 | |||
74 | /** |
||
75 | * @param ContainerInterface $container container |
||
76 | * @param Router $router router |
||
77 | * @param Serializer $serializer serializer |
||
78 | * @param LoggerInterface $logger PSR logger (e.g. Monolog) |
||
79 | * @param SchemaUtils $schemaUtils schema utils |
||
80 | * @param Validator $schemaValidator schema validator |
||
81 | * @param CacheProvider $cacheProvider Cache service |
||
82 | */ |
||
83 | 2 | public function __construct( |
|
100 | |||
101 | /** |
||
102 | * Builds a map of baseroutes (controllers) to its relevant route to the actions. |
||
103 | * ignores schema stuff. |
||
104 | * |
||
105 | * @return array grouped array of basenames and actions.. |
||
106 | */ |
||
107 | public function getServiceRoutingMap() |
||
132 | |||
133 | /** |
||
134 | * Public function to serialize stuff according to the serializer rules. |
||
135 | * |
||
136 | * @param object $content Any content to serialize |
||
137 | * @param string $format Which format to serialize into |
||
138 | * |
||
139 | * @throws \Exception |
||
140 | * |
||
141 | * @return string $content Json content |
||
142 | */ |
||
143 | public function serializeContent($content, $format = 'json') |
||
161 | |||
162 | /** |
||
163 | * Deserialize the given content throw an exception if something went wrong |
||
164 | * |
||
165 | * @param string $content Request content |
||
166 | * @param string $documentClass Document class |
||
167 | * @param string $format Which format to deserialize from |
||
168 | * |
||
169 | * @throws \Exception |
||
170 | * |
||
171 | * @return object|array|integer|double|string|boolean |
||
172 | */ |
||
173 | public function deserializeContent($content, $documentClass, $format = 'json') |
||
183 | |||
184 | /** |
||
185 | * Validates content with the given schema, returning an array of errors. |
||
186 | * If all is good, you will receive an empty array. |
||
187 | * |
||
188 | * @param object $content \stdClass of the request content |
||
189 | * @param DocumentModel $model the model to check the schema for |
||
190 | * |
||
191 | * @return \Graviton\JsonSchemaBundle\Exception\ValidationExceptionError[] |
||
192 | * @throws \Exception |
||
193 | */ |
||
194 | public function validateContent($content, DocumentModel $model) |
||
205 | |||
206 | /** |
||
207 | * validate raw json input |
||
208 | * |
||
209 | * @param Request $request request |
||
210 | * @param Response $response response |
||
211 | * @param DocumentModel $model model |
||
212 | * @param string $content Alternative request content. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | public function checkJsonRequest(Request $request, Response $response, DocumentModel $model, $content = '') |
||
266 | |||
267 | /** |
||
268 | * Validate JSON patch for any object |
||
269 | * |
||
270 | * @param array $jsonPatch json patch as array |
||
271 | * |
||
272 | * @throws InvalidJsonPatchException |
||
273 | * @return void |
||
274 | */ |
||
275 | public function checkJsonPatchRequest(array $jsonPatch) |
||
286 | |||
287 | /** |
||
288 | * Used for backwards compatibility to PHP 5.4 |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | private function getLastJsonErrorMessage() |
||
302 | |||
303 | /** |
||
304 | * Get the serializer |
||
305 | * |
||
306 | * @return Serializer |
||
307 | */ |
||
308 | public function getSerializer() |
||
312 | |||
313 | /** |
||
314 | * It has been deemed that we search for OPTION routes in order to detect our |
||
315 | * service routes and then derive the rest from them. |
||
316 | * |
||
317 | * @return array An array with option routes |
||
318 | */ |
||
319 | public function getOptionRoutes() |
||
345 | |||
346 | /** |
||
347 | * Based on $baseName, this function returns all routes that match this basename.. |
||
348 | * So if you pass graviton.cont.action; it will return all route names that start with the same. |
||
349 | * In our routing naming schema, this means all the routes from the same controller. |
||
350 | * |
||
351 | * @param string $baseName basename |
||
352 | * |
||
353 | * @return array array with matching routes |
||
354 | */ |
||
355 | public function getRoutesByBasename($baseName) |
||
372 | |||
373 | /** |
||
374 | * Gets the Model assigned to the RestController |
||
375 | * |
||
376 | * @param Route $route Route |
||
377 | * |
||
378 | * @return bool|object The model or false |
||
|
|||
379 | * @throws \Exception |
||
380 | */ |
||
381 | public function getModelFromRoute(Route $route) |
||
392 | |||
393 | /** |
||
394 | * Gets the controller from a Route |
||
395 | * |
||
396 | * @param Route $route Route |
||
397 | * |
||
398 | * @return bool|object The controller or false |
||
399 | */ |
||
400 | public function getControllerFromRoute(Route $route) |
||
411 | |||
412 | /** |
||
413 | * @param Request $request request |
||
414 | * @return string |
||
415 | */ |
||
416 | public function getRouteName(Request $request) |
||
428 | |||
429 | /** |
||
430 | * Serialize the given record and throw an exception if something went wrong |
||
431 | * |
||
432 | * @param object|object[] $result Record(s) |
||
433 | * |
||
434 | * @throws \Graviton\ExceptionBundle\Exception\SerializationException |
||
435 | * |
||
436 | * @return string $content Json content |
||
437 | */ |
||
438 | public function serialize($result) |
||
459 | |||
460 | /** |
||
461 | * Deserialize the given content throw an exception if something went wrong |
||
462 | * |
||
463 | * @param string $content Request content |
||
464 | * @param string $documentClass Document class |
||
465 | * |
||
466 | * @throws DeserializationException |
||
467 | * |
||
468 | * @return object $record Document |
||
469 | */ |
||
470 | public function deserialize($content, $documentClass) |
||
483 | |||
484 | /** |
||
485 | * Validates the current request on schema violations. If there are errors, |
||
486 | * the exception is thrown. If not, the deserialized record is returned. |
||
487 | * |
||
488 | * @param object|string $content \stdClass of the request content |
||
489 | * @param DocumentModel $model the model to check the schema for |
||
490 | * |
||
491 | * @return ValidationExceptionError|Object |
||
492 | * @throws \Exception |
||
493 | */ |
||
494 | public function validateRequest($content, DocumentModel $model) |
||
502 | } |
||
503 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.