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) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param ResourceInterface $resource |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | protected function serializeResource(ResourceInterface $resource) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Serialize resource identifier object and make type juggling |
||
| 154 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
| 155 | * @param ResourceIdentifierInterface $identifier |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param ResourceInterface $resource |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | protected function serializeIncluded($resource) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Serializes a data provider. |
||
| 199 | * @param DataProviderInterface $dataProvider |
||
| 200 | * @return array the array representation of the data provider. |
||
| 201 | */ |
||
| 202 | protected function serializeDataProvider($dataProvider) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Serializes a pagination into an array. |
||
| 237 | * @param Pagination $pagination |
||
| 238 | * @return array the array representation of the pagination |
||
| 239 | * @see addPaginationHeaders() |
||
| 240 | */ |
||
| 241 | protected function serializePagination($pagination) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Serializes the validation errors in a model. |
||
| 256 | * @param Model $model |
||
| 257 | * @return array the array representation of the errors |
||
| 258 | */ |
||
| 259 | protected function serializeModelErrors($model) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | protected function getRequestedFields() |
||
| 288 | |||
| 289 | protected function getIncluded() |
||
| 294 | } |
||
| 295 |
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.