PropertyMetadata::isCollectionList()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Metadata;
6
7
use JMS\Serializer\Exception\InvalidMetadataException;
8
use JMS\Serializer\Expression\Expression;
9
use JMS\Serializer\Type\Type;
10
use Metadata\PropertyMetadata as BasePropertyMetadata;
11
12
/**
13
 * @phpstan-import-type TypeArray from Type
14
 */
15
class PropertyMetadata extends BasePropertyMetadata
16
{
17
    public const ACCESS_TYPE_PROPERTY = 'property';
18
    public const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
19
20
    /**
21
     * @var string|null
22
     */
23
    public $sinceVersion;
24
25
    /**
26
     * @var string|null
27
     */
28
    public $untilVersion;
29
30
    /**
31
     * @var string[]|null
32
     */
33
    public $groups;
34
35
    /**
36
     * @var string|null
37
     */
38
    public $serializedName;
39
40
    /**
41
     * @var TypeArray|null
0 ignored issues
show
Bug introduced by
The type JMS\Serializer\Metadata\TypeArray was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
     */
43
    public $type;
44
45
    /**
46 280
     * @var bool
47
     */
48 280
    public $xmlCollection = false;
49
50
    /**
51 280
     * @var bool
52 280
     */
53
    public $xmlCollectionInline = false;
54
55 280
    /**
56
     * @var bool
57 280
     */
58
    public $xmlCollectionSkipWhenEmpty = true;
59 280
60
    /**
61
     * @var string|null
62 260
     */
63
    public $xmlEntryName;
64 260
65 14
    /**
66
     * @var string|null
67 14
     */
68 10
    public $xmlEntryNamespace;
69 6
70 4
    /**
71 1
     * @var string|null
72 3
     */
73 1
    public $xmlKeyAttribute;
74
75 2
    /**
76
     * @var bool
77
     */
78
    public $xmlAttribute = false;
79 12
80 6
    /**
81 5
     * @var bool
82
     */
83 1
    public $xmlValue = false;
84
85
    /**
86
     * @var string|null
87
     */
88 257
    public $xmlNamespace;
89 257
90 257
    /**
91
     * @var bool
92 217
     */
93
    public $xmlKeyValuePairs = false;
94 217
95 217
    /**
96
     * @var bool
97 17
     */
98
    public $xmlElementCData = true;
99 17
100 17
    /**
101 17
     * @var string|null
102 17
     */
103
    public $getter;
104
105 17
    /**
106
     * @var string|null
107 17
     */
108 17
    public $setter;
109 17
110 17
    /**
111
     * @var bool
112
     */
113 1
    public $inline = false;
114
115 1
    /**
116 1
     * @var bool
117 1
     */
118 1
    public $skipWhenEmpty = false;
119 1
120 1
    /**
121 1
     * @var bool
122 1
     */
123 1
    public $readOnly = false;
124 1
125 1
    /**
126 1
     * @var bool
127 1
     */
128 1
    public $xmlAttributeMap = false;
129 1
130 1
    /**
131 1
     * @var int|null
132 1
     */
133 1
    public $maxDepth = null;
134 1
135 1
    /**
136 1
     * @var string|Expression|null
137 1
     */
138 1
    public $excludeIf = null;
139 1
140 1
    /**
141 1
     * @var bool|null
142
     */
143
    public $hasDefault;
144
145 1
    /**
146
     * @var mixed|null
147 1
     */
148
    public $defaultValue;
149 1
150 1
    /**
151 1
     * @internal
152 1
     *
153 1
     * @var bool
154 1
     */
155 1
    public $forceReflectionAccess = false;
156 1
157 1
    public function __construct(string $class, string $name)
158 1
    {
159 1
        parent::__construct($class, $name);
160 1
161 1
        try {
162 1
            $class = $this->getReflection()->getDeclaringClass();
163 1
            $this->forceReflectionAccess = $class->isInternal() || $class->getProperty($name)->isStatic();
164 1
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
165 1
        }
166 1
    }
167 1
168 1
    private function getReflection(): \ReflectionProperty
169
    {
170 1
        return new \ReflectionProperty($this->class, $this->name);
171
    }
172 1
173
    public function setAccessor(string $type, ?string $getter = null, ?string $setter = null): void
174
    {
175 1
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
176 1
            $class = $this->getReflection()->getDeclaringClass();
177
178 1
            if (empty($getter)) {
179
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
180
                    $getter = 'get' . $this->name;
181 1
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
182 1
                    $getter = 'is' . $this->name;
183
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
184 1
                    $getter = 'has' . $this->name;
185 1
                } else {
186
                    throw new InvalidMetadataException(sprintf('There is neither a public %s method, nor a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get' . ucfirst($this->name), 'is' . ucfirst($this->name), 'has' . ucfirst($this->name), $this->class, $this->name));
187
                }
188 1
            }
189 1
190
            if (empty($setter) && !$this->readOnly) {
191
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
192
                    $setter = 'set' . $this->name;
193
                } else {
194
                    throw new InvalidMetadataException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set' . ucfirst($this->name), $this->class, $this->name));
195
                }
196
            }
197
        }
198
199
        $this->getter = $getter;
200
        $this->setter = $setter;
201
    }
202
203
    /**
204
     * @param TypeArray $type
205
     */
206
    public function setType(array $type): void
207
    {
208
        $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type of type array is incompatible with the declared type JMS\Serializer\Metadata\TypeArray|null of property $type.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
209
    }
210
211
    /**
212
     * @param TypeArray|null $type
213
     */
214
    public static function isCollectionList(?array $type = null): bool
215
    {
216
        return is_array($type)
217
            && 'array' === $type['name']
218
            && isset($type['params'][0])
219
            && !isset($type['params'][1]);
220
    }
221
222
    /**
223
     * @param TypeArray|null $type
224
     */
225
    public static function isCollectionMap(?array $type = null): bool
226
    {
227
        return is_array($type)
228
            && 'array' === $type['name']
229
            && isset($type['params'][0])
230
            && isset($type['params'][1]);
231
    }
232
233
    protected function serializeToArray(): array
234
    {
235
        return [
236
            $this->sinceVersion,
237
            $this->untilVersion,
238
            $this->groups,
239
            $this->serializedName,
240
            $this->type,
241
            $this->xmlCollection,
242
            $this->xmlCollectionInline,
243
            $this->xmlEntryName,
244
            $this->xmlKeyAttribute,
245
            $this->xmlAttribute,
246
            $this->xmlValue,
247
            $this->xmlNamespace,
248
            $this->xmlKeyValuePairs,
249
            $this->xmlElementCData,
250
            $this->getter,
251
            $this->setter,
252
            $this->inline,
253
            $this->readOnly,
254
            $this->xmlAttributeMap,
255
            $this->maxDepth,
256
            $this->xmlEntryNamespace,
257
            $this->xmlCollectionSkipWhenEmpty,
258
            $this->excludeIf,
259
            $this->skipWhenEmpty,
260
            $this->forceReflectionAccess,
261
            $this->hasDefault,
262
            $this->defaultValue,
263
            parent::serializeToArray(),
264
        ];
265
    }
266
267
    protected function unserializeFromArray(array $data): void
268
    {
269
        [
270
            $this->sinceVersion,
271
            $this->untilVersion,
272
            $this->groups,
273
            $this->serializedName,
274
            $this->type,
275
            $this->xmlCollection,
276
            $this->xmlCollectionInline,
277
            $this->xmlEntryName,
278
            $this->xmlKeyAttribute,
279
            $this->xmlAttribute,
280
            $this->xmlValue,
281
            $this->xmlNamespace,
282
            $this->xmlKeyValuePairs,
283
            $this->xmlElementCData,
284
            $this->getter,
285
            $this->setter,
286
            $this->inline,
287
            $this->readOnly,
288
            $this->xmlAttributeMap,
289
            $this->maxDepth,
290
            $this->xmlEntryNamespace,
291
            $this->xmlCollectionSkipWhenEmpty,
292
            $this->excludeIf,
293
            $this->skipWhenEmpty,
294
            $this->forceReflectionAccess,
295
            $this->hasDefault,
296
            $this->defaultValue,
297
            $parentData,
298
        ] = $data;
299
300
        parent::unserializeFromArray($parentData);
301
    }
302
}
303