Completed
Push — standalone ( f309de...aaef72 )
by Philip
05:10
created

PropertyMetadata::getPostableRight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata;
4
5
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method;
6
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Right;
7
use Metadata\MergeableInterface;
8
use Metadata\PropertyMetadata as BasePropertyMetadata;
9
10
class PropertyMetadata extends BasePropertyMetadata implements MergeableInterface
11
{
12 76
    public function __construct($class, $name)
13
    {
14
        try {
15 76
            parent::__construct($class, $name);
16 26
        } catch (\ReflectionException $e) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
17
            /* Ignore missing property definition as they might just be overridden and therefore only exist in the
18
              parent class. They will be accessible after merging. */
19
        }
20 76
    }
21
22
    /**
23
     * @var string|null
24
     */
25
    private $type;
26
27
    /**
28
     * @var bool
29
     */
30
    private $puttable;
31
32
    /**
33
     * @var bool
34
     */
35
    private $excluded;
36
37
    /**
38
     * @var bool
39
     */
40
    private $postable;
41
42
    /**
43
     * @var Right|null
44
     */
45
    private $postableRight;
46
47
    /**
48
     * @var Right|null
49
     */
50
    private $puttableRight;
51
52
    /**
53
     * @var bool
54
     */
55
    private $includable;
56
57
    /**
58
     * @var string[]|null
59
     */
60
    private $includablePaths;
61
62
    /**
63
     * @var bool
64
     */
65
    private $subResource;
66
67
    /**
68
     * @var Method[]|null
69
     */
70
    private $methods;
71
72
    /**
73
     * @var bool
74
     */
75
    private $association;
76
77
    /**
78
     * @var bool
79
     */
80
    private $collection;
81
82
    /**
83
     * @var bool
84
     */
85
    private $virtual;
86
87
    /**
88
     * @var string|null
89
     */
90
    private $subResourcePath;
91
92 8
    public function isPuttable(): bool
93
    {
94 8
        return $this->getBool($this->puttable, false);
95
    }
96
97 48
    public function setPuttable(bool $puttable)
98
    {
99 48
        $this->puttable = $puttable;
100 48
    }
101
102 10
    public function isPostable(): bool
103
    {
104 10
        return $this->getBool($this->postable, false);
105
    }
106
107 48
    public function setPostable(bool $postable)
108
    {
109 48
        $this->postable = $postable;
110 48
    }
111
112 58
    public function isIncludable(): bool
113
    {
114 58
        return $this->getBool($this->includable, false);
115
    }
116
117
    public function isVirtual(): bool
118
    {
119
        return $this->getBool($this->virtual, false);
120
    }
121
122 24
    public function setVirtual(bool $virtual)
123
    {
124 24
        $this->virtual = $virtual;
125 24
    }
126
127 64
    public function setIncludable(bool $includable)
128
    {
129 64
        $this->includable = $includable;
130 64
    }
131
132 10
    public function isSubResource(): bool
133
    {
134 10
        return $this->getBool($this->subResource, false);
135
    }
136
137 62
    public function setSubResource(bool $subResource)
138
    {
139 62
        $this->subResource = $subResource;
140 62
    }
141
142 10
    public function getSubResourcePath(): ?string
143
    {
144 10
        return $this->subResourcePath;
145
    }
146
147
    public function setSubResourcePath(string $subResourcePath)
148
    {
149
        $this->subResourcePath = $subResourcePath;
150
    }
151
152 58
    public function isExcluded(): bool
153
    {
154 58
        return $this->getBool($this->excluded, false);
155
    }
156
157 58
    public function setExcluded(bool $excluded)
158
    {
159 58
        $this->excluded = $excluded;
160 58
    }
161
162
    /**
163
     * @return null|\string[]
164
     */
165 44
    public function getIncludablePaths(): ?array
166
    {
167 44
        return $this->includablePaths;
168
    }
169
170
    /**
171
     * @param null|\string[] $includablePaths
172
     */
173 64
    public function setIncludablePaths(?array $includablePaths)
174
    {
175 64
        $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...
176 64
    }
177
178 58
    public function isAssociation(): bool
179
    {
180 58
        return $this->getBool($this->association, false);
181
    }
182
183 62
    public function setAssociation(bool $association)
184
    {
185 62
        $this->association = $association;
186 62
    }
187
188 30
    public function isCollection(): bool
189
    {
190 30
        return $this->getBool($this->collection, false);
191
    }
192
193 62
    public function setCollection(bool $collection)
194
    {
195 62
        $this->collection = $collection;
196 62
    }
197
198 60
    public function getType(): ?string
199
    {
200 60
        return $this->type;
201
    }
202
203 76
    public function setType(?string $type)
204
    {
205 76
        $this->type = $type;
206 76
    }
207
208
    /**
209
     * @param Method[] $methods
210
     */
211 62
    public function setMethods(array $methods)
212
    {
213 62
        $this->methods = $methods;
214 62
    }
215
216 10
    public function getPostableRight(): ?Right
217
    {
218 10
        return $this->postableRight;
219
    }
220
221 8
    public function getPuttableRight(): ?Right
222
    {
223 8
        return $this->puttableRight;
224
    }
225
226 16
    public function setPostableRight(?Right $postableRight)
227
    {
228 16
        $this->postableRight = $postableRight;
229 16
    }
230
231 16
    public function setPuttableRight(?Right $puttableRight)
232
    {
233 16
        $this->puttableRight = $puttableRight;
234 16
    }
235
236 8
    public function merge(MergeableInterface $other)
237
    {
238 8
        if (!$other instanceof PropertyMetadata) {
239
            throw new \InvalidArgumentException('$object must be an instance of PropertyMetadata.');
240
        }
241
242 8
        $this->reflection = $this->mergeField($other->reflection, $this->reflection);
243 8
        $this->type = $this->mergeField($other->type, $this->type);
244 8
        $this->puttable = $this->mergeField($other->puttable, $this->puttable);
245 8
        $this->postable = $this->mergeField($other->postable, $this->postable);
246 8
        $this->excluded = $this->mergeField($other->excluded, $this->excluded);
247 8
        $this->includable = $this->mergeField($other->includable, $this->includable);
248 8
        $this->subResource = $this->mergeField($other->subResource, $this->subResource);
249 8
        $this->includablePaths = $this->mergeField($other->includablePaths, $this->includablePaths);
250 8
        $this->association = $this->mergeField($other->association, $this->association);
251 8
        $this->collection = $this->mergeField($other->collection, $this->collection);
252 8
        $this->subResourcePath = $this->mergeField($other->subResourcePath, $this->subResourcePath);
253 8
        $this->methods = $this->mergeField($other->methods, $this->methods);
254 8
        $this->virtual = $this->mergeField($other->virtual, $this->virtual);
255 8
        $this->postableRight = $this->mergeField($other->postableRight, $this->postableRight);
256 8
        $this->puttableRight = $this->mergeField($other->puttableRight, $this->puttableRight);
257
258 8
        return $this;
259
    }
260
261 62
    protected function getBool(?bool $value, bool $default)
262
    {
263 62
        if (null === $value) {
264 60
            return $default;
265
        }
266
267 62
        return $value;
268
    }
269
270 8
    private function mergeField($thisValue, $otherValue)
271
    {
272 8
        if (null !== $thisValue) {
273 8
            return $thisValue;
274
        }
275
276 8
        return $otherValue;
277
    }
278
279 38
    public function getMethod(string $methodName): ?Method
280
    {
281 38
        if (null === $this->methods) {
282
            return null;
283
        }
284
285 38
        foreach ($this->methods as $method) {
286 38
            if ($methodName === $method->name) {
287 38
                return $method;
288
            }
289
        }
290
291 10
        return null;
292
    }
293
}
294