Completed
Push — standalone ( 57a4d8...49bbc6 )
by Philip
10:02
created

PropertyMetadata::setTargetClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1.0156
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 19
    public function __construct($class, $name)
12
    {
13
        try {
14 19
            parent::__construct($class, $name);
15 2
        } 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 19
    }
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
     * @var string|null
93
     */
94
    private $targetClass;
95
96
    /**
97
     * @return boolean
98
     */
99 1
    public function isPuttable()
100
    {
101 1
        return $this->getBool($this->puttable, false);
102
    }
103
104
    /**
105
     * @param boolean $puttable
106
     */
107 12
    public function setPuttable($puttable)
108
    {
109 12
        $this->puttable = $puttable;
110 12
    }
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 12
    public function isIncludable()
132
    {
133 12
        return $this->getBool($this->includable, false);
134
    }
135
136
    /**
137
     * @param boolean $includable
138
     */
139 13
    public function setIncludable($includable)
140
    {
141 13
        $this->includable = $includable;
142 13
    }
143
144
    /**
145
     * @return boolean
146
     */
147 2
    public function isSubResource()
148
    {
149 2
        return $this->getBool($this->subResource, false);
150
    }
151
152
    /**
153
     * @param boolean $subResource
154
     */
155 12
    public function setSubResource($subResource)
156
    {
157 12
        $this->subResource = $subResource;
158 12
    }
159
160
    /**
161
     * @return Right|null
162
     */
163 3
    public function getSubResourceListRight()
164
    {
165 3
        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 3
    public function getSubResourcePostRight()
180
    {
181 3
        return $this->subResourcePostRight;
182
    }
183
184
    /**
185
     * @param Right|null $subResourcePostRight
186
     */
187 12
    public function setSubResourcePostRight($subResourcePostRight)
188
    {
189 12
        $this->subResourcePostRight = $subResourcePostRight;
190 12
    }
191
192
    /**
193
     * @param Right|null $subResourcePutRight
194
     */
195 12
    public function setSubResourcePutRight($subResourcePutRight)
196
    {
197 12
        $this->subResourcePutRight = $subResourcePutRight;
198 12
    }
199
200
    /**
201
     * @return Right|null
202
     */
203 2
    public function getSubResourcePutRight()
204
    {
205 2
        return $this->subResourcePutRight;
206
    }
207
208
    /**
209
     * @param Right|null $subResourceDeleteRight
210
     */
211 12
    public function setSubResourceDeleteRight($subResourceDeleteRight)
212
    {
213 12
        $this->subResourceDeleteRight = $subResourceDeleteRight;
214 12
    }
215
216
    /**
217
     * @return Right|null
218
     */
219 2
    public function getSubResourceDeleteRight()
220
    {
221 2
        return $this->subResourceDeleteRight;
222
    }
223
224
    /**
225
     * @return null|string
226
     */
227 1
    public function getTargetClass()
228
    {
229 1
        return $this->targetClass;
230
    }
231
232
    /**
233
     * @param null|string $targetClass
234
     */
235 13
    public function setTargetClass($targetClass)
236
    {
237 13
        $this->targetClass = $targetClass;
238 13
    }
239
240
    /**
241
     * @return null|string
242
     */
243 1
    public function getSubResourcePath()
244
    {
245 1
        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 12
    public function isExcluded(): bool
260
    {
261 12
        return $this->getBool($this->excluded, false);
262
    }
263
264
    /**
265
     * @param bool $excluded
266
     */
267 14
    public function setExcluded(bool $excluded)
268
    {
269 14
        $this->excluded = $excluded;
270 14
    }
271
272
    /**
273
     * @return null|\string[]
274
     */
275 9
    public function getIncludablePaths(): ?array
276
    {
277 9
        return $this->includablePaths;
278
    }
279
280
    /**
281
     * @param null|\string[] $includablePaths
282
     */
283 13
    public function setIncludablePaths(?array $includablePaths)
284
    {
285 13
        $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 13
    }
287
288
    /**
289
     * @return bool
290
     */
291 12
    public function isAssociation(): bool
292
    {
293 12
        return $this->getBool($this->association, false);
294
    }
295
296
    /**
297
     * @param bool $association
298
     */
299 13
    public function setAssociation(bool $association)
300
    {
301 13
        $this->association = $association;
302 13
    }
303
304
    /**
305
     * @return bool
306
     */
307 2
    public function isCollection(): bool
308
    {
309 2
        return $this->getBool($this->collection, false);
310
    }
311
312
    /**
313
     * @param bool $collection
314
     */
315 13
    public function setCollection(bool $collection)
316
    {
317 13
        $this->collection = $collection;
318 13
    }
319
320 12
    public function getType(): ?string
321
    {
322 12
        return $this->type;
323
    }
324
325 19
    public function setType(?string $type)
326
    {
327 19
        $this->type = $type;
328 19
    }
329
330 2
    public function merge(MergeableInterface $other)
331
    {
332 2
        if (!$other instanceof PropertyMetadata) {
333
            throw new \InvalidArgumentException('$object must be an instance of PropertyMetadata.');
334
        }
335
336 2
        $this->reflection = $this->mergeField($other->reflection, $this->reflection);
337 2
        $this->type = $this->mergeField($other->type, $this->type);
338 2
        $this->puttable = $this->mergeField($other->puttable, $this->puttable);
339 2
        $this->postable = $this->mergeField($other->postable, $this->postable);
340 2
        $this->type = $this->mergeField($other->type, $other->type);
341 2
        $this->excluded = $this->mergeField($other->excluded, $this->excluded);
342 2
        $this->includable = $this->mergeField($other->includable, $this->includable);
343 2
        $this->subResource = $this->mergeField($other->subResource, $this->subResource);
344 2
        $this->includablePaths = $this->mergeField($other->includablePaths, $this->includablePaths);
345 2
        $this->association = $this->mergeField($other->association, $this->association);
346 2
        $this->collection = $this->mergeField($other->collection, $this->collection);
347 2
        $this->subResourcePath = $this->mergeField($other->subResourcePath, $this->subResourcePath);
348 2
        $this->subResourceListRight = $this->mergeField($other->subResourceListRight, $this->subResourceListRight);
349 2
        $this->subResourcePostRight = $this->mergeField($other->subResourcePostRight, $this->subResourcePostRight);
350 2
        $this->subResourcePutRight = $this->mergeField($other->subResourcePutRight, $this->subResourcePutRight);
351 2
        $this->subResourceDeleteRight = $this->mergeField(
352 2
            $other->subResourceDeleteRight,
353 2
            $this->subResourceDeleteRight
354
        );
355 2
        $this->targetClass = $this->mergeField($other->targetClass, $this->targetClass);
356
357 2
        return $this;
358
    }
359
360 13
    protected function getBool(?bool $value, bool $default)
361
    {
362 13
        if (null === $value) {
363 13
            return $default;
364
        }
365
366 11
        return $value;
367
    }
368
369 2
    private function mergeField($thisValue, $otherValue)
370
    {
371 2
        if (null !== $thisValue) {
372 2
            return $thisValue;
373
        }
374
375 2
        return $otherValue;
376
    }
377
}
378