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 |
||
17 | class Serializer extends Component |
||
18 | { |
||
19 | /** |
||
20 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
21 | * for a [[Model]] object. If the parameter is not provided or empty, the default set of fields as defined |
||
22 | * by [[Model::fields()]] will be returned. |
||
23 | */ |
||
24 | public $fieldsParam = 'fields'; |
||
25 | /** |
||
26 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
27 | * in addition to those listed in [[fieldsParam]] for a resource object. |
||
28 | */ |
||
29 | public $expandParam = 'include'; |
||
30 | /** |
||
31 | * @var string the name of the envelope (e.g. `_links`) for returning the links objects. |
||
32 | * It takes effect only, if `collectionEnvelope` is set. |
||
33 | * @since 2.0.4 |
||
34 | */ |
||
35 | public $linksEnvelope = 'links'; |
||
36 | /** |
||
37 | * @var string the name of the envelope (e.g. `_meta`) for returning the pagination object. |
||
38 | * It takes effect only, if `collectionEnvelope` is set. |
||
39 | * @since 2.0.4 |
||
40 | */ |
||
41 | public $metaEnvelope = 'meta'; |
||
42 | /** |
||
43 | * @var Request the current request. If not set, the `request` application component will be used. |
||
44 | */ |
||
45 | public $request; |
||
46 | /** |
||
47 | * @var Response the response to be sent. If not set, the `response` application component will be used. |
||
48 | */ |
||
49 | public $response; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function init() |
||
64 | |||
65 | /** |
||
66 | * Serializes the given data into a format that can be easily turned into other formats. |
||
67 | * This method mainly converts the objects of recognized types into array representation. |
||
68 | * It will not do conversion for unknown object types or non-object data. |
||
69 | * @param mixed $data the data to be serialized. |
||
70 | * @return mixed the converted data. |
||
71 | */ |
||
72 | public function serialize($data) |
||
84 | |||
85 | /** |
||
86 | * @param ResourceInterface $model |
||
87 | * @return array |
||
88 | */ |
||
89 | protected function serializeModel($model) |
||
132 | |||
133 | /** |
||
134 | * @param ResourceInterface $resource |
||
135 | * @return array |
||
136 | */ |
||
137 | protected function serializeResource($resource) |
||
152 | |||
153 | /** |
||
154 | * @param ResourceInterface $resource |
||
155 | * @return array |
||
156 | */ |
||
157 | protected function serializeIncluded($resource) |
||
177 | |||
178 | /** |
||
179 | * Serializes a data provider. |
||
180 | * @param DataProviderInterface $dataProvider |
||
181 | * @return array the array representation of the data provider. |
||
182 | */ |
||
183 | protected function serializeDataProvider($dataProvider) |
||
215 | |||
216 | /** |
||
217 | * Serializes a pagination into an array. |
||
218 | * @param Pagination $pagination |
||
219 | * @return array the array representation of the pagination |
||
220 | * @see addPaginationHeaders() |
||
221 | */ |
||
222 | protected function serializePagination($pagination) |
||
234 | |||
235 | /** |
||
236 | * Serializes the validation errors in a model. |
||
237 | * @param Model $model |
||
238 | * @return array the array representation of the errors |
||
239 | */ |
||
240 | protected function serializeModelErrors($model) |
||
253 | |||
254 | /** |
||
255 | * @return array |
||
256 | */ |
||
257 | protected function getRequestedFields() |
||
269 | |||
270 | protected function getIncluded() |
||
275 | } |
||
276 |
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.