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, array $included = null) |
||
154 | |||
155 | /** |
||
156 | * @param ResourceInterface $resource |
||
157 | * @return array |
||
158 | */ |
||
159 | protected function serializeResource(ResourceInterface $resource) |
||
187 | |||
188 | /** |
||
189 | * Serialize resource identifier object and make type juggling |
||
190 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
191 | * @param ResourceIdentifierInterface $identifier |
||
192 | * @return array |
||
193 | */ |
||
194 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
210 | |||
211 | /** |
||
212 | * @param ResourceInterface|array $resources |
||
213 | * @param null|array $included |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function serializeIncluded($resources, array $included = null) |
||
265 | |||
266 | /** |
||
267 | * Serializes a data provider. |
||
268 | * @param DataProviderInterface $dataProvider |
||
269 | * @return null|array the array representation of the data provider. |
||
270 | */ |
||
271 | protected function serializeDataProvider($dataProvider) |
||
300 | |||
301 | /** |
||
302 | * Serializes a pagination into an array. |
||
303 | * @param Pagination $pagination |
||
304 | * @return array the array representation of the pagination |
||
305 | * @see addPaginationHeaders() |
||
306 | */ |
||
307 | protected function serializePagination($pagination) |
||
319 | |||
320 | /** |
||
321 | * Serializes the validation errors in a model. |
||
322 | * @param Model $model |
||
323 | * @return array the array representation of the errors |
||
324 | */ |
||
325 | protected function serializeModelErrors($model) |
||
339 | |||
340 | /** |
||
341 | * @return array |
||
342 | */ |
||
343 | protected function getRequestedFields() |
||
355 | |||
356 | /** |
||
357 | * @return array|null |
||
358 | */ |
||
359 | protected function getIncluded() |
||
367 | |||
368 | |||
369 | /** |
||
370 | * Format member names according to recommendations for JSON API implementations |
||
371 | * @link http://jsonapi.org/format/#document-member-names |
||
372 | * @param array $memberNames |
||
373 | * @return array |
||
374 | */ |
||
375 | protected function prepareMemberNames(array $memberNames = []) |
||
379 | } |
||
380 |
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.