Complex classes like Serializer 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 Serializer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Serializer extends Component |
||
19 | { |
||
20 | /** |
||
21 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
22 | * for a [[Model]] object. If the parameter is not provided or empty, the default set of fields as defined |
||
23 | * by [[Model::fields()]] will be returned. |
||
24 | */ |
||
25 | public $fieldsParam = 'fields'; |
||
26 | /** |
||
27 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
28 | * in addition to those listed in [[fieldsParam]] for a resource object. |
||
29 | */ |
||
30 | public $expandParam = 'include'; |
||
31 | /** |
||
32 | * @var string the name of the envelope (e.g. `_links`) for returning the links objects. |
||
33 | * It takes effect only, if `collectionEnvelope` is set. |
||
34 | * @since 2.0.4 |
||
35 | */ |
||
36 | public $linksEnvelope = 'links'; |
||
37 | /** |
||
38 | * @var string the name of the envelope (e.g. `_meta`) for returning the pagination object. |
||
39 | * It takes effect only, if `collectionEnvelope` is set. |
||
40 | * @since 2.0.4 |
||
41 | */ |
||
42 | public $metaEnvelope = 'meta'; |
||
43 | /** |
||
44 | * @var Request the current request. If not set, the `request` application component will be used. |
||
45 | */ |
||
46 | public $request; |
||
47 | /** |
||
48 | * @var Response the response to be sent. If not set, the `response` application component will be used. |
||
49 | */ |
||
50 | public $response; |
||
51 | /** |
||
52 | * @var bool whether to automatically pluralize the `type` of resource. |
||
53 | */ |
||
54 | public $pluralize = true; |
||
55 | |||
56 | /** |
||
57 | * Prepares the member name that should be returned. |
||
58 | * If not set, all member names will be converted to recommended format. |
||
59 | * For example, both 'firstName' and 'first_name' will be converted to 'first-name'. |
||
60 | * @var callable |
||
61 | */ |
||
62 | public $prepareMemberName = ['tuyakhov\jsonapi\Inflector', 'var2member']; |
||
63 | |||
64 | /** |
||
65 | * Converts a member name to an attribute name. |
||
66 | * @var callable |
||
67 | */ |
||
68 | public $formatMemberName = ['tuyakhov\jsonapi\Inflector', 'member2var']; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | public function init() |
||
83 | |||
84 | /** |
||
85 | * Serializes the given data into a format that can be easily turned into other formats. |
||
86 | * This method mainly converts the objects of recognized types into array representation. |
||
87 | * It will not do conversion for unknown object types or non-object data. |
||
88 | * @param mixed $data the data to be serialized. |
||
89 | * @return mixed the converted data. |
||
90 | */ |
||
91 | public function serialize($data) |
||
103 | |||
104 | /** |
||
105 | * @param ResourceInterface $model |
||
106 | * @return array |
||
107 | */ |
||
108 | protected function serializeModel(ResourceInterface $model) |
||
157 | |||
158 | /** |
||
159 | * @param ResourceInterface $resource |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function serializeResource(ResourceInterface $resource) |
||
177 | |||
178 | /** |
||
179 | * Serialize resource identifier object and make type juggling |
||
180 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
181 | * @param ResourceIdentifierInterface $identifier |
||
182 | * @return array |
||
183 | */ |
||
184 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
200 | |||
201 | /** |
||
202 | * @param ResourceInterface $resource |
||
203 | * @param array $includedObjects All included objects from previous serialization, prevent duplicate includes |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function serializeIncluded($resource, $includedObjects = []) |
||
207 | { |
||
208 | $included = $this->getIncluded(); |
||
209 | $relationships = $resource->getResourceRelationships(); |
||
210 | foreach ($relationships as $name => $relationship) { |
||
211 | if (!in_array($name, $included)) { |
||
212 | continue; |
||
213 | } |
||
214 | if (!is_array($relationship)) { |
||
215 | $relationship = [$relationship]; |
||
216 | } |
||
217 | foreach ($relationship as $model) { |
||
218 | if ($model instanceof ResourceInterface) { |
||
219 | $deduplicateKey = $model->getType() . '|' . $model->getId(); |
||
220 | if (!isset($includedObjects[$deduplicateKey])) { |
||
221 | $includedObjects[$deduplicateKey] = $this->serializeModel($model); |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | return $includedObjects; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Serializes a data provider. |
||
231 | * @param DataProviderInterface $dataProvider |
||
232 | * @return array the array representation of the data provider. |
||
233 | */ |
||
234 | protected function serializeDataProvider($dataProvider) |
||
235 | { |
||
236 | if ($this->request->getIsHead()) { |
||
237 | return null; |
||
238 | } else { |
||
239 | $models = []; |
||
240 | $includedModels = []; |
||
241 | |||
242 | foreach ($dataProvider->getModels() as $model) { |
||
243 | if ($model instanceof ResourceInterface) { |
||
244 | $models[] = $this->serializeModel($model); |
||
245 | |||
246 | $includedModels = $this->serializeIncluded($model, $includedModels); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | $result = ['data' => $models]; |
||
251 | |||
252 | if (!empty($includedModels)) { |
||
253 | $result['included'] = array_values($includedModels); |
||
254 | } |
||
255 | |||
256 | if (($pagination = $dataProvider->getPagination()) !== false) { |
||
257 | return array_merge($result, $this->serializePagination($pagination)); |
||
258 | } |
||
259 | |||
260 | return $result; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Serializes a pagination into an array. |
||
266 | * @param Pagination $pagination |
||
267 | * @return array the array representation of the pagination |
||
268 | * @see addPaginationHeaders() |
||
269 | */ |
||
270 | protected function serializePagination($pagination) |
||
282 | |||
283 | /** |
||
284 | * Serializes the validation errors in a model. |
||
285 | * @param Model $model |
||
286 | * @return array the array representation of the errors |
||
287 | */ |
||
288 | protected function serializeModelErrors($model) |
||
301 | |||
302 | /** |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function getRequestedFields() |
||
317 | |||
318 | protected function getIncluded() |
||
323 | |||
324 | |||
325 | /** |
||
326 | * Format member names according to recommendations for JSON API implementations |
||
327 | * @link http://jsonapi.org/format/#document-member-names |
||
328 | * @param array $memberNames |
||
329 | * @return array |
||
330 | */ |
||
331 | protected function prepareMemberNames(array $memberNames = []) |
||
335 | } |
||
336 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.