Total Complexity | 40 |
Total Lines | 324 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResourceMetadata 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.
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 ResourceMetadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | final class ResourceMetadata |
||
24 | { |
||
25 | private $shortName; |
||
26 | private $description; |
||
27 | private $iri; |
||
28 | private $itemOperations; |
||
29 | private $collectionOperations; |
||
30 | private $subresourceOperations; |
||
31 | private $graphql; |
||
32 | private $attributes; |
||
33 | |||
34 | public function __construct(string $shortName = null, string $description = null, string $iri = null, array $itemOperations = null, array $collectionOperations = null, array $attributes = null, array $subresourceOperations = null, array $graphql = null) |
||
35 | { |
||
36 | $this->shortName = $shortName; |
||
37 | $this->description = $description; |
||
38 | $this->iri = $iri; |
||
39 | $this->itemOperations = $itemOperations; |
||
40 | $this->collectionOperations = $collectionOperations; |
||
41 | $this->subresourceOperations = $subresourceOperations; |
||
42 | $this->graphql = $graphql; |
||
43 | $this->attributes = $attributes; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Gets the short name. |
||
48 | * |
||
49 | * @return string|null |
||
50 | */ |
||
51 | public function getShortName() |
||
52 | { |
||
53 | return $this->shortName; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns a new instance with the given short name. |
||
58 | */ |
||
59 | public function withShortName(string $shortName): self |
||
60 | { |
||
61 | $metadata = clone $this; |
||
62 | $metadata->shortName = $shortName; |
||
63 | |||
64 | return $metadata; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Gets the description. |
||
69 | * |
||
70 | * @return string|null |
||
71 | */ |
||
72 | public function getDescription() |
||
73 | { |
||
74 | return $this->description; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns a new instance with the given description. |
||
79 | */ |
||
80 | public function withDescription(string $description): self |
||
81 | { |
||
82 | $metadata = clone $this; |
||
83 | $metadata->description = $description; |
||
84 | |||
85 | return $metadata; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Gets the associated IRI. |
||
90 | * |
||
91 | * @return string|null |
||
92 | */ |
||
93 | public function getIri() |
||
94 | { |
||
95 | return $this->iri; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns a new instance with the given IRI. |
||
100 | */ |
||
101 | public function withIri(string $iri): self |
||
102 | { |
||
103 | $metadata = clone $this; |
||
104 | $metadata->iri = $iri; |
||
105 | |||
106 | return $metadata; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Gets item operations. |
||
111 | * |
||
112 | * @return array|null |
||
113 | */ |
||
114 | public function getItemOperations() |
||
115 | { |
||
116 | return $this->itemOperations; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns a new instance with the given item operations. |
||
121 | */ |
||
122 | public function withItemOperations(array $itemOperations): self |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Gets collection operations. |
||
132 | * |
||
133 | * @return array|null |
||
134 | */ |
||
135 | public function getCollectionOperations() |
||
136 | { |
||
137 | return $this->collectionOperations; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Returns a new instance with the given collection operations. |
||
142 | */ |
||
143 | public function withCollectionOperations(array $collectionOperations): self |
||
144 | { |
||
145 | $metadata = clone $this; |
||
146 | $metadata->collectionOperations = $collectionOperations; |
||
147 | |||
148 | return $metadata; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Gets subresource operations. |
||
153 | * |
||
154 | * @return array|null |
||
155 | */ |
||
156 | public function getSubresourceOperations() |
||
157 | { |
||
158 | return $this->subresourceOperations; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns a new instance with the given subresource operations. |
||
163 | */ |
||
164 | public function withSubresourceOperations(array $subresourceOperations): self |
||
165 | { |
||
166 | $metadata = clone $this; |
||
167 | $metadata->subresourceOperations = $subresourceOperations; |
||
168 | |||
169 | return $metadata; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Gets a collection operation attribute, optionally fallback to a resource attribute. |
||
174 | * |
||
175 | * @param mixed $defaultValue |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function getCollectionOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Gets an item operation attribute, optionally fallback to a resource attribute. |
||
186 | * |
||
187 | * @param mixed $defaultValue |
||
188 | * |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function getItemOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Gets a subresource operation attribute, optionally fallback to a resource attribute. |
||
198 | * |
||
199 | * @param mixed $defaultValue |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | public function getSubresourceOperationAttribute(string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
204 | { |
||
205 | return $this->findOperationAttribute($this->subresourceOperations, $operationName, $key, $defaultValue, $resourceFallback); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Gets an operation attribute, optionally fallback to a resource attribute. |
||
210 | * |
||
211 | * @param mixed $defaultValue |
||
212 | * |
||
213 | * @return mixed |
||
214 | */ |
||
215 | private function findOperationAttribute(array $operations = null, string $operationName = null, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public function getGraphqlAttribute(string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Gets the first available operation attribute according to the following order: collection, item, subresource, optionally fallback to a default value. |
||
246 | * |
||
247 | * @param mixed $defaultValue |
||
248 | * |
||
249 | * @return mixed |
||
250 | */ |
||
251 | public function getOperationAttribute(array $attributes, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
252 | { |
||
253 | if (isset($attributes['collection_operation_name'])) { |
||
254 | return $this->getCollectionOperationAttribute($attributes['collection_operation_name'], $key, $defaultValue, $resourceFallback); |
||
255 | } |
||
256 | |||
257 | if (isset($attributes['item_operation_name'])) { |
||
258 | return $this->getItemOperationAttribute($attributes['item_operation_name'], $key, $defaultValue, $resourceFallback); |
||
259 | } |
||
260 | |||
261 | if (isset($attributes['subresource_operation_name'])) { |
||
262 | return $this->getSubresourceOperationAttribute($attributes['subresource_operation_name'], $key, $defaultValue, $resourceFallback); |
||
263 | } |
||
264 | |||
265 | if ($resourceFallback && isset($this->attributes[$key])) { |
||
266 | return $this->attributes[$key]; |
||
267 | } |
||
268 | |||
269 | return $defaultValue; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Gets an attribute for a given operation type and operation name. |
||
274 | * |
||
275 | * @param mixed $defaultValue |
||
276 | * |
||
277 | * @return mixed |
||
278 | */ |
||
279 | public function getTypedOperationAttribute(string $operationType, string $operationName, string $key, $defaultValue = null, bool $resourceFallback = false) |
||
280 | { |
||
281 | switch ($operationType) { |
||
282 | case OperationType::COLLECTION: |
||
283 | return $this->getCollectionOperationAttribute($operationName, $key, $defaultValue, $resourceFallback); |
||
284 | case OperationType::ITEM: |
||
285 | return $this->getItemOperationAttribute($operationName, $key, $defaultValue, $resourceFallback); |
||
286 | default: |
||
287 | return $this->getSubresourceOperationAttribute($operationName, $key, $defaultValue, $resourceFallback); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Gets attributes. |
||
293 | * |
||
294 | * @return array|null |
||
295 | */ |
||
296 | public function getAttributes() |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Gets an attribute. |
||
303 | * |
||
304 | * @param mixed $defaultValue |
||
305 | * |
||
306 | * @return mixed |
||
307 | */ |
||
308 | public function getAttribute(string $key, $defaultValue = null) |
||
309 | { |
||
310 | if (isset($this->attributes[$key])) { |
||
311 | return $this->attributes[$key]; |
||
312 | } |
||
313 | |||
314 | return $defaultValue; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Returns a new instance with the given attribute. |
||
319 | */ |
||
320 | public function withAttributes(array $attributes): self |
||
321 | { |
||
322 | $metadata = clone $this; |
||
323 | $metadata->attributes = $attributes; |
||
324 | |||
325 | return $metadata; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Gets options of for the GraphQL query. |
||
330 | * |
||
331 | * @return array|null |
||
332 | */ |
||
333 | public function getGraphql() |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Returns a new instance with the given GraphQL options. |
||
340 | */ |
||
341 | public function withGraphql(array $graphql): self |
||
347 | } |
||
348 | } |
||
349 |