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) |
||
125 | |||
126 | /** |
||
127 | * @param ResourceInterface $resource |
||
128 | * @return array |
||
129 | */ |
||
130 | protected function serializeResource($resource) |
||
145 | |||
146 | /** |
||
147 | * @param ResourceInterface $resource |
||
148 | * @return array |
||
149 | */ |
||
150 | protected function serializeIncluded($resource) |
||
170 | |||
171 | /** |
||
172 | * Serializes a data provider. |
||
173 | * @param DataProviderInterface $dataProvider |
||
174 | * @return array the array representation of the data provider. |
||
175 | */ |
||
176 | protected function serializeDataProvider($dataProvider) |
||
208 | |||
209 | /** |
||
210 | * Serializes a pagination into an array. |
||
211 | * @param Pagination $pagination |
||
212 | * @return array the array representation of the pagination |
||
213 | * @see addPaginationHeaders() |
||
214 | */ |
||
215 | protected function serializePagination($pagination) |
||
227 | |||
228 | /** |
||
229 | * Serializes the validation errors in a model. |
||
230 | * @param Model $model |
||
231 | * @return array the array representation of the errors |
||
232 | */ |
||
233 | protected function serializeModelErrors($model) |
||
246 | |||
247 | /** |
||
248 | * @return array |
||
249 | */ |
||
250 | protected function getRequestedFields() |
||
262 | |||
263 | protected function getIncluded() |
||
268 | } |
||
269 |
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.