Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Document 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 Document, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Document implements JsonSerializable |
||
17 | { |
||
18 | use LinksTrait; |
||
19 | use MetaTrait; |
||
20 | |||
21 | const MEDIA_TYPE = 'application/vnd.api+json'; |
||
22 | |||
23 | /** |
||
24 | * The data object. |
||
25 | * |
||
26 | * @var ResourceInterface|ResourceInterface[]|null |
||
27 | */ |
||
28 | protected $data; |
||
29 | |||
30 | /** |
||
31 | * The errors array. |
||
32 | * |
||
33 | * @var array|null |
||
34 | */ |
||
35 | protected $errors; |
||
36 | |||
37 | /** |
||
38 | * The jsonapi array. |
||
39 | * |
||
40 | * @var array|null |
||
41 | */ |
||
42 | protected $jsonapi; |
||
43 | |||
44 | /** |
||
45 | * Relationships to include. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $include = []; |
||
50 | |||
51 | /** |
||
52 | 12 | * Sparse fieldsets. |
|
53 | * |
||
54 | 12 | * @var array |
|
55 | 12 | */ |
|
56 | protected $fields = []; |
||
57 | |||
58 | /** |
||
59 | * @param ResourceInterface|ResourceInterface[] $data |
||
|
|||
60 | */ |
||
61 | public function __construct($data = null) |
||
65 | 9 | ||
66 | /** |
||
67 | 9 | * Get the data object. |
|
68 | * |
||
69 | 9 | * @return ResourceInterface|ResourceInterface[]|null $data |
|
70 | 9 | */ |
|
71 | public function getData() |
||
75 | 3 | ||
76 | 3 | /** |
|
77 | 9 | * Set the data object. |
|
78 | 9 | * |
|
79 | * @param ResourceInterface|ResourceInterface[]|null $data |
||
80 | * |
||
81 | 9 | * @return $this |
|
82 | 3 | */ |
|
83 | public function setData($data) |
||
89 | |||
90 | /** |
||
91 | * Get the errors array. |
||
92 | 3 | * |
|
93 | * @return array|null $errors |
||
94 | */ |
||
95 | public function getErrors() |
||
99 | 9 | ||
100 | /** |
||
101 | 9 | * Set the errors array. |
|
102 | * |
||
103 | * @param array|null $errors |
||
104 | 3 | * |
|
105 | 9 | * @return $this |
|
106 | */ |
||
107 | 9 | public function setErrors(array $errors = null) |
|
113 | |||
114 | /** |
||
115 | * Get the jsonapi array. |
||
116 | 3 | * |
|
117 | * @return array|null $jsonapi |
||
118 | 3 | */ |
|
119 | 3 | public function getJsonapi() |
|
123 | |||
124 | 3 | /** |
|
125 | * Set the jsonapi array. |
||
126 | * |
||
127 | 3 | * @param array|null $jsonapi |
|
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setJsonapi(array $jsonapi = null) |
||
137 | |||
138 | /** |
||
139 | * Get the relationships to include. |
||
140 | * |
||
141 | * @return array $include |
||
142 | */ |
||
143 | public function getInclude() |
||
147 | |||
148 | /** |
||
149 | * Set the relationships to include. |
||
150 | * |
||
151 | * @param array $include |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setInclude(array $include) |
||
161 | |||
162 | /** |
||
163 | * Get the sparse fieldsets. |
||
164 | * |
||
165 | * @return array $fields |
||
166 | */ |
||
167 | public function getFields() |
||
171 | |||
172 | /** |
||
173 | * Set the sparse fieldsets. |
||
174 | * |
||
175 | * @param array $fields |
||
176 | * |
||
177 | 12 | * @return $this |
|
178 | */ |
||
179 | 12 | public function setFields(array $fields) |
|
185 | 12 | ||
186 | 9 | /** |
|
187 | * Build the JSON-API document as an array. |
||
188 | 9 | * |
|
189 | * @return array |
||
190 | 9 | */ |
|
191 | 3 | public function toArray() |
|
248 | |||
249 | /** |
||
250 | * Build the JSON-API document and encode it as a JSON string. |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function __toString() |
||
255 | { |
||
256 | return json_encode($this->toArray()); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Serialize for JSON usage. |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | public function jsonSerialize() |
||
265 | { |
||
266 | return $this->toArray(); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Recursively add the given resources and their relationships to a map. |
||
271 | * |
||
272 | * @param array &$map The map to merge resources into. |
||
273 | * @param ResourceInterface[] $resources |
||
274 | * @param array $include An array of relationship paths to include. |
||
275 | */ |
||
276 | private function addResourcesToMap(array &$map, array $resources, array $include) |
||
309 | |||
310 | /** |
||
311 | * Serialize the given resource as an array and add it to the given map. |
||
312 | * |
||
313 | * If it is already present in the map, its properties will be merged into |
||
314 | * the existing array. |
||
315 | * |
||
316 | * @param array &$map |
||
317 | * @param ResourceInterface $resource |
||
318 | * @param Relationship[] $resource |
||
319 | */ |
||
320 | private function addResourceToMap(array &$map, ResourceInterface $resource, array $relationships) |
||
363 | |||
364 | /** |
||
365 | * Index relationship paths by top-level relationships. |
||
366 | * |
||
367 | * Given an array of relationship paths such as: |
||
368 | * |
||
369 | * ['user', 'user.employer', 'user.employer.country', 'comments'] |
||
370 | * |
||
371 | * Returns an array with key-value pairs of top-level relationships and |
||
372 | * their nested relationships: |
||
373 | * |
||
374 | * ['user' => ['employer', 'employer.country'], 'comments' => []] |
||
375 | * |
||
376 | * @param array $paths |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | private function indexRelationshipPaths(array $paths) |
||
398 | |||
399 | /** |
||
400 | * Get the fields that should be included for resources of the given type. |
||
401 | * |
||
402 | * @param string $type |
||
403 | * |
||
404 | * @return array|null |
||
405 | */ |
||
406 | private function getFieldsForType($type) |
||
410 | } |
||
411 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.