Completed
Push — standalone ( 8da518...787e4c )
by Philip
09:19
created

PropertyMetadata::merge()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2.0004

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 20
cts 21
cp 0.9524
rs 8.8571
cc 2
eloc 21
nc 2
nop 1
crap 2.0004
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata;
4
5
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Right;
6
use Metadata\MergeableInterface;
7
use Metadata\PropertyMetadata as BasePropertyMetadata;
8
9
class PropertyMetadata extends BasePropertyMetadata implements MergeableInterface
10
{
11 38
    public function __construct($class, $name)
12
    {
13
        try {
14 38
            parent::__construct($class, $name);
15 4
        } catch (\ReflectionException $e) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
16
            /* Ignore missing property definition as they might just be overridden and therefore only exist in the
17
              parent class. They will be accessible after merging. */
18
        }
19 38
    }
20
21
    /**
22
     * @var string|null
23
     */
24
    private $type;
25
26
    /**
27
     * @var bool
28
     */
29
    private $puttable;
30
31
    /**
32
     * @var bool
33
     */
34
    private $excluded;
35
36
    /**
37
     * @var bool
38
     */
39
    private $postable;
40
41
    /**
42
     * @var bool
43
     */
44
    private $includable;
45
46
    /**
47
     * @var string[]|null
48
     */
49
    private $includablePaths;
50
51
    /**
52
     * @var bool
53
     */
54
    private $subResource;
55
56
    /**
57
     * @var bool
58
     */
59
    private $association;
60
61
    /**
62
     * @var bool
63
     */
64
    private $collection;
65
66
    /**
67
     * @var string|null
68
     */
69
    private $subResourcePath;
70
71
    /**
72
     * @var Right|null
73
     */
74
    private $subResourceListRight;
75
76
    /**
77
     * @var Right|null
78
     */
79
    private $subResourcePostRight;
80
81
    /**
82
     * @var Right|null
83
     */
84
    private $subResourcePutRight;
85
86
    /**
87
     * @var Right|null
88
     */
89
    private $subResourceDeleteRight;
90
91
    /**
92
     * @return boolean
93
     */
94 2
    public function isPuttable()
95
    {
96 2
        return $this->getBool($this->puttable, false);
97
    }
98
99
    /**
100
     * @param boolean $puttable
101
     */
102 24
    public function setPuttable($puttable)
103
    {
104 24
        $this->puttable = $puttable;
105 24
    }
106
107
    /**
108
     * @return boolean
109
     */
110
    public function isPostable()
111
    {
112
        return $this->getBool($this->postable, false);
113
    }
114
115
    /**
116
     * @param boolean $postable
117
     */
118
    public function setPostable($postable)
119
    {
120
        $this->postable = $postable;
121
    }
122
123
    /**
124
     * @return boolean
125
     */
126 24
    public function isIncludable()
127
    {
128 24
        return $this->getBool($this->includable, false);
129
    }
130
131
    /**
132
     * @param boolean $includable
133
     */
134 26
    public function setIncludable($includable)
135
    {
136 26
        $this->includable = $includable;
137 26
    }
138
139
    /**
140
     * @return boolean
141
     */
142 4
    public function isSubResource()
143
    {
144 4
        return $this->getBool($this->subResource, false);
145
    }
146
147
    /**
148
     * @param boolean $subResource
149
     */
150 24
    public function setSubResource($subResource)
151
    {
152 24
        $this->subResource = $subResource;
153 24
    }
154
155
    /**
156
     * @return Right|null
157
     */
158 6
    public function getSubResourceListRight()
159
    {
160 6
        return $this->subResourceListRight;
161
    }
162
163
    /**
164
     * @param Right|null $subResourceListRight
165
     */
166
    public function setSubResourceListRight($subResourceListRight)
167
    {
168
        $this->subResourceListRight = $subResourceListRight;
169
    }
170
171
    /**
172
     * @return Right|null
173
     */
174 6
    public function getSubResourcePostRight()
175
    {
176 6
        return $this->subResourcePostRight;
177
    }
178
179
    /**
180
     * @param Right|null $subResourcePostRight
181
     */
182 24
    public function setSubResourcePostRight($subResourcePostRight)
183
    {
184 24
        $this->subResourcePostRight = $subResourcePostRight;
185 24
    }
186
187
    /**
188
     * @param Right|null $subResourcePutRight
189
     */
190 24
    public function setSubResourcePutRight($subResourcePutRight)
191
    {
192 24
        $this->subResourcePutRight = $subResourcePutRight;
193 24
    }
194
195
    /**
196
     * @return Right|null
197
     */
198 4
    public function getSubResourcePutRight()
199
    {
200 4
        return $this->subResourcePutRight;
201
    }
202
203
    /**
204
     * @param Right|null $subResourceDeleteRight
205
     */
206 24
    public function setSubResourceDeleteRight($subResourceDeleteRight)
207
    {
208 24
        $this->subResourceDeleteRight = $subResourceDeleteRight;
209 24
    }
210
211
    /**
212
     * @return Right|null
213
     */
214 4
    public function getSubResourceDeleteRight()
215
    {
216 4
        return $this->subResourceDeleteRight;
217
    }
218
219
    /**
220
     * @return null|string
221
     */
222 2
    public function getSubResourcePath()
223
    {
224 2
        return $this->subResourcePath;
225
    }
226
227
    /**
228
     * @param null|string $subResourcePath
229
     */
230
    public function setSubResourcePath($subResourcePath)
231
    {
232
        $this->subResourcePath = $subResourcePath;
233
    }
234
235
    /**
236
     * @return bool
237
     */
238 24
    public function isExcluded(): bool
239
    {
240 24
        return $this->getBool($this->excluded, false);
241
    }
242
243
    /**
244
     * @param bool $excluded
245
     */
246 28
    public function setExcluded(bool $excluded)
247
    {
248 28
        $this->excluded = $excluded;
249 28
    }
250
251
    /**
252
     * @return null|\string[]
253
     */
254 18
    public function getIncludablePaths(): ?array
255
    {
256 18
        return $this->includablePaths;
257
    }
258
259
    /**
260
     * @param null|\string[] $includablePaths
261
     */
262 26
    public function setIncludablePaths(?array $includablePaths)
263
    {
264 26
        $this->includablePaths = $includablePaths;
0 ignored issues
show
Documentation Bug introduced by
It seems like $includablePaths can also be of type array<integer,object<string>>. However, the property $includablePaths is declared as type array<integer,string>|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
265 26
    }
266
267
    /**
268
     * @return bool
269
     */
270 24
    public function isAssociation(): bool
271
    {
272 24
        return $this->getBool($this->association, false);
273
    }
274
275
    /**
276
     * @param bool $association
277
     */
278 26
    public function setAssociation(bool $association)
279
    {
280 26
        $this->association = $association;
281 26
    }
282
283
    /**
284
     * @return bool
285
     */
286 4
    public function isCollection(): bool
287
    {
288 4
        return $this->getBool($this->collection, false);
289
    }
290
291
    /**
292
     * @param bool $collection
293
     */
294 26
    public function setCollection(bool $collection)
295
    {
296 26
        $this->collection = $collection;
297 26
    }
298
299 24
    public function getType(): ?string
300
    {
301 24
        return $this->type;
302
    }
303
304 38
    public function setType(?string $type)
305
    {
306 38
        $this->type = $type;
307 38
    }
308
309 4
    public function merge(MergeableInterface $other)
310
    {
311 4
        if (!$other instanceof PropertyMetadata) {
312
            throw new \InvalidArgumentException('$object must be an instance of PropertyMetadata.');
313
        }
314
315 4
        $this->reflection = $this->mergeField($other->reflection, $this->reflection);
316 4
        $this->type = $this->mergeField($other->type, $this->type);
317 4
        $this->puttable = $this->mergeField($other->puttable, $this->puttable);
318 4
        $this->postable = $this->mergeField($other->postable, $this->postable);
319 4
        $this->excluded = $this->mergeField($other->excluded, $this->excluded);
320 4
        $this->includable = $this->mergeField($other->includable, $this->includable);
321 4
        $this->subResource = $this->mergeField($other->subResource, $this->subResource);
322 4
        $this->includablePaths = $this->mergeField($other->includablePaths, $this->includablePaths);
323 4
        $this->association = $this->mergeField($other->association, $this->association);
324 4
        $this->collection = $this->mergeField($other->collection, $this->collection);
325 4
        $this->subResourcePath = $this->mergeField($other->subResourcePath, $this->subResourcePath);
326 4
        $this->subResourceListRight = $this->mergeField($other->subResourceListRight, $this->subResourceListRight);
327 4
        $this->subResourcePostRight = $this->mergeField($other->subResourcePostRight, $this->subResourcePostRight);
328 4
        $this->subResourcePutRight = $this->mergeField($other->subResourcePutRight, $this->subResourcePutRight);
329 4
        $this->subResourceDeleteRight = $this->mergeField(
330 4
            $other->subResourceDeleteRight,
331 4
            $this->subResourceDeleteRight
332
        );
333
334 4
        return $this;
335
    }
336
337 26
    protected function getBool(?bool $value, bool $default)
338
    {
339 26
        if (null === $value) {
340 26
            return $default;
341
        }
342
343 22
        return $value;
344
    }
345
346 4
    private function mergeField($thisValue, $otherValue)
347
    {
348 4
        if (null !== $thisValue) {
349 4
            return $thisValue;
350
        }
351
352 4
        return $otherValue;
353
    }
354
}
355