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) |
||
124 | |||
125 | /** |
||
126 | * @param ResourceInterface $resource |
||
127 | * @return array |
||
128 | */ |
||
129 | protected function serializeResource($resource) |
||
144 | |||
145 | /** |
||
146 | * @param ResourceInterface $resource |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function serializeIncluded($resource) |
||
164 | |||
165 | /** |
||
166 | * Serializes a data provider. |
||
167 | * @param DataProviderInterface $dataProvider |
||
168 | * @return array the array representation of the data provider. |
||
169 | */ |
||
170 | protected function serializeDataProvider($dataProvider) |
||
202 | |||
203 | /** |
||
204 | * Serializes a pagination into an array. |
||
205 | * @param Pagination $pagination |
||
206 | * @return array the array representation of the pagination |
||
207 | * @see addPaginationHeaders() |
||
208 | */ |
||
209 | protected function serializePagination($pagination) |
||
221 | |||
222 | /** |
||
223 | * Serializes the validation errors in a model. |
||
224 | * @param Model $model |
||
225 | * @return array the array representation of the errors |
||
226 | */ |
||
227 | protected function serializeModelErrors($model) |
||
240 | |||
241 | /** |
||
242 | * @return array |
||
243 | */ |
||
244 | protected function getRequestedFields() |
||
256 | |||
257 | protected function getIncluded() |
||
262 | } |
||
263 |
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.