Passed
Branch master (f07ed3)
by Nate
02:39
created

Property::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 9
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Internal\Data;
10
11
use Tebru\AnnotationReader\AbstractAnnotation;
12
use Tebru\AnnotationReader\AnnotationCollection;
13
use Tebru\Gson\ClassMetadata;
14
use Tebru\Gson\Internal\GetterStrategy;
15
use Tebru\Gson\Internal\SetterStrategy;
16
use Tebru\Gson\PropertyMetadata;
17
use Tebru\PhpType\TypeToken;
18
19
/**
20
 * Class Property
21
 *
22
 * Represents static information about an object property.  Instances of this class may be
23
 * cached for later use.
24
 *
25
 * @author Nate Brunette <[email protected]>
26
 */
27
final class Property implements PropertyMetadata
28
{
29
    /**
30
     * The actual name of the property
31
     *
32
     * @var string
33
     */
34
    private $realName;
35
36
    /**
37
     * The serialized version of the property name
38
     *
39
     * @var string
40
     */
41
    private $serializedName;
42
43
    /**
44
     * The property type
45
     *
46
     * @var TypeToken
47
     */
48
    private $type;
49
50
    /**
51
     * The method for getting values from this property
52
     *
53
     * @var GetterStrategy
54
     */
55
    private $getterStrategy;
56
57
    /**
58
     * The method for setting values to this property
59
     *
60
     * @var SetterStrategy
61
     */
62
    private $setterStrategy;
63
64
    /**
65
     * A set of annotations
66
     *
67
     * @var AnnotationCollection
68
     */
69
    private $annotations;
70
71
    /**
72
     * An integer that represents what modifiers are associated with the property
73
     *
74
     * These constants are defined in [@see \ReflectionProperty]
75
     *
76
     * @var int
77
     */
78
    private $modifiers;
79
80
    /**
81
     * The property's class metadata
82
     *
83
     * @var ClassMetadata
84
     */
85
    private $classMetadata;
86
87
    /**
88
     * True if the property should be skipped during serialization
89
     *
90
     * @var bool
91
     */
92
    private $skipSerialize = false;
93
94
    /**
95
     * True if the property should be skipped during deserialization
96
     *
97
     * @var bool
98
     */
99
    private $skipDeserialize = false;
100
101
    /**
102
     * If the property is a virtual property
103
     * @var bool
104
     */
105
    private $virtual;
106
107
    /**
108
     * Constructor
109
     *
110
     * @param string $realName
111
     * @param string $serializedName
112
     * @param TypeToken $type
113
     * @param GetterStrategy $getterStrategy
114
     * @param SetterStrategy $setterStrategy
115
     * @param AnnotationCollection $annotations
116
     * @param int $modifiers
117
     * @param bool $virtual
118
     * @param ClassMetadata $classMetadata
119
     */
120 12
    public function __construct(
121
        string $realName,
122
        string $serializedName,
123
        TypeToken $type,
124
        GetterStrategy $getterStrategy,
125
        SetterStrategy $setterStrategy,
126
        AnnotationCollection $annotations,
127
        int $modifiers,
128
        bool $virtual,
129
        ClassMetadata $classMetadata
130
    ) {
131 12
        $this->realName = $realName;
132 12
        $this->serializedName = $serializedName;
133 12
        $this->type = $type;
134 12
        $this->getterStrategy = $getterStrategy;
135 12
        $this->setterStrategy = $setterStrategy;
136 12
        $this->annotations = $annotations;
137 12
        $this->modifiers = $modifiers;
138 12
        $this->virtual = $virtual;
139 12
        $this->classMetadata = $classMetadata;
140
141 12
        $classMetadata->addPropertyMetadata($this);
142 12
    }
143
144
    /**
145
     * Get the real name of the property
146
     *
147
     * @return string
148
     */
149 3
    public function getName(): string
150
    {
151 3
        return $this->realName;
152
    }
153
154
    /**
155
     * Get the serialized name of the property
156
     *
157
     * @return string
158
     */
159 3
    public function getSerializedName(): string
160
    {
161 3
        return $this->serializedName;
162
    }
163
164
    /**
165
     * Get the property type
166
     *
167
     * @return TypeToken
168
     */
169 2
    public function getType(): TypeToken
170
    {
171 2
        return $this->type;
172
    }
173
174
    /**
175
     * Get the property type as a string
176
     *
177
     * @return string
178
     */
179 1
    public function getTypeName(): string
180
    {
181 1
        return (string)$this->type;
182
    }
183
184
    /**
185
     * The property modifiers
186
     *
187
     * @return int
188
     */
189 3
    public function getModifiers(): int
190
    {
191 3
        return $this->modifiers;
192
    }
193
194
    /**
195
     * Get full declaring class metadata
196
     *
197
     * @return ClassMetadata
198
     */
199 2
    public function getDeclaringClassMetadata(): ClassMetadata
200
    {
201 2
        return $this->classMetadata;
202
    }
203
204
    /**
205
     * Get the declaring class name
206
     *
207
     * @return string
208
     */
209 1
    public function getDeclaringClassName(): string
210
    {
211 1
        return $this->classMetadata->getName();
212
    }
213
214
    /**
215
     * Return the collection of annotations
216
     *
217
     * @return AnnotationCollection
218
     */
219 3
    public function getAnnotations(): AnnotationCollection
220
    {
221 3
        return $this->annotations;
222
    }
223
224
    /**
225
     * Get a single annotation, returns null if the annotation doesn't exist
226
     *
227
     * @param string $annotationName
228
     * @return null|AbstractAnnotation
229
     */
230 1
    public function getAnnotation(string $annotationName): ?AbstractAnnotation
231
    {
232 1
        return $this->annotations->get($annotationName);
233
    }
234
235
    /**
236
     * Returns true if the property is virtual
237
     *
238
     * @return bool
239
     */
240 1
    public function isVirtual(): bool
241
    {
242 1
        return $this->virtual;
243
    }
244
245
    /**
246
     * Returns should if we should skip during serialization
247
     *
248
     * @return bool
249
     */
250 2
    public function skipSerialize(): bool
251
    {
252 2
        return $this->skipSerialize;
253
    }
254
255
    /**
256
     * Set whether we should skip during serialization
257
     *
258
     * @param bool $skipSerialize
259
     */
260 3
    public function setSkipSerialize(bool $skipSerialize): void
261
    {
262 3
        $this->skipSerialize = $skipSerialize;
263 3
    }
264
265
    /**
266
     * Returns should if we should skip during deserialization
267
     *
268
     * @return bool
269
     */
270 3
    public function skipDeserialize(): bool
271
    {
272 3
        return $this->skipDeserialize;
273
    }
274
275
    /**
276
     * Set whether we should skip during deserialization
277
     *
278
     * @param bool $skipDeserialize
279
     */
280 3
    public function setSkipDeserialize(bool $skipDeserialize): void
281
    {
282 3
        $this->skipDeserialize = $skipDeserialize;
283 3
    }
284
285
    /**
286
     * Given an object, get the value at this property
287
     *
288
     * @param object $object
289
     * @return mixed
290
     */
291 6
    public function get($object)
292
    {
293 6
        return $this->getterStrategy->get($object);
294
    }
295
296
    /**
297
     * Given an object and value, set the value to the object at this property
298
     *
299
     * @param object $object
300
     * @param mixed $value
301
     */
302 7
    public function set($object, $value): void
303
    {
304 7
        $this->setterStrategy->set($object, $value);
305 7
    }
306
}
307