Completed
Push — master ( 3886f0...1f9c9a )
by Nate
02:55
created

Property::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 8
crap 1

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
namespace Tebru\Gson\Internal\Data;
8
9
use Tebru\Gson\Internal\GetterStrategy;
10
use Tebru\Gson\PhpType;
11
use Tebru\Gson\Internal\SetterStrategy;
12
13
/**
14
 * Class Property
15
 *
16
 * Represents static information about an object property.  Instances of this class may be
17
 * cached for later use.
18
 *
19
 * @author Nate Brunette <[email protected]>
20
 */
21
final class Property
22
{
23
    /**
24
     * The actual name of the property
25
     *
26
     * @var string
27
     */
28
    private $realName;
29
30
    /**
31
     * The serialized version of the property name
32
     *
33
     * @var string
34
     */
35
    private $serializedName;
36
37
    /**
38
     * The property type
39
     *
40
     * @var PhpType
41
     */
42
    private $type;
43
44
    /**
45
     * The method for getting values from this property
46
     *
47
     * @var GetterStrategy
48
     */
49
    private $getterStrategy;
50
51
    /**
52
     * The method for setting values to this property
53
     *
54
     * @var SetterStrategy
55
     */
56
    private $setterStrategy;
57
58
    /**
59
     * A set of annotations
60
     *
61
     * @var AnnotationSet
62
     */
63
    private $annotations;
64
65
    /**
66
     * An integer that represents what modifiers are associated with the property
67
     *
68
     * These constants are defined in [@see \ReflectionProperty]
69
     *
70
     * @var int
71
     */
72
    private $modifiers;
73
74
    /**
75
     * True if the property should be skipped during serialization
76
     *
77
     * @var bool
78
     */
79
    private $skipSerialize = false;
80
81
    /**
82
     * True if the property should be skipped during deserialization
83
     *
84
     * @var bool
85
     */
86
    private $skipDeserialize = false;
87
88
    /**
89
     * If the property is a virtual property
90
     * @var bool
91
     */
92
    private $virtual;
93
94
    /**
95
     * Constructor
96
     *
97
     * @param string $realName
98
     * @param string $serializedName
99
     * @param PhpType $type
100
     * @param GetterStrategy $getterStrategy
101
     * @param SetterStrategy $setterStrategy
102
     * @param AnnotationSet $annotations
103
     * @param int $modifiers
104
     * @param bool $virtual
105
     */
106 8
    public function __construct(
107
        string $realName,
108
        string $serializedName,
109
        PhpType $type,
110
        GetterStrategy $getterStrategy,
111
        SetterStrategy $setterStrategy,
112
        AnnotationSet $annotations,
113
        int $modifiers,
114
        bool $virtual
115
    )
116
    {
117 8
        $this->realName = $realName;
118 8
        $this->serializedName = $serializedName;
119 8
        $this->type = $type;
120 8
        $this->getterStrategy = $getterStrategy;
121 8
        $this->setterStrategy = $setterStrategy;
122 8
        $this->annotations = $annotations;
123 8
        $this->modifiers = $modifiers;
124 8
        $this->virtual = $virtual;
125 8
    }
126
127
    /**
128
     * Get the real name of the property
129
     *
130
     * @return string
131
     */
132 1
    public function getRealName(): string
133
    {
134 1
        return $this->realName;
135
    }
136
137
    /**
138
     * Get the serialized name of the property
139
     *
140
     * @return string
141
     */
142 1
    public function getSerializedName(): string
143
    {
144 1
        return $this->serializedName;
145
    }
146
147
    /**
148
     * Get the property type
149
     *
150
     * @return PhpType
151
     */
152 1
    public function getType(): PhpType
153
    {
154 1
        return $this->type;
155
    }
156
157
    /**
158
     * Return the collection of annotations
159
     *
160
     * @return AnnotationSet
161
     */
162 1
    public function getAnnotations(): AnnotationSet
163
    {
164 1
        return $this->annotations;
165
    }
166
167
    /**
168
     * The property modifiers
169
     *
170
     * @return int
171
     */
172 1
    public function getModifiers(): int
173
    {
174 1
        return $this->modifiers;
175
    }
176
177
    /**
178
     * Returns true if the property is virtual
179
     *
180
     * @return bool
181
     */
182 1
    public function isVirtual(): bool
183
    {
184 1
        return $this->virtual;
185
    }
186
187
    /**
188
     * Returns should if we should skip during serialization
189
     *
190
     * @return bool
191
     */
192 2
    public function skipSerialize(): bool
193
    {
194 2
        return $this->skipSerialize;
195
    }
196
197
    /**
198
     * Set whether we should skip during serialization
199
     *
200
     * @param bool $skipSerialize
201
     */
202 1
    public function setSkipSerialize(bool $skipSerialize): void
203
    {
204 1
        $this->skipSerialize = $skipSerialize;
205 1
    }
206
207
    /**
208
     * Returns should if we should skip during deserialization
209
     *
210
     * @return bool
211
     */
212 2
    public function skipDeserialize(): bool
213
    {
214 2
        return $this->skipDeserialize;
215
    }
216
217
    /**
218
     * Set whether we should skip during deserialization
219
     *
220
     * @param bool $skipDeserialize
221
     */
222 1
    public function setSkipDeserialize(bool $skipDeserialize): void
223
    {
224 1
        $this->skipDeserialize = $skipDeserialize;
225 1
    }
226
227
    /**
228
     * Given an object, get the value at this property
229
     *
230
     * @param object $object
231
     * @return mixed
232
     */
233 6
    public function get($object)
234
    {
235 6
        return $this->getterStrategy->get($object);
236
    }
237
238
    /**
239
     * Given an object an value, set the value to the object at this property
240
     *
241
     * @param object $object
242
     * @param mixed $value
243
     */
244 6
    public function set($object, $value): void
245
    {
246 6
        if (null === $value) {
247 1
            return;
248
        }
249
250 5
        $this->setterStrategy->set($object, $value);
251 5
    }
252
}
253