Completed
Push — standalone ( 444422...a45559 )
by Philip
05:18
created

PropertyMetadata::getMethod()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

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