Total Complexity | 40 |
Total Lines | 397 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PropertyMetadata 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 PropertyMetadata, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | final class PropertyMetadata |
||
24 | { |
||
25 | private $type; |
||
26 | private $description; |
||
27 | private $readable; |
||
28 | private $writable; |
||
29 | private $readableLink; |
||
30 | private $writableLink; |
||
31 | private $required; |
||
32 | private $iri; |
||
33 | private $identifier; |
||
34 | /** |
||
35 | * @deprecated since 2.6, to be removed in 3.0 |
||
36 | */ |
||
37 | private $childInherited; |
||
38 | private $attributes; |
||
39 | private $subresource; |
||
40 | private $initializable; |
||
41 | /** |
||
42 | * @var null |
||
43 | */ |
||
44 | private $default; |
||
45 | /** |
||
46 | * @var null |
||
47 | */ |
||
48 | private $example; |
||
49 | private $schema; |
||
50 | |||
51 | public function __construct(Type $type = null, string $description = null, bool $readable = null, bool $writable = null, bool $readableLink = null, bool $writableLink = null, bool $required = null, bool $identifier = null, string $iri = null, $childInherited = null, array $attributes = null, SubresourceMetadata $subresource = null, bool $initializable = null, $default = null, $example = null, array $schema = null) |
||
52 | { |
||
53 | $this->type = $type; |
||
54 | $this->description = $description; |
||
55 | $this->readable = $readable; |
||
56 | $this->writable = $writable; |
||
57 | $this->readableLink = $readableLink; |
||
58 | $this->writableLink = $writableLink; |
||
59 | $this->required = $required; |
||
60 | $this->identifier = $identifier; |
||
61 | $this->iri = $iri; |
||
62 | if (null !== $childInherited) { |
||
63 | @trigger_error(sprintf('Providing a non-null value for the 10th argument ($childInherited) of the "%s" constructor is deprecated since 2.6 and will not be supported in 3.0.', __CLASS__), E_USER_DEPRECATED); |
||
64 | } |
||
65 | $this->childInherited = $childInherited; |
||
|
|||
66 | $this->attributes = $attributes; |
||
67 | $this->subresource = $subresource; |
||
68 | $this->initializable = $initializable; |
||
69 | $this->default = $default; |
||
70 | $this->example = $example; |
||
71 | $this->schema = $schema; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Gets type. |
||
76 | */ |
||
77 | public function getType(): ?Type |
||
78 | { |
||
79 | return $this->type; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns a new instance with the given type. |
||
84 | */ |
||
85 | public function withType(Type $type): self |
||
86 | { |
||
87 | $metadata = clone $this; |
||
88 | $metadata->type = $type; |
||
89 | |||
90 | return $metadata; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Gets description. |
||
95 | */ |
||
96 | public function getDescription(): ?string |
||
97 | { |
||
98 | return $this->description; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Returns a new instance with the given description. |
||
103 | */ |
||
104 | public function withDescription(string $description): self |
||
105 | { |
||
106 | $metadata = clone $this; |
||
107 | $metadata->description = $description; |
||
108 | |||
109 | return $metadata; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Is readable? |
||
114 | */ |
||
115 | public function isReadable(): ?bool |
||
116 | { |
||
117 | return $this->readable; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns a new instance of Metadata with the given readable flag. |
||
122 | */ |
||
123 | public function withReadable(bool $readable): self |
||
124 | { |
||
125 | $metadata = clone $this; |
||
126 | $metadata->readable = $readable; |
||
127 | |||
128 | return $metadata; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Is writable? |
||
133 | */ |
||
134 | public function isWritable(): ?bool |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Returns a new instance with the given writable flag. |
||
141 | */ |
||
142 | public function withWritable(bool $writable): self |
||
143 | { |
||
144 | $metadata = clone $this; |
||
145 | $metadata->writable = $writable; |
||
146 | |||
147 | return $metadata; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Is required? |
||
152 | */ |
||
153 | public function isRequired(): ?bool |
||
154 | { |
||
155 | if (true === $this->required && false === $this->writable) { |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | return $this->required; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Returns a new instance with the given required flag. |
||
164 | */ |
||
165 | public function withRequired(bool $required): self |
||
166 | { |
||
167 | $metadata = clone $this; |
||
168 | $metadata->required = $required; |
||
169 | |||
170 | return $metadata; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Should an IRI or an object be provided in write context? |
||
175 | */ |
||
176 | public function isWritableLink(): ?bool |
||
177 | { |
||
178 | return $this->writableLink; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Returns a new instance with the given writable link flag. |
||
183 | */ |
||
184 | public function withWritableLink(bool $writableLink): self |
||
185 | { |
||
186 | $metadata = clone $this; |
||
187 | $metadata->writableLink = $writableLink; |
||
188 | |||
189 | return $metadata; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Is an IRI or an object generated in read context? |
||
194 | */ |
||
195 | public function isReadableLink(): ?bool |
||
196 | { |
||
197 | return $this->readableLink; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Returns a new instance with the given readable link flag. |
||
202 | */ |
||
203 | public function withReadableLink(bool $readableLink): self |
||
204 | { |
||
205 | $metadata = clone $this; |
||
206 | $metadata->readableLink = $readableLink; |
||
207 | |||
208 | return $metadata; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Gets IRI of this property. |
||
213 | */ |
||
214 | public function getIri(): ?string |
||
215 | { |
||
216 | return $this->iri; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Returns a new instance with the given IRI. |
||
221 | */ |
||
222 | public function withIri(string $iri = null): self |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Is this attribute an identifier? |
||
232 | */ |
||
233 | public function isIdentifier(): ?bool |
||
234 | { |
||
235 | return $this->identifier; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Returns a new instance with the given identifier flag. |
||
240 | */ |
||
241 | public function withIdentifier(bool $identifier): self |
||
242 | { |
||
243 | $metadata = clone $this; |
||
244 | $metadata->identifier = $identifier; |
||
245 | |||
246 | return $metadata; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Gets attributes. |
||
251 | */ |
||
252 | public function getAttributes(): ?array |
||
253 | { |
||
254 | return $this->attributes; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Gets an attribute. |
||
259 | * |
||
260 | * @param mixed|null $defaultValue |
||
261 | */ |
||
262 | public function getAttribute(string $key, $defaultValue = null) |
||
263 | { |
||
264 | return $this->attributes[$key] ?? $defaultValue; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Returns a new instance with the given attribute. |
||
269 | */ |
||
270 | public function withAttributes(array $attributes): self |
||
271 | { |
||
272 | $metadata = clone $this; |
||
273 | $metadata->attributes = $attributes; |
||
274 | |||
275 | return $metadata; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @deprecated since 2.6, to be removed in 3.0 |
||
280 | */ |
||
281 | public function getChildInherited(): ?string |
||
282 | { |
||
283 | return $this->childInherited; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @deprecated since 2.6, to be removed in 3.0 |
||
288 | */ |
||
289 | public function hasChildInherited(): bool |
||
290 | { |
||
291 | return null !== $this->childInherited; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @deprecated since 2.4, to be removed in 3.0 |
||
296 | */ |
||
297 | public function isChildInherited(): ?string |
||
298 | { |
||
299 | @trigger_error(sprintf('"%s::%s" is deprecated since 2.4 and will be removed in 3.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED); |
||
300 | |||
301 | return $this->getChildInherited(); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @deprecated since 2.6, to be removed in 3.0 |
||
306 | */ |
||
307 | public function withChildInherited(string $childInherited): self |
||
308 | { |
||
309 | @trigger_error(sprintf('"%s::%s" is deprecated since 2.6 and will be removed in 3.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED); |
||
310 | |||
311 | $metadata = clone $this; |
||
312 | $metadata->childInherited = $childInherited; |
||
313 | |||
314 | return $metadata; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Represents whether the property has a subresource. |
||
319 | */ |
||
320 | public function hasSubresource(): bool |
||
321 | { |
||
322 | return null !== $this->subresource; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Gets the subresource metadata. |
||
327 | */ |
||
328 | public function getSubresource(): ?SubresourceMetadata |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Returns a new instance with the given subresource. |
||
335 | * |
||
336 | * @param SubresourceMetadata $subresource |
||
337 | */ |
||
338 | public function withSubresource(SubresourceMetadata $subresource = null): self |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Is initializable? |
||
348 | */ |
||
349 | public function isInitializable(): ?bool |
||
350 | { |
||
351 | return $this->initializable; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Returns a new instance with the given initializable flag. |
||
356 | */ |
||
357 | public function withInitializable(bool $initializable): self |
||
358 | { |
||
359 | $metadata = clone $this; |
||
360 | $metadata->initializable = $initializable; |
||
361 | |||
362 | return $metadata; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Returns the default value of the property or NULL if the property doesn't have a default value. |
||
367 | */ |
||
368 | public function getDefault() |
||
369 | { |
||
370 | return $this->default; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Returns a new instance with the given default value for the property. |
||
375 | */ |
||
376 | public function withDefault($default): self |
||
377 | { |
||
378 | $metadata = clone $this; |
||
379 | $metadata->default = $default; |
||
380 | |||
381 | return $metadata; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Returns an example of the value of the property. |
||
386 | */ |
||
387 | public function getExample() |
||
388 | { |
||
389 | return $this->example; |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Returns a new instance with the given example. |
||
394 | */ |
||
395 | public function withExample($example): self |
||
396 | { |
||
397 | $metadata = clone $this; |
||
398 | $metadata->example = $example; |
||
399 | |||
400 | return $metadata; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | public function getSchema(): ?array |
||
407 | { |
||
408 | return $this->schema; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Returns a new instance with the given schema. |
||
413 | */ |
||
414 | public function withSchema(array $schema = null): self |
||
420 | } |
||
421 | } |
||
422 |
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.