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 array $included |
||
| 106 | * @param ResourceInterface $model |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | protected function serializeModel(ResourceInterface $model, array $included = []) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param ResourceInterface $resource |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function serializeResource(ResourceInterface $resource) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Serialize resource identifier object and make type juggling |
||
| 188 | * @link http://jsonapi.org/format/#document-resource-object-identification |
||
| 189 | * @param ResourceIdentifierInterface $identifier |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | protected function serializeIdentifier(ResourceIdentifierInterface $identifier) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param ResourceInterface|array $resources |
||
| 211 | * @param array $included |
||
| 212 | * @param true $assoc |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | protected function serializeIncluded($resources, array $included = [], $assoc = false) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Serializes a data provider. |
||
| 263 | * @param DataProviderInterface $dataProvider |
||
| 264 | * @return null|array the array representation of the data provider. |
||
| 265 | */ |
||
| 266 | protected function serializeDataProvider($dataProvider) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Serializes a pagination into an array. |
||
| 298 | * @param Pagination $pagination |
||
| 299 | * @return array the array representation of the pagination |
||
| 300 | * @see addPaginationHeaders() |
||
| 301 | */ |
||
| 302 | protected function serializePagination($pagination) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Serializes the validation errors in a model. |
||
| 317 | * @param Model $model |
||
| 318 | * @return array the array representation of the errors |
||
| 319 | */ |
||
| 320 | protected function serializeModelErrors($model) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | protected function getRequestedFields() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return array|null |
||
| 353 | */ |
||
| 354 | protected function getIncluded() |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Format member names according to recommendations for JSON API implementations |
||
| 363 | * @link http://jsonapi.org/format/#document-member-names |
||
| 364 | * @param array $memberNames |
||
| 365 | * @return array |
||
| 366 | */ |
||
| 367 | protected function prepareMemberNames(array $memberNames = []) |
||
| 371 | } |
||
| 372 |
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.