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 | * @var bool whether to automatically pluralize the `type` of resource. |
||
| 53 | */ |
||
| 54 | public $pluralize = true; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Prepares the member name that should be returned. |
||
| 58 | * If not set, all member names will be converted to recommended format. |
||
| 59 | * For example, both 'firstName' and 'first_name' will be converted to 'first-name'. |
||
| 60 | * @var callable |
||
| 61 | */ |
||
| 62 | public $prepareMemberName = ['tuyakhov\jsonapi\Inflector', 'var2member']; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Converts a member name to an attribute name. |
||
| 66 | * @var callable |
||
| 67 | */ |
||
| 68 | public $formatMemberName = ['tuyakhov\jsonapi\Inflector', 'member2var']; |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * @inheritdoc |
||
| 73 | */ |
||
| 74 | public function init() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Serializes the given data into a format that can be easily turned into other formats. |
||
| 86 | * This method mainly converts the objects of recognized types into array representation. |
||
| 87 | * It will not do conversion for unknown object types or non-object data. |
||
| 88 | * @param mixed $data the data to be serialized. |
||
| 89 | * @return mixed the converted data. |
||
| 90 | */ |
||
| 91 | public function serialize($data) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param ResourceInterface $model |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | protected function serializeModel(ResourceInterface $model) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param ResourceInterface $resource |
||
| 160 | * @return array |
||
| 161 | */ |
||
| 162 | protected function serializeResource(ResourceInterface $resource) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Serialize resource identifier object and make type juggling |
||
| 180 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
| 181 | * @param ResourceIdentifierInterface $identifier |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param ResourceInterface $resource |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | protected function serializeIncluded($resource) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Serializes a data provider. |
||
| 228 | * @param DataProviderInterface $dataProvider |
||
| 229 | * @return array the array representation of the data provider. |
||
| 230 | */ |
||
| 231 | protected function serializeDataProvider($dataProvider) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Serializes a pagination into an array. |
||
| 266 | * @param Pagination $pagination |
||
| 267 | * @return array the array representation of the pagination |
||
| 268 | * @see addPaginationHeaders() |
||
| 269 | */ |
||
| 270 | protected function serializePagination($pagination) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Serializes the validation errors in a model. |
||
| 285 | * @param Model $model |
||
| 286 | * @return array the array representation of the errors |
||
| 287 | */ |
||
| 288 | protected function serializeModelErrors($model) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | protected function getRequestedFields() |
||
| 317 | |||
| 318 | protected function getIncluded() |
||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * Format member names according to recommendations for JSON API implementations |
||
| 327 | * @link http://jsonapi.org/format/#document-member-names |
||
| 328 | * @param array $memberNames |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | protected function prepareMemberNames(array $memberNames = []) |
||
| 335 | } |
||
| 336 |
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.