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 |
||
| 20 | class Serializer extends Component |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
| 24 | * for a [[Model]] object. If the parameter is not provided or empty, the default set of fields as defined |
||
| 25 | * by [[Model::fields()]] will be returned. |
||
| 26 | */ |
||
| 27 | public $fieldsParam = 'fields'; |
||
| 28 | /** |
||
| 29 | * @var string the name of the query parameter containing the information about which fields should be returned |
||
| 30 | * in addition to those listed in [[fieldsParam]] for a resource object. |
||
| 31 | */ |
||
| 32 | public $expandParam = 'include'; |
||
| 33 | /** |
||
| 34 | * @var string the name of the envelope (e.g. `_links`) for returning the links objects. |
||
| 35 | * It takes effect only, if `collectionEnvelope` is set. |
||
| 36 | * @since 2.0.4 |
||
| 37 | */ |
||
| 38 | public $linksEnvelope = 'links'; |
||
| 39 | /** |
||
| 40 | * @var string the name of the envelope (e.g. `_meta`) for returning the pagination object. |
||
| 41 | * It takes effect only, if `collectionEnvelope` is set. |
||
| 42 | * @since 2.0.4 |
||
| 43 | */ |
||
| 44 | public $metaEnvelope = 'meta'; |
||
| 45 | /** |
||
| 46 | * @var Request the current request. If not set, the `request` application component will be used. |
||
| 47 | */ |
||
| 48 | public $request; |
||
| 49 | /** |
||
| 50 | * @var Response the response to be sent. If not set, the `response` application component will be used. |
||
| 51 | */ |
||
| 52 | public $response; |
||
| 53 | /** |
||
| 54 | * @var bool whether to automatically pluralize the `type` of resource. |
||
| 55 | */ |
||
| 56 | public $pluralize = true; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Prepares the member name that should be returned. |
||
| 60 | * If not set, all member names will be converted to recommended format. |
||
| 61 | * For example, both 'firstName' and 'first_name' will be converted to 'first-name'. |
||
| 62 | * @var callable |
||
| 63 | */ |
||
| 64 | public $prepareMemberName = ['tuyakhov\jsonapi\Inflector', 'var2member']; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Converts a member name to an attribute name. |
||
| 68 | * @var callable |
||
| 69 | */ |
||
| 70 | public $formatMemberName = ['tuyakhov\jsonapi\Inflector', 'member2var']; |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * @inheritdoc |
||
| 75 | */ |
||
| 76 | public function init() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Serializes the given data into a format that can be easily turned into other formats. |
||
| 88 | * This method mainly converts the objects of recognized types into array representation. |
||
| 89 | * It will not do conversion for unknown object types or non-object data. |
||
| 90 | * @param mixed $data the data to be serialized. |
||
| 91 | * @return mixed the converted data. |
||
| 92 | */ |
||
| 93 | public function serialize($data) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param ResourceInterface $model |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | protected function serializeModel(ResourceInterface $model) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param ResourceInterface $resource |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function serializeResource(ResourceInterface $resource) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Serialize resource identifier object and make type juggling |
||
| 184 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
| 185 | * @param ResourceIdentifierInterface $identifier |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param ResourceInterface|array $resources |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | protected function serializeIncluded($resources) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Serializes a data provider. |
||
| 241 | * @param ActiveDataProvider $dataProvider |
||
| 242 | * @return array the array representation of the data provider. |
||
| 243 | */ |
||
| 244 | protected function serializeActiveDataProvider($dataProvider) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Serializes a data provider. |
||
| 256 | * @param DataProviderInterface $dataProvider |
||
| 257 | * @return array the array representation of the data provider. |
||
| 258 | */ |
||
| 259 | protected function serializeDataProvider($dataProvider) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Serializes a pagination into an array. |
||
| 290 | * @param Pagination $pagination |
||
| 291 | * @return array the array representation of the pagination |
||
| 292 | * @see addPaginationHeaders() |
||
| 293 | */ |
||
| 294 | protected function serializePagination($pagination) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Serializes the validation errors in a model. |
||
| 309 | * @param Model $model |
||
| 310 | * @return array the array representation of the errors |
||
| 311 | */ |
||
| 312 | protected function serializeModelErrors($model) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | protected function getRequestedFields() |
||
| 342 | |||
| 343 | protected function getIncluded() |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * Format member names according to recommendations for JSON API implementations |
||
| 352 | * @link http://jsonapi.org/format/#document-member-names |
||
| 353 | * @param array $memberNames |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | protected function prepareMemberNames(array $memberNames = []) |
||
| 360 | } |
||
| 361 |
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.