Completed
Push — standalone ( 787e4c...e35bae )
by Philip
07:04
created

ClassMetadata   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 62.11%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 3
dl 0
loc 314
ccs 59
cts 95
cp 0.6211
rs 9.2
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
B merge() 0 30 3
A setRestResource() 0 4 1
A isRestResource() 0 4 1
A getNamePrefix() 0 4 1
A setNamePrefix() 0 4 1
A getPathPrefix() 0 4 1
A setPathPrefix() 0 4 1
A getController() 0 4 1
A setController() 0 4 1
A getService() 0 4 1
A setService() 0 4 1
A getDeleteRight() 0 4 1
A setDeleteRight() 0 4 1
A getPostRight() 0 4 1
A setPostRight() 0 4 1
A getPutRight() 0 4 1
A setPutRight() 0 4 1
A getListRight() 0 4 1
A setListRight() 0 4 1
A getGetRight() 0 4 1
A setGetRight() 0 4 1
A getMethods() 0 4 1
A setMethods() 0 4 1
A getPropertyMetadata() 0 8 2
A mergePropertyMetadata() 0 17 3
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\Right;
7
use Metadata\MergeableClassMetadata;
8
use Metadata\MergeableInterface;
9
10
class ClassMetadata extends MergeableClassMetadata
11
{
12
    /**
13
     * @var bool
14
     */
15
    public $restResource;
16
17
    /**
18
     * @var string
19
     */
20
    public $namePrefix;
21
22
    /**
23
     * @var string
24
     */
25
    public $pathPrefix;
26
27
    /**
28
     * @var string
29
     */
30
    public $service;
31
32
    /**
33
     * @var string
34
     */
35
    public $controller;
36
37
    /**
38
     * @var Right|null
39
     */
40
    public $postRight;
41
42
    /**
43
     * @var Right|null
44
     */
45
    public $putRight;
46
47
    /**
48
     * @var Right|null
49
     */
50
    public $deleteRight;
51
52
    /**
53
     * @var Right|null
54
     */
55
    public $listRight;
56
57
    /**
58
     * @var Right|null
59
     */
60
    public $getRight;
61
62
    /**
63
     * @var Method[]
64
     */
65
    public $methods;
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 6
    public function merge(MergeableInterface $object)
71
    {
72 6
        if (!$object instanceof MergeableClassMetadata) {
73
            throw new \InvalidArgumentException('$object must be an instance of MergeableClassMetadata.');
74
        }
75
76 6
        $this->name = $object->name;
77 6
        $this->reflection = $object->reflection;
78 6
        $this->methodMetadata = array_merge($this->methodMetadata, $object->methodMetadata);
79 6
        $this->propertyMetadata = $this->mergePropertyMetadata($object);
80 6
        $this->fileResources = array_merge($this->fileResources, $object->fileResources);
81
82 6
        if ($object->createdAt < $this->createdAt) {
83
            $this->createdAt = $object->createdAt;
84
        }
85
86
        /** @var ClassMetadata $object */
87 6
        $this->restResource = $object->restResource;
88
        //TODO: merge
89 6
        $this->methods = $object->methods;
90 6
        $this->namePrefix = $object->namePrefix;
91 6
        $this->pathPrefix = $object->pathPrefix;
92 6
        $this->service = $object->service;
93 6
        $this->controller = $object->controller;
94 6
        $this->listRight = $object->listRight;
95 6
        $this->getRight = $object->getRight;
96 6
        $this->postRight = $object->postRight;
97 6
        $this->putRight = $object->putRight;
98 6
        $this->deleteRight = $object->deleteRight;
99 6
    }
100
101
    /**
102
     * @param bool $restResource
103
     */
104 34
    public function setRestResource($restResource)
105
    {
106 34
        $this->restResource = $restResource;
107 34
    }
108
109
    /**
110
     * @return boolean
111
     */
112 4
    public function isRestResource()
113
    {
114 4
        return $this->restResource;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 4
    public function getNamePrefix()
121
    {
122 4
        return $this->namePrefix;
123
    }
124
125
    /**
126
     * @param string $namePrefix
127
     */
128
    public function setNamePrefix($namePrefix)
129
    {
130
        $this->namePrefix = $namePrefix;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 4
    public function getPathPrefix()
137
    {
138 4
        return $this->pathPrefix;
139
    }
140
141
    /**
142
     * @param string $pathPrefix
143
     */
144 24
    public function setPathPrefix(string $pathPrefix)
145
    {
146 24
        $this->pathPrefix = $pathPrefix;
147 24
    }
148
149
    /**
150
     * @return string
151
     */
152 4
    public function getController()
153
    {
154 4
        return $this->controller;
155
    }
156
157
    /**
158
     * @param string $controller
159
     */
160
    public function setController($controller)
161
    {
162
        $this->controller = $controller;
163
    }
164
165
    /**
166
     * @return string
167
     */
168 4
    public function getService()
169
    {
170 4
        return $this->service;
171
    }
172
173
    /**
174
     * @param string $service
175
     */
176 4
    public function setService($service)
177
    {
178 4
        $this->service = $service;
179 4
    }
180
181
    /**
182
     * @return Right|null
183
     */
184
    public function getDeleteRight()
185
    {
186
        return $this->deleteRight;
187
    }
188
189
    /**
190
     * @param Right|null $deleteRight
191
     */
192
    public function setDeleteRight(Right $deleteRight)
193
    {
194
        $this->deleteRight = $deleteRight;
195
    }
196
197
    /**
198
     * @return Right|null
199
     */
200
    public function getPostRight()
201
    {
202
        return $this->postRight;
203
    }
204
205
    /**
206
     * @param Right|null $postRight
207
     */
208
    public function setPostRight(Right $postRight)
209
    {
210
        $this->postRight = $postRight;
211
    }
212
213
    /**
214
     * @return Right|null
215
     */
216
    public function getPutRight()
217
    {
218
        return $this->putRight;
219
    }
220
221
    /**
222
     * @param Right|null $putRight
223
     */
224
    public function setPutRight(Right $putRight)
225
    {
226
        $this->putRight = $putRight;
227
    }
228
229
    /**
230
     * @return Right|null
231
     */
232
    public function getListRight()
233
    {
234
        return $this->listRight;
235
    }
236
237
    /**
238
     * @param Right|null $listRight
239
     */
240
    public function setListRight(Right $listRight)
241
    {
242
        $this->listRight = $listRight;
243
    }
244
245
    /**
246
     * @return Right|null
247
     */
248
    public function getGetRight()
249
    {
250
        return $this->getRight;
251
    }
252
253
    /**
254
     * @param Right|null $getRight
255
     */
256
    public function setGetRight(Right $getRight)
257
    {
258
        $this->getRight = $getRight;
259
    }
260
261
    /**
262
     * @return Method[]
263
     */
264
    public function getMethods()
265
    {
266
        return $this->methods;
267
    }
268
269
    /**
270
     * @param string[] $methods
271
     */
272 34
    public function setMethods(array $methods)
273
    {
274 34
        $this->methods = $methods;
0 ignored issues
show
Documentation Bug introduced by
It seems like $methods of type array<integer,string> is incompatible with the declared type array<integer,object<Don...ata\Annotation\Method>> of property $methods.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
275 34
    }
276
277 34
    public function getPropertyMetadata(string $property): ?PropertyMetadata
278
    {
279 34
        if (array_key_exists($property, $this->propertyMetadata)) {
280 34
            return $this->propertyMetadata[$property];
281
        }
282
283 28
        return null;
284
    }
285
286
    /**
287
     * @param MergeableInterface $object
288
     *
289
     * @return array
290
     */
291 6
    protected function mergePropertyMetadata(MergeableInterface $object): array
292
    {
293
        /** @var ClassMetadata $object */
294
        /** @var PropertyMetadata[] $mergedMetadata */
295 6
        $mergedMetadata = $this->propertyMetadata;
296
297 6
        foreach ($object->propertyMetadata as $otherMetadata) {
0 ignored issues
show
Bug introduced by
Accessing propertyMetadata on the interface Metadata\MergeableInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
298
            /** @var PropertyMetadata $otherMetadata */
299 6
            if (array_key_exists($otherMetadata->name, $mergedMetadata)) {
300 4
                $mergedMetadata[$otherMetadata->name] = $mergedMetadata[$otherMetadata->name]->merge($otherMetadata);
301
            } else {
302 6
                $mergedMetadata[$otherMetadata->name] = $otherMetadata;
303
            }
304
        }
305
306 6
        return $mergedMetadata;
307
    }
308
309 24
    public function getMethod(string $methodName): ?Method
310
    {
311 24
        if (null === $this->methods) {
312
            return null;
313
        }
314
315 24
        foreach ($this->methods as $method) {
316 24
            if ($methodName === $method->getName()) {
317 24
                return $method;
318
            }
319
        }
320
321 4
        return null;
322
    }
323
}
324