Completed
Push — standalone ( 88d8ec...763a1b )
by Philip
04:53 queued 40s
created

PropertyMetadata   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 369
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 38
lcom 1
cbo 1
dl 0
loc 369
ccs 98
cts 110
cp 0.8909
rs 8.3999
c 1
b 0
f 0

34 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A isPuttable() 0 4 1
A setPuttable() 0 4 1
A isPostable() 0 4 1
A setPostable() 0 4 1
A isIncludable() 0 4 1
A setIncludable() 0 4 1
A isSubResource() 0 4 1
A setSubResource() 0 4 1
A getSubResourceListRight() 0 4 1
A setSubResourceListRight() 0 4 1
A getSubResourcePostRight() 0 4 1
A setSubResourcePutRight() 0 4 1
A getSubResourcePutRight() 0 4 1
A setSubResourceDeleteRight() 0 4 1
A getSubResourceDeleteRight() 0 4 1
A setTargetClass() 0 4 1
A getSubResourcePath() 0 4 1
A setSubResourcePath() 0 4 1
A isExcluded() 0 4 1
A setExcluded() 0 4 1
A getIncludablePaths() 0 4 1
A setIncludablePaths() 0 4 1
A isAssociation() 0 4 1
A setAssociation() 0 4 1
A isCollection() 0 4 1
A setCollection() 0 4 1
A getType() 0 4 1
A setType() 0 4 1
A setSubResourcePostRight() 0 4 1
A getTargetClass() 0 4 1
B merge() 0 29 2
A getBool() 0 8 2
A mergeField() 0 8 2
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 = false;
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 = false;
60
61
    /**
62
     * @var bool
63
     */
64
    private $collection = false;
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
     * @var string|null
93
     */
94
    private $targetClass;
95
96
    /**
97
     * @return boolean
98
     */
99 2
    public function isPuttable()
100
    {
101 2
        return $this->getBool($this->puttable, false);
102
    }
103
104
    /**
105
     * @param boolean $puttable
106
     */
107 24
    public function setPuttable($puttable)
108
    {
109 24
        $this->puttable = $puttable;
110 24
    }
111
112
    /**
113
     * @return boolean
114
     */
115
    public function isPostable()
116
    {
117
        return $this->getBool($this->postable, false);
118
    }
119
120
    /**
121
     * @param boolean $postable
122
     */
123
    public function setPostable($postable)
124
    {
125
        $this->postable = $postable;
126
    }
127
128
    /**
129
     * @return boolean
130
     */
131 24
    public function isIncludable()
132
    {
133 24
        return $this->getBool($this->includable, false);
134
    }
135
136
    /**
137
     * @param boolean $includable
138
     */
139 26
    public function setIncludable($includable)
140
    {
141 26
        $this->includable = $includable;
142 26
    }
143
144
    /**
145
     * @return boolean
146
     */
147 4
    public function isSubResource()
148
    {
149 4
        return $this->getBool($this->subResource, false);
150
    }
151
152
    /**
153
     * @param boolean $subResource
154
     */
155 24
    public function setSubResource($subResource)
156
    {
157 24
        $this->subResource = $subResource;
158 24
    }
159
160
    /**
161
     * @return Right|null
162
     */
163 6
    public function getSubResourceListRight()
164
    {
165 6
        return $this->subResourceListRight;
166
    }
167
168
    /**
169
     * @param Right|null $subResourceListRight
170
     */
171
    public function setSubResourceListRight($subResourceListRight)
172
    {
173
        $this->subResourceListRight = $subResourceListRight;
174
    }
175
176
    /**
177
     * @return Right|null
178
     */
179 6
    public function getSubResourcePostRight()
180
    {
181 6
        return $this->subResourcePostRight;
182
    }
183
184
    /**
185
     * @param Right|null $subResourcePostRight
186
     */
187 24
    public function setSubResourcePostRight($subResourcePostRight)
188
    {
189 24
        $this->subResourcePostRight = $subResourcePostRight;
190 24
    }
191
192
    /**
193
     * @param Right|null $subResourcePutRight
194
     */
195 24
    public function setSubResourcePutRight($subResourcePutRight)
196
    {
197 24
        $this->subResourcePutRight = $subResourcePutRight;
198 24
    }
199
200
    /**
201
     * @return Right|null
202
     */
203 4
    public function getSubResourcePutRight()
204
    {
205 4
        return $this->subResourcePutRight;
206
    }
207
208
    /**
209
     * @param Right|null $subResourceDeleteRight
210
     */
211 24
    public function setSubResourceDeleteRight($subResourceDeleteRight)
212
    {
213 24
        $this->subResourceDeleteRight = $subResourceDeleteRight;
214 24
    }
215
216
    /**
217
     * @return Right|null
218
     */
219 4
    public function getSubResourceDeleteRight()
220
    {
221 4
        return $this->subResourceDeleteRight;
222
    }
223
224
    /**
225
     * @return null|string
226
     */
227 2
    public function getTargetClass()
228
    {
229 2
        return $this->targetClass;
230
    }
231
232
    /**
233
     * @param null|string $targetClass
234
     */
235 26
    public function setTargetClass($targetClass)
236
    {
237 26
        $this->targetClass = $targetClass;
238 26
    }
239
240
    /**
241
     * @return null|string
242
     */
243 2
    public function getSubResourcePath()
244
    {
245 2
        return $this->subResourcePath;
246
    }
247
248
    /**
249
     * @param null|string $subResourcePath
250
     */
251
    public function setSubResourcePath($subResourcePath)
252
    {
253
        $this->subResourcePath = $subResourcePath;
254
    }
255
256
    /**
257
     * @return bool
258
     */
259 24
    public function isExcluded(): bool
260
    {
261 24
        return $this->getBool($this->excluded, false);
262
    }
263
264
    /**
265
     * @param bool $excluded
266
     */
267 28
    public function setExcluded(bool $excluded)
268
    {
269 28
        $this->excluded = $excluded;
270 28
    }
271
272
    /**
273
     * @return null|\string[]
274
     */
275 18
    public function getIncludablePaths(): ?array
276
    {
277 18
        return $this->includablePaths;
278
    }
279
280
    /**
281
     * @param null|\string[] $includablePaths
282
     */
283 26
    public function setIncludablePaths(?array $includablePaths)
284
    {
285 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...
286 26
    }
287
288
    /**
289
     * @return bool
290
     */
291 24
    public function isAssociation(): bool
292
    {
293 24
        return $this->getBool($this->association, false);
294
    }
295
296
    /**
297
     * @param bool $association
298
     */
299 26
    public function setAssociation(bool $association)
300
    {
301 26
        $this->association = $association;
302 26
    }
303
304
    /**
305
     * @return bool
306
     */
307 4
    public function isCollection(): bool
308
    {
309 4
        return $this->getBool($this->collection, false);
310
    }
311
312
    /**
313
     * @param bool $collection
314
     */
315 26
    public function setCollection(bool $collection)
316
    {
317 26
        $this->collection = $collection;
318 26
    }
319
320 24
    public function getType(): ?string
321
    {
322 24
        return $this->type;
323
    }
324
325 38
    public function setType(?string $type)
326
    {
327 38
        $this->type = $type;
328 38
    }
329
330 4
    public function merge(MergeableInterface $other)
331
    {
332 4
        if (!$other instanceof PropertyMetadata) {
333
            throw new \InvalidArgumentException('$object must be an instance of PropertyMetadata.');
334
        }
335
336 4
        $this->reflection = $this->mergeField($other->reflection, $this->reflection);
337 4
        $this->type = $this->mergeField($other->type, $this->type);
338 4
        $this->puttable = $this->mergeField($other->puttable, $this->puttable);
339 4
        $this->postable = $this->mergeField($other->postable, $this->postable);
340 4
        $this->type = $this->mergeField($other->type, $other->type);
341 4
        $this->excluded = $this->mergeField($other->excluded, $this->excluded);
342 4
        $this->includable = $this->mergeField($other->includable, $this->includable);
343 4
        $this->subResource = $this->mergeField($other->subResource, $this->subResource);
344 4
        $this->includablePaths = $this->mergeField($other->includablePaths, $this->includablePaths);
345 4
        $this->association = $this->mergeField($other->association, $this->association);
346 4
        $this->collection = $this->mergeField($other->collection, $this->collection);
347 4
        $this->subResourcePath = $this->mergeField($other->subResourcePath, $this->subResourcePath);
348 4
        $this->subResourceListRight = $this->mergeField($other->subResourceListRight, $this->subResourceListRight);
349 4
        $this->subResourcePostRight = $this->mergeField($other->subResourcePostRight, $this->subResourcePostRight);
350 4
        $this->subResourcePutRight = $this->mergeField($other->subResourcePutRight, $this->subResourcePutRight);
351 4
        $this->subResourceDeleteRight = $this->mergeField(
352 4
            $other->subResourceDeleteRight,
353 4
            $this->subResourceDeleteRight
354
        );
355 4
        $this->targetClass = $this->mergeField($other->targetClass, $this->targetClass);
356
357 4
        return $this;
358
    }
359
360 26
    protected function getBool(?bool $value, bool $default)
361
    {
362 26
        if (null === $value) {
363 26
            return $default;
364
        }
365
366 26
        return $value;
367
    }
368
369 4
    private function mergeField($thisValue, $otherValue)
370
    {
371 4
        if (null !== $thisValue) {
372 4
            return $thisValue;
373
        }
374
375 4
        return $otherValue;
376
    }
377
}
378