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 | * @var LoggerInterface |
||
57 | */ |
||
58 | private $logger; |
||
59 | |||
60 | /** |
||
61 | * @var SchemaUtils |
||
62 | */ |
||
63 | private $schemaUtils; |
||
64 | |||
65 | /** |
||
66 | * @var Validator |
||
67 | */ |
||
68 | 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 | 6 | public function __construct( |
|
80 | ContainerInterface $container, |
||
81 | Router $router, |
||
82 | Serializer $serializer, |
||
83 | LoggerInterface $logger, |
||
84 | SerializationContext $serializerContext, |
||
85 | SchemaUtils $schemaUtils, |
||
86 | Validator $schemaValidator |
||
87 | ) { |
||
88 | 6 | $this->container = $container; |
|
89 | 6 | $this->serializer = $serializer; |
|
90 | 6 | $this->serializerContext = $serializerContext; |
|
91 | 6 | $this->router = $router; |
|
92 | 6 | $this->logger = $logger; |
|
93 | 6 | $this->schemaUtils = $schemaUtils; |
|
94 | 6 | $this->schemaValidator = $schemaValidator; |
|
95 | 6 | } |
|
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 | 4 | public function serializeContent($content, $format = 'json') |
|
137 | { |
||
138 | try { |
||
139 | 4 | return $this->getSerializer()->serialize( |
|
140 | 2 | $content, |
|
141 | 2 | $format, |
|
142 | 4 | $this->getSerializerContext() |
|
143 | 2 | ); |
|
144 | } catch (\Exception $e) { |
||
145 | $msg = sprintf( |
||
146 | 'Cannot serialize content class: %s; with id: %s; Message: %s', |
||
147 | get_class($content), |
||
148 | method_exists($content, 'getId') ? $content->getId() : '-no id-', |
||
149 | str_replace('MongoDBODMProxies\__CG__\GravitonDyn', '', $e->getMessage()) |
||
150 | ); |
||
151 | $this->logger->alert($msg); |
||
152 | throw new \Exception($msg, $e->getCode()); |
||
153 | } |
||
154 | } |
||
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 | 4 | 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 | 4 | public function validateContent($content, DocumentModel $model) |
|
199 | |||
200 | /** |
||
201 | * validate raw json input |
||
202 | * |
||
203 | * @param Request $request request |
||
204 | * @param Response $response response |
||
205 | * @param DocumentModel $model model |
||
206 | * @param string $content Alternative request content. |
||
207 | * |
||
208 | * @return void |
||
209 | */ |
||
210 | 4 | public function checkJsonRequest(Request $request, Response $response, DocumentModel $model, $content = '') |
|
260 | |||
261 | /** |
||
262 | * Validate JSON patch for any object |
||
263 | * |
||
264 | * @param array $jsonPatch json patch as array |
||
265 | * |
||
266 | * @throws InvalidJsonPatchException |
||
267 | * @return void |
||
268 | */ |
||
269 | public function checkJsonPatchRequest(array $jsonPatch) |
||
280 | |||
281 | /** |
||
282 | * Used for backwards compatibility to PHP 5.4 |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | private function getLastJsonErrorMessage() |
||
296 | |||
297 | /** |
||
298 | * Get the serializer |
||
299 | * |
||
300 | * @return Serializer |
||
301 | */ |
||
302 | 4 | public function getSerializer() |
|
306 | |||
307 | /** |
||
308 | * Get the serializer context |
||
309 | * |
||
310 | * @return SerializationContext |
||
311 | */ |
||
312 | 4 | public function getSerializerContext() |
|
316 | |||
317 | /** |
||
318 | * It has been deemed that we search for OPTION routes in order to detect our |
||
319 | * service routes and then derive the rest from them. |
||
320 | * |
||
321 | * @return array An array with option routes |
||
322 | */ |
||
323 | public function getOptionRoutes() |
||
347 | |||
348 | /** |
||
349 | * Based on $baseName, this function returns all routes that match this basename.. |
||
350 | * So if you pass graviton.cont.action; it will return all route names that start with the same. |
||
351 | * In our routing naming schema, this means all the routes from the same controller. |
||
352 | * |
||
353 | * @param string $baseName basename |
||
354 | * |
||
355 | * @return array array with matching routes |
||
356 | */ |
||
357 | public function getRoutesByBasename($baseName) |
||
370 | |||
371 | /** |
||
372 | * Gets the Model assigned to the RestController |
||
373 | * |
||
374 | * @param Route $route Route |
||
375 | * |
||
376 | * @return bool|object The model or false |
||
|
|||
377 | * @throws \Exception |
||
378 | */ |
||
379 | public function getModelFromRoute(Route $route) |
||
390 | |||
391 | /** |
||
392 | * Gets the controller from a Route |
||
393 | * |
||
394 | * @param Route $route Route |
||
395 | * |
||
396 | * @return bool|object The controller or false |
||
397 | */ |
||
398 | public function getControllerFromRoute(Route $route) |
||
409 | } |
||
410 |
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.