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 namespace Neomerx\JsonApi\Document; |
||
32 | class Document implements DocumentInterface, LoggerAwareInterface |
||
33 | { |
||
34 | use LoggerAwareTrait; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $errors; |
||
40 | |||
41 | /** |
||
42 | * @var array|object|null |
||
43 | */ |
||
44 | private $meta; |
||
45 | |||
46 | /** |
||
47 | * @var array|null|string |
||
48 | */ |
||
49 | private $links; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $hasBeenMetAlready; |
||
55 | |||
56 | /** |
||
57 | * @var array|null |
||
58 | */ |
||
59 | private $included; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $includedIndex = 0; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | private $includedResources = []; |
||
70 | |||
71 | /** |
||
72 | * @var array|null |
||
73 | */ |
||
74 | private $version; |
||
75 | |||
76 | /** |
||
77 | * @var array|null |
||
78 | */ |
||
79 | private $data; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | private $bufferForData = []; |
||
85 | |||
86 | /** |
||
87 | * @var array |
||
88 | */ |
||
89 | private $bufferForIncluded = []; |
||
90 | |||
91 | /** |
||
92 | * If original data were in array. |
||
93 | * |
||
94 | * @var bool|null |
||
95 | */ |
||
96 | private $isDataArrayed; |
||
97 | |||
98 | /** |
||
99 | * @var ElementPresenter |
||
100 | */ |
||
101 | private $presenter; |
||
102 | |||
103 | /** |
||
104 | * @var bool |
||
105 | */ |
||
106 | private $showData = true; |
||
107 | |||
108 | /** |
||
109 | * @var string|null |
||
110 | */ |
||
111 | private $urlPrefix; |
||
112 | |||
113 | /** |
||
114 | * Constructor. |
||
115 | */ |
||
116 | 94 | public function __construct() |
|
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | 8 | public function setDocumentLinks($links) |
|
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | 6 | public function setMetaToDocument($meta) |
|
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 27 | public function addToIncluded(ResourceObjectInterface $resource) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 66 | public function addToData(ResourceObjectInterface $resource) |
|
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | 6 | public function setEmptyData() |
|
186 | { |
||
187 | 6 | $this->data = []; |
|
188 | 6 | } |
|
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | 3 | public function setNullData() |
|
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 30 | public function addRelationshipToData( |
|
208 | |||
209 | /** |
||
210 | * @inheritdoc |
||
211 | */ |
||
212 | 17 | public function addRelationshipToIncluded( |
|
219 | |||
220 | /** |
||
221 | * @inheritdoc |
||
222 | */ |
||
223 | 5 | public function addEmptyRelationshipToData( |
|
229 | |||
230 | /** |
||
231 | * @inheritdoc |
||
232 | */ |
||
233 | 9 | public function addNullRelationshipToData( |
|
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | 3 | public function addEmptyRelationshipToIncluded( |
|
249 | |||
250 | /** |
||
251 | * @inheritdoc |
||
252 | */ |
||
253 | 6 | public function addNullRelationshipToIncluded( |
|
259 | |||
260 | /** |
||
261 | * @inheritdoc |
||
262 | */ |
||
263 | 69 | public function setResourceCompleted(ResourceObjectInterface $resource) |
|
307 | |||
308 | /** |
||
309 | * @inheritdoc |
||
310 | */ |
||
311 | 89 | public function getDocument() |
|
336 | |||
337 | /** |
||
338 | * @inheritdoc |
||
339 | */ |
||
340 | 2 | public function addJsonApiVersion($version, $meta = null) |
|
345 | |||
346 | /** |
||
347 | * @inheritdoc |
||
348 | */ |
||
349 | 3 | public function unsetData() |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | 6 | public function addError(ErrorInterface $error) |
|
377 | |||
378 | /** |
||
379 | * @inheritdoc |
||
380 | */ |
||
381 | 3 | public function addErrors($errors) |
|
389 | |||
390 | /** |
||
391 | * @inheritdoc |
||
392 | */ |
||
393 | 45 | public function setUrlPrefix($prefix) |
|
397 | |||
398 | /** |
||
399 | * Get URL prefix. |
||
400 | * |
||
401 | * @return null|string |
||
402 | */ |
||
403 | 64 | public function getUrlPrefix() |
|
407 | } |
||
408 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.