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 |
||
19 | class Serializer extends Component |
||
20 | { |
||
21 | /** |
||
22 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
23 | * for a [[Model]] object. If the parameter is not provided or empty, the default set of fields as defined |
||
24 | * by [[Model::fields()]] will be returned. |
||
25 | */ |
||
26 | public $fieldsParam = 'fields'; |
||
27 | /** |
||
28 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
29 | * in addition to those listed in [[fieldsParam]] for a resource object. |
||
30 | */ |
||
31 | public $expandParam = 'include'; |
||
32 | /** |
||
33 | * @var string the name of the envelope (e.g. `_links`) for returning the links objects. |
||
34 | * It takes effect only, if `collectionEnvelope` is set. |
||
35 | * @since 2.0.4 |
||
36 | */ |
||
37 | public $linksEnvelope = 'links'; |
||
38 | /** |
||
39 | * @var string the name of the envelope (e.g. `_meta`) for returning the pagination object. |
||
40 | * It takes effect only, if `collectionEnvelope` is set. |
||
41 | * @since 2.0.4 |
||
42 | */ |
||
43 | public $metaEnvelope = 'meta'; |
||
44 | /** |
||
45 | * @var Request the current request. If not set, the `request` application component will be used. |
||
46 | */ |
||
47 | public $request; |
||
48 | /** |
||
49 | * @var Response the response to be sent. If not set, the `response` application component will be used. |
||
50 | */ |
||
51 | public $response; |
||
52 | |||
53 | /** |
||
54 | * Prepares the member name that should be returned. |
||
55 | * If not set, all member names will be converted to recommended format. |
||
56 | * For example, both 'firstName' and 'first_name' will be converted to 'first-name'. |
||
57 | * @var callable |
||
58 | */ |
||
59 | public $prepareMemberName; |
||
60 | |||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | public function init() |
||
74 | |||
75 | /** |
||
76 | * Serializes the given data into a format that can be easily turned into other formats. |
||
77 | * This method mainly converts the objects of recognized types into array representation. |
||
78 | * It will not do conversion for unknown object types or non-object data. |
||
79 | * @param mixed $data the data to be serialized. |
||
80 | * @return mixed the converted data. |
||
81 | */ |
||
82 | public function serialize($data) |
||
94 | |||
95 | /** |
||
96 | * @param ResourceInterface $model |
||
97 | * @return array |
||
98 | */ |
||
99 | protected function serializeModel(ResourceInterface $model) |
||
146 | |||
147 | /** |
||
148 | * @param ResourceInterface $resource |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function serializeResource(ResourceInterface $resource) |
||
166 | |||
167 | /** |
||
168 | * Serialize resource identifier object and make type juggling |
||
169 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
170 | * @param ResourceIdentifierInterface $identifier |
||
171 | * @return array |
||
172 | */ |
||
173 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
186 | |||
187 | /** |
||
188 | * @param ResourceInterface $resource |
||
189 | * @return array |
||
190 | */ |
||
191 | protected function serializeIncluded($resource) |
||
211 | |||
212 | /** |
||
213 | * Serializes a data provider. |
||
214 | * @param DataProviderInterface $dataProvider |
||
215 | * @return array the array representation of the data provider. |
||
216 | */ |
||
217 | protected function serializeDataProvider($dataProvider) |
||
249 | |||
250 | /** |
||
251 | * Serializes a pagination into an array. |
||
252 | * @param Pagination $pagination |
||
253 | * @return array the array representation of the pagination |
||
254 | * @see addPaginationHeaders() |
||
255 | */ |
||
256 | protected function serializePagination($pagination) |
||
268 | |||
269 | /** |
||
270 | * Serializes the validation errors in a model. |
||
271 | * @param Model $model |
||
272 | * @return array the array representation of the errors |
||
273 | */ |
||
274 | protected function serializeModelErrors($model) |
||
287 | |||
288 | /** |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function getRequestedFields() |
||
303 | |||
304 | protected function getIncluded() |
||
309 | |||
310 | |||
311 | /** |
||
312 | * Format member names according to recommendations for JSON API implementations |
||
313 | * @link http://jsonapi.org/format/#document-member-names |
||
314 | * @param array $memberNames |
||
315 | * @return array |
||
316 | */ |
||
317 | protected function prepareMemberNames(array $memberNames = []) |
||
324 | } |
||
325 |
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.