Completed
Push — standalone ( 1b0272...9e8eb1 )
by Philip
04:15
created

PropertyMetadata   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 37
lcom 1
cbo 2
dl 0
loc 262
ccs 91
cts 98
cp 0.9286
rs 8.6
c 1
b 0
f 0

30 Methods

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