Complex classes like Resource 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 Resource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Resource implements ElementInterface |
||
| 15 | { |
||
| 16 | use LinksTrait; |
||
| 17 | use MetaTrait; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var mixed |
||
| 21 | */ |
||
| 22 | protected $data; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var SerializerInterface |
||
| 26 | */ |
||
| 27 | protected $serializer; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A list of relationships to include. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $includes = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * A list of fields to restrict to. |
||
| 38 | * |
||
| 39 | * @var array|null |
||
| 40 | */ |
||
| 41 | protected $fields; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * An array of Resources that should be merged into this one. |
||
| 45 | * |
||
| 46 | * @var Resource[] |
||
| 47 | */ |
||
| 48 | protected $merged = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param mixed $data |
||
| 52 | * @param SerializerInterface $serializer |
||
| 53 | */ |
||
| 54 | 33 | public function __construct($data, SerializerInterface $serializer) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 3 | public function getResources() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | 21 | public function toArray() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Check whether or not this resource is an identifier (i.e. does it have |
||
| 112 | * any data attached?). |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | 3 | public function isIdentifier() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 27 | public function toIdentifier() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Get the resource type. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 27 | public function getType() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the resource ID. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 33 | public function getId() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the resource attributes. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | 21 | public function getAttributes() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Get the requested fields for this resource type. |
||
| 180 | * |
||
| 181 | * @return array|null |
||
| 182 | */ |
||
| 183 | 21 | protected function getOwnFields() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Filter the given fields array (attributes or relationships) according |
||
| 194 | * to the requested fieldset. |
||
| 195 | * |
||
| 196 | * @param array $fields |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | 21 | protected function filterFields(array $fields) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Merge the attributes of merged resources into an array of attributes. |
||
| 210 | * |
||
| 211 | * @param array $attributes |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 21 | protected function mergeAttributes(array $attributes) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get the resource relationships. |
||
| 225 | * |
||
| 226 | * @return Relationship[] |
||
| 227 | */ |
||
| 228 | 21 | public function getRelationships() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get the resource relationships without considering requested ones. |
||
| 237 | * |
||
| 238 | * @return Relationship[] |
||
| 239 | */ |
||
| 240 | 3 | public function getUnfilteredRelationships() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get the resource relationships as an array. |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | 21 | public function getRelationshipsAsArray() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Get an array of built relationships. |
||
| 261 | * |
||
| 262 | * @return Relationship[] |
||
| 263 | */ |
||
| 264 | 21 | protected function buildRelationships() |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Merge the relationships of merged resources into an array of |
||
| 285 | * relationships. |
||
| 286 | * |
||
| 287 | * @param array $relationships |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | 21 | protected function mergeRelationships(array $relationships) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Convert the given array of Relationship objects into an array. |
||
| 301 | * |
||
| 302 | * @param Relationship[] $relationships |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | protected function convertRelationshipsToArray(array $relationships) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Merge a resource into this one. |
||
| 314 | * |
||
| 315 | * @param Resource $resource |
||
| 316 | */ |
||
| 317 | 3 | public function merge(Resource $resource) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | */ |
||
| 325 | 3 | public function with($relationships) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | 6 | public function fields($fields) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @return mixed |
||
| 344 | */ |
||
| 345 | public function getData() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param mixed $data |
||
| 352 | */ |
||
| 353 | public function setData($data) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return SerializerInterface |
||
| 360 | */ |
||
| 361 | public function getSerializer() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param SerializerInterface $serializer |
||
| 368 | */ |
||
| 369 | public function setSerializer(SerializerInterface $serializer) |
||
| 373 | } |
||
| 374 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.