Passed
Pull Request — master (#1366)
by Asmir
09:41
created

PropertyMetadata::setAccessor()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 13.1112

Importance

Changes 0
Metric Value
cc 13
eloc 18
c 0
b 0
f 0
nc 14
nop 3
dl 0
loc 28
ccs 21
cts 23
cp 0.913
crap 13.1112
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Metadata\PropertyMetadata as BasePropertyMetadata;
10
11
class PropertyMetadata extends BasePropertyMetadata
12
{
13
    public const ACCESS_TYPE_PROPERTY = 'property';
14
    public const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
15
16
    /**
17
     * @var string
18
     */
19
    public $sinceVersion;
20
    /**
21
     * @var string
22
     */
23
    public $untilVersion;
24
    /**
25
     * @var string[]
26
     */
27
    public $groups;
28
    /**
29
     * @var string
30
     */
31
    public $serializedName;
32
    /**
33
     * @var array
34
     */
35
    public $type;
36
37
    /**
38
     * @var bool
39
     */
40
    public $xmlCollection = false;
41
42
    /**
43
     * @var bool
44
     */
45
    public $xmlCollectionInline = false;
46 280
47
    /**
48 280
     * @var bool
49
     */
50
    public $xmlCollectionSkipWhenEmpty = true;
51 280
52 280
    /**
53
     * @var string
54
     */
55 280
    public $xmlEntryName;
56
57 280
    /**
58
     * @var string
59 280
     */
60
    public $xmlEntryNamespace;
61
62 260
    /**
63
     * @var string
64 260
     */
65 14
    public $xmlKeyAttribute;
66
67 14
    /**
68 10
     * @var bool
69 6
     */
70 4
    public $xmlAttribute = false;
71 1
72 3
    /**
73 1
     * @var bool
74
     */
75 2
    public $xmlValue = false;
76
77
    /**
78
     * @var string
79 12
     */
80 6
    public $xmlNamespace;
81 5
82
    /**
83 1
     * @var bool
84
     */
85
    public $xmlKeyValuePairs = false;
86
87
    /**
88 257
     * @var bool
89 257
     */
90 257
    public $xmlElementCData = true;
91
92 217
    /**
93
     * @var string
94 217
     */
95 217
    public $getter;
96
97 17
    /**
98
     * @var string
99 17
     */
100 17
    public $setter;
101 17
102 17
    /**
103
     * @var bool
104
     */
105 17
    public $inline = false;
106
107 17
    /**
108 17
     * @var bool
109 17
     */
110 17
    public $skipWhenEmpty = false;
111
112
    /**
113 1
     * @var bool
114
     */
115 1
    public $readOnly = false;
116 1
117 1
    /**
118 1
     * @var bool
119 1
     */
120 1
    public $xmlAttributeMap = false;
121 1
122 1
    /**
123 1
     * @var int|null
124 1
     */
125 1
    public $maxDepth = null;
126 1
127 1
    /**
128 1
     * @var string|Expression
129 1
     */
130 1
    public $excludeIf = null;
131 1
132 1
    /**
133 1
     * @internal
134 1
     *
135 1
     * @var bool
136 1
     */
137 1
    public $forceReflectionAccess = false;
138 1
139 1
    public function __construct(string $class, string $name)
140 1
    {
141 1
        parent::__construct($class, $name);
142
143
        try {
144
            $class = $this->getReflection()->getDeclaringClass();
145 1
            $this->forceReflectionAccess = $class->isInternal() || $class->getProperty($name)->isStatic();
146
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
147 1
        }
148
    }
149 1
150 1
    private function getReflection(): \ReflectionProperty
151 1
    {
152 1
        return new \ReflectionProperty($this->class, $this->name);
153 1
    }
154 1
155 1
    public function setAccessor(string $type, ?string $getter = null, ?string $setter = null): void
156 1
    {
157 1
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
158 1
            $class = $this->getReflection()->getDeclaringClass();
159 1
160 1
            if (empty($getter)) {
161 1
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
162 1
                    $getter = 'get' . $this->name;
163 1
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
164 1
                    $getter = 'is' . $this->name;
165 1
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
166 1
                    $getter = 'has' . $this->name;
167 1
                } else {
168 1
                    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));
169
                }
170 1
            }
171
172 1
            if (empty($setter) && !$this->readOnly) {
173
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
174
                    $setter = 'set' . $this->name;
175 1
                } else {
176 1
                    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));
177
                }
178 1
            }
179
        }
180
181 1
        $this->getter = $getter;
182 1
        $this->setter = $setter;
183
    }
184 1
185 1
    public function setType(array $type): void
186
    {
187
        $this->type = $type;
188 1
    }
189 1
190
    public static function isCollectionList(?array $type = null): bool
191
    {
192
        return is_array($type)
193
            && 'array' === $type['name']
194
            && isset($type['params'][0])
195
            && !isset($type['params'][1]);
196
    }
197
198
    public static function isCollectionMap(?array $type = null): bool
199
    {
200
        return is_array($type)
201
            && 'array' === $type['name']
202
            && isset($type['params'][0])
203
            && isset($type['params'][1]);
204
    }
205
206
    protected function serializeToArray(): array
207
    {
208
        return [
209
            $this->sinceVersion,
210
            $this->untilVersion,
211
            $this->groups,
212
            $this->serializedName,
213
            $this->type,
214
            $this->xmlCollection,
215
            $this->xmlCollectionInline,
216
            $this->xmlEntryName,
217
            $this->xmlKeyAttribute,
218
            $this->xmlAttribute,
219
            $this->xmlValue,
220
            $this->xmlNamespace,
221
            $this->xmlKeyValuePairs,
222
            $this->xmlElementCData,
223
            $this->getter,
224
            $this->setter,
225
            $this->inline,
226
            $this->readOnly,
227
            $this->xmlAttributeMap,
228
            $this->maxDepth,
229
            $this->xmlEntryNamespace,
230
            $this->xmlCollectionSkipWhenEmpty,
231
            $this->excludeIf,
232
            $this->skipWhenEmpty,
233
            $this->forceReflectionAccess,
234
            parent::serializeToArray(),
235
        ];
236
    }
237
238
    protected function unserializeFromArray(array $data): void
239
    {
240
        [
241
            $this->sinceVersion,
242
            $this->untilVersion,
243
            $this->groups,
244
            $this->serializedName,
245
            $this->type,
246
            $this->xmlCollection,
247
            $this->xmlCollectionInline,
248
            $this->xmlEntryName,
249
            $this->xmlKeyAttribute,
250
            $this->xmlAttribute,
251
            $this->xmlValue,
252
            $this->xmlNamespace,
253
            $this->xmlKeyValuePairs,
254
            $this->xmlElementCData,
255
            $this->getter,
256
            $this->setter,
257
            $this->inline,
258
            $this->readOnly,
259
            $this->xmlAttributeMap,
260
            $this->maxDepth,
261
            $this->xmlEntryNamespace,
262
            $this->xmlCollectionSkipWhenEmpty,
263
            $this->excludeIf,
264
            $this->skipWhenEmpty,
265
            $this->forceReflectionAccess,
266
            $parentData,
267
        ] = $data;
268
269
        parent::unserializeFromArray($parentData);
270
    }
271
}
272