Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | |||
53 | /** |
||
54 | * @inheritdoc |
||
55 | */ |
||
56 | public function init() |
||
65 | |||
66 | /** |
||
67 | * Serializes the given data into a format that can be easily turned into other formats. |
||
68 | * This method mainly converts the objects of recognized types into array representation. |
||
69 | * It will not do conversion for unknown object types or non-object data. |
||
70 | * @param mixed $data the data to be serialized. |
||
71 | * @return mixed the converted data. |
||
72 | */ |
||
73 | public function serialize($data) |
||
85 | |||
86 | /** |
||
87 | * @param ResourceInterface $model |
||
88 | * @return array |
||
89 | */ |
||
90 | protected function serializeModel(ResourceInterface $model) |
||
137 | |||
138 | /** |
||
139 | * @param ResourceInterface $resource |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function serializeResource($resource) |
||
157 | |||
158 | /** |
||
159 | * @param ResourceInterface $resource |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function serializeIncluded($resource) |
||
182 | |||
183 | /** |
||
184 | * Serializes a data provider. |
||
185 | * @param DataProviderInterface $dataProvider |
||
186 | * @return array the array representation of the data provider. |
||
187 | */ |
||
188 | protected function serializeDataProvider($dataProvider) |
||
220 | |||
221 | /** |
||
222 | * Serializes a pagination into an array. |
||
223 | * @param Pagination $pagination |
||
224 | * @return array the array representation of the pagination |
||
225 | * @see addPaginationHeaders() |
||
226 | */ |
||
227 | protected function serializePagination($pagination) |
||
239 | |||
240 | /** |
||
241 | * Serializes the validation errors in a model. |
||
242 | * @param Model $model |
||
243 | * @return array the array representation of the errors |
||
244 | */ |
||
245 | protected function serializeModelErrors($model) |
||
258 | |||
259 | /** |
||
260 | * @return array |
||
261 | */ |
||
262 | protected function getRequestedFields() |
||
274 | |||
275 | protected function getIncluded() |
||
280 | |||
281 | /** |
||
282 | * Checks Identification of resource object. |
||
283 | * http://jsonapi.org/format/#document-resource-object-identification |
||
284 | * @param ResourceInterface $resource |
||
285 | */ |
||
286 | protected function checkIdentification(ResourceInterface $resource) |
||
300 | } |
||
301 |
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.