Completed
Pull Request — master (#3329)
by Andrii
04:24
created

PropertyMetadata   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 418
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 107
c 0
b 0
f 0
dl 0
loc 418
rs 9.2
wmc 40

37 Methods

Rating   Name   Duplication   Size   Complexity  
A isRequired() 0 7 3
A hasSubresource() 0 3 1
A withChildInherited() 0 8 1
A getAttributes() 0 3 1
A isIdentifier() 0 3 1
A withIri() 0 6 1
A getSubresource() 0 3 1
A withSchema() 0 6 1
A isReadable() 0 3 1
A getType() 0 3 1
A getExample() 0 3 1
A getAttribute() 0 3 1
A isWritableLink() 0 3 1
A hasChildInherited() 0 3 1
A getSchema() 0 3 1
A withWritableLink() 0 6 1
A withDescription() 0 6 1
A __construct() 0 38 2
A isWritable() 0 3 1
A getIri() 0 3 1
A getDefault() 0 3 1
A withAttributes() 0 6 1
A getChildInherited() 0 3 1
A isInitializable() 0 3 1
A withSubresource() 0 6 1
A withIdentifier() 0 6 1
A withExample() 0 6 1
A withType() 0 6 1
A withRequired() 0 6 1
A isReadableLink() 0 3 1
A withWritable() 0 6 1
A isChildInherited() 0 5 1
A withReadable() 0 6 1
A withReadableLink() 0 6 1
A withInitializable() 0 6 1
A withDefault() 0 6 1
A getDescription() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Metadata\Property;
15
16
use Symfony\Component\PropertyInfo\Type;
17
18
/**
19
 * Property metadata.
20
 *
21
 * @author Kévin Dunglas <[email protected]>
22
 */
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
50
    /**
51
     * @var array
52
     */
53
    private $schema;
54
55
    public function __construct(
56
        Type $type = null,
57
        string $description = null,
58
        bool $readable = null,
59
        bool $writable = null,
60
        bool $readableLink = null,
61
        bool $writableLink = null,
62
        bool $required = null,
63
        bool $identifier = null,
64
        string $iri = null,
65
        $childInherited = null,
66
        array $attributes = null,
67
        SubresourceMetadata $subresource = null,
68
        bool $initializable = null,
69
        $default = null,
70
        $example = null,
71
        array $schema = null
72
    )
73
    {
74
        $this->type = $type;
75
        $this->description = $description;
76
        $this->readable = $readable;
77
        $this->writable = $writable;
78
        $this->readableLink = $readableLink;
79
        $this->writableLink = $writableLink;
80
        $this->required = $required;
81
        $this->identifier = $identifier;
82
        $this->iri = $iri;
83
        if (null !== $childInherited) {
84
            @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);
85
        }
86
        $this->childInherited = $childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

86
        /** @scrutinizer ignore-deprecated */ $this->childInherited = $childInherited;

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.

Loading history...
87
        $this->attributes = $attributes;
88
        $this->subresource = $subresource;
89
        $this->initializable = $initializable;
90
        $this->default = $default;
91
        $this->example = $example;
92
        $this->schema = $schema;
93
    }
94
95
    /**
96
     * Gets type.
97
     */
98
    public function getType(): ?Type
99
    {
100
        return $this->type;
101
    }
102
103
    /**
104
     * Returns a new instance with the given type.
105
     */
106
    public function withType(Type $type): self
107
    {
108
        $metadata = clone $this;
109
        $metadata->type = $type;
110
111
        return $metadata;
112
    }
113
114
    /**
115
     * Gets description.
116
     */
117
    public function getDescription(): ?string
118
    {
119
        return $this->description;
120
    }
121
122
    /**
123
     * Returns a new instance with the given description.
124
     */
125
    public function withDescription(string $description): self
126
    {
127
        $metadata = clone $this;
128
        $metadata->description = $description;
129
130
        return $metadata;
131
    }
132
133
    /**
134
     * Is readable?
135
     */
136
    public function isReadable(): ?bool
137
    {
138
        return $this->readable;
139
    }
140
141
    /**
142
     * Returns a new instance of Metadata with the given readable flag.
143
     */
144
    public function withReadable(bool $readable): self
145
    {
146
        $metadata = clone $this;
147
        $metadata->readable = $readable;
148
149
        return $metadata;
150
    }
151
152
    /**
153
     * Is writable?
154
     */
155
    public function isWritable(): ?bool
156
    {
157
        return $this->writable;
158
    }
159
160
    /**
161
     * Returns a new instance with the given writable flag.
162
     */
163
    public function withWritable(bool $writable): self
164
    {
165
        $metadata = clone $this;
166
        $metadata->writable = $writable;
167
168
        return $metadata;
169
    }
170
171
    /**
172
     * Is required?
173
     */
174
    public function isRequired(): ?bool
175
    {
176
        if (true === $this->required && false === $this->writable) {
177
            return false;
178
        }
179
180
        return $this->required;
181
    }
182
183
    /**
184
     * Returns a new instance with the given required flag.
185
     */
186
    public function withRequired(bool $required): self
187
    {
188
        $metadata = clone $this;
189
        $metadata->required = $required;
190
191
        return $metadata;
192
    }
193
194
    /**
195
     * Should an IRI or an object be provided in write context?
196
     */
197
    public function isWritableLink(): ?bool
198
    {
199
        return $this->writableLink;
200
    }
201
202
    /**
203
     * Returns a new instance with the given writable link flag.
204
     */
205
    public function withWritableLink(bool $writableLink): self
206
    {
207
        $metadata = clone $this;
208
        $metadata->writableLink = $writableLink;
209
210
        return $metadata;
211
    }
212
213
    /**
214
     * Is an IRI or an object generated in read context?
215
     */
216
    public function isReadableLink(): ?bool
217
    {
218
        return $this->readableLink;
219
    }
220
221
    /**
222
     * Returns a new instance with the given readable link flag.
223
     */
224
    public function withReadableLink(bool $readableLink): self
225
    {
226
        $metadata = clone $this;
227
        $metadata->readableLink = $readableLink;
228
229
        return $metadata;
230
    }
231
232
    /**
233
     * Gets IRI of this property.
234
     */
235
    public function getIri(): ?string
236
    {
237
        return $this->iri;
238
    }
239
240
    /**
241
     * Returns a new instance with the given IRI.
242
     */
243
    public function withIri(string $iri = null): self
244
    {
245
        $metadata = clone $this;
246
        $metadata->iri = $iri;
247
248
        return $metadata;
249
    }
250
251
    /**
252
     * Is this attribute an identifier?
253
     */
254
    public function isIdentifier(): ?bool
255
    {
256
        return $this->identifier;
257
    }
258
259
    /**
260
     * Returns a new instance with the given identifier flag.
261
     */
262
    public function withIdentifier(bool $identifier): self
263
    {
264
        $metadata = clone $this;
265
        $metadata->identifier = $identifier;
266
267
        return $metadata;
268
    }
269
270
    /**
271
     * Gets attributes.
272
     */
273
    public function getAttributes(): ?array
274
    {
275
        return $this->attributes;
276
    }
277
278
    /**
279
     * Gets an attribute.
280
     *
281
     * @param mixed|null $defaultValue
282
     */
283
    public function getAttribute(string $key, $defaultValue = null)
284
    {
285
        return $this->attributes[$key] ?? $defaultValue;
286
    }
287
288
    /**
289
     * Returns a new instance with the given attribute.
290
     */
291
    public function withAttributes(array $attributes): self
292
    {
293
        $metadata = clone $this;
294
        $metadata->attributes = $attributes;
295
296
        return $metadata;
297
    }
298
299
    /**
300
     * @deprecated since 2.6, to be removed in 3.0
301
     */
302
    public function getChildInherited(): ?string
303
    {
304
        return $this->childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

304
        return /** @scrutinizer ignore-deprecated */ $this->childInherited;

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.

Loading history...
305
    }
306
307
    /**
308
     * @deprecated since 2.6, to be removed in 3.0
309
     */
310
    public function hasChildInherited(): bool
311
    {
312
        return null !== $this->childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

312
        return null !== /** @scrutinizer ignore-deprecated */ $this->childInherited;

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.

Loading history...
313
    }
314
315
    /**
316
     * @deprecated since 2.4, to be removed in 3.0
317
     */
318
    public function isChildInherited(): ?string
319
    {
320
        @trigger_error(sprintf('"%s::%s" is deprecated since 2.4 and will be removed in 3.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
321
322
        return $this->getChildInherited();
0 ignored issues
show
Deprecated Code introduced by
The function ApiPlatform\Core\Metadat...ta::getChildInherited() has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

322
        return /** @scrutinizer ignore-deprecated */ $this->getChildInherited();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
323
    }
324
325
    /**
326
     * @deprecated since 2.6, to be removed in 3.0
327
     */
328
    public function withChildInherited(string $childInherited): self
329
    {
330
        @trigger_error(sprintf('"%s::%s" is deprecated since 2.6 and will be removed in 3.0.', __CLASS__, __METHOD__), E_USER_DEPRECATED);
331
332
        $metadata = clone $this;
333
        $metadata->childInherited = $childInherited;
0 ignored issues
show
Deprecated Code introduced by
The property ApiPlatform\Core\Metadat...tadata::$childInherited has been deprecated: since 2.6, to be removed in 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

333
        /** @scrutinizer ignore-deprecated */ $metadata->childInherited = $childInherited;

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.

Loading history...
334
335
        return $metadata;
336
    }
337
338
    /**
339
     * Represents whether the property has a subresource.
340
     */
341
    public function hasSubresource(): bool
342
    {
343
        return null !== $this->subresource;
344
    }
345
346
    /**
347
     * Gets the subresource metadata.
348
     */
349
    public function getSubresource(): ?SubresourceMetadata
350
    {
351
        return $this->subresource;
352
    }
353
354
    /**
355
     * Returns a new instance with the given subresource.
356
     *
357
     * @param SubresourceMetadata $subresource
358
     */
359
    public function withSubresource(SubresourceMetadata $subresource = null): self
360
    {
361
        $metadata = clone $this;
362
        $metadata->subresource = $subresource;
363
364
        return $metadata;
365
    }
366
367
    /**
368
     * Is initializable?
369
     */
370
    public function isInitializable(): ?bool
371
    {
372
        return $this->initializable;
373
    }
374
375
    /**
376
     * Returns a new instance with the given initializable flag.
377
     */
378
    public function withInitializable(bool $initializable): self
379
    {
380
        $metadata = clone $this;
381
        $metadata->initializable = $initializable;
382
383
        return $metadata;
384
    }
385
386
    /**
387
     * Returns the default value of the property or NULL if the property doesn't have a default value.
388
     */
389
    public function getDefault()
390
    {
391
        return $this->default;
392
    }
393
394
    /**
395
     * Returns a new instance with the given default value for the property.
396
     */
397
    public function withDefault($default): self
398
    {
399
        $metadata = clone $this;
400
        $metadata->default = $default;
401
402
        return $metadata;
403
    }
404
405
    /**
406
     * Returns an example of the value of the property.
407
     */
408
    public function getExample()
409
    {
410
        return $this->example;
411
    }
412
413
    /**
414
     * Returns a new instance with the given example.
415
     */
416
    public function withExample($example): self
417
    {
418
        $metadata = clone $this;
419
        $metadata->example = $example;
420
421
        return $metadata;
422
    }
423
424
    /**
425
     * @return array
426
     */
427
    public function getSchema(): ?array
428
    {
429
        return $this->schema;
430
    }
431
432
    /**
433
     * Returns a new instance with the given schema.
434
     */
435
    public function withSchema(array $schema = null): self
436
    {
437
        $metadata = clone $this;
438
        $metadata->schema = $schema;
439
440
        return $metadata;
441
    }
442
}
443