Completed
Push — standalone ( e42fd2...f309de )
by Philip
10:13
created

PropertyMetadata::setVirtual()   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

Importance

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