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

ClassMetadata::setGetRight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata;
4
5
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method;
6
use Metadata\MergeableClassMetadata;
7
use Metadata\MergeableInterface;
8
9
class ClassMetadata extends MergeableClassMetadata
10
{
11
    /**
12
     * @var bool
13
     */
14
    public $restResource;
15
16
    /**
17
     * @var string
18
     */
19
    public $namePrefix;
20
21
    /**
22
     * @var string
23
     */
24
    public $pathPrefix;
25
26
    /**
27
     * @var string
28
     */
29
    public $service;
30
31
    /**
32
     * @var string
33
     */
34
    public $controller;
35
36
    /**
37
     * @var Method[]|null
38
     */
39
    public $methods;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 6
    public function merge(MergeableInterface $object)
45
    {
46 6
        if (!$object instanceof MergeableClassMetadata) {
47
            throw new \InvalidArgumentException('$object must be an instance of MergeableClassMetadata.');
48
        }
49
50 6
        $this->name = $object->name;
51 6
        $this->reflection = $object->reflection;
52 6
        $this->methodMetadata = array_merge($this->methodMetadata, $object->methodMetadata);
53 6
        $this->propertyMetadata = $this->mergePropertyMetadata($object);
54 6
        $this->fileResources = array_merge($this->fileResources, $object->fileResources);
55
56 6
        if ($object->createdAt < $this->createdAt) {
57
            $this->createdAt = $object->createdAt;
58
        }
59
60
        /** @var ClassMetadata $object */
61 6
        $this->restResource = $object->restResource;
62 6
        $this->methods = $object->methods;
63 6
        $this->namePrefix = $object->namePrefix;
64 6
        $this->pathPrefix = $object->pathPrefix;
65 6
        $this->service = $object->service;
66 6
        $this->controller = $object->controller;
67 6
    }
68
69
    /**
70
     * @param bool $restResource
71
     */
72 34
    public function setRestResource($restResource)
73
    {
74 34
        $this->restResource = $restResource;
75 34
    }
76
77
    /**
78
     * @return boolean
79
     */
80 4
    public function isRestResource()
81
    {
82 4
        return $this->restResource;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 4
    public function getNamePrefix()
89
    {
90 4
        return $this->namePrefix;
91
    }
92
93
    /**
94
     * @param string $namePrefix
95
     */
96
    public function setNamePrefix($namePrefix)
97
    {
98
        $this->namePrefix = $namePrefix;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 4
    public function getPathPrefix()
105
    {
106 4
        return $this->pathPrefix;
107
    }
108
109
    /**
110
     * @param string $pathPrefix
111
     */
112 24
    public function setPathPrefix(string $pathPrefix)
113
    {
114 24
        $this->pathPrefix = $pathPrefix;
115 24
    }
116
117
    /**
118
     * @return string
119
     */
120 4
    public function getController()
121
    {
122 4
        return $this->controller;
123
    }
124
125
    /**
126
     * @param string $controller
127
     */
128
    public function setController($controller)
129
    {
130
        $this->controller = $controller;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 4
    public function getService()
137
    {
138 4
        return $this->service;
139
    }
140
141
    /**
142
     * @param string $service
143
     */
144 4
    public function setService($service)
145
    {
146 4
        $this->service = $service;
147 4
    }
148
149
    /**
150
     * @return Method[]
151
     */
152
    public function getMethods()
153
    {
154
        return $this->methods;
155
    }
156
157
    /**
158
     * @param string[] $methods
159
     */
160 34
    public function setMethods(array $methods)
161
    {
162 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...nnotation\Method>>|null 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...
163 34
    }
164
165 34
    public function getPropertyMetadata(string $property): ?PropertyMetadata
166
    {
167 34
        if (array_key_exists($property, $this->propertyMetadata)) {
168 34
            return $this->propertyMetadata[$property];
169
        }
170
171 30
        return null;
172
    }
173
174
    /**
175
     * @param MergeableInterface $object
176
     *
177
     * @return array
178
     */
179 6
    protected function mergePropertyMetadata(MergeableInterface $object): array
180
    {
181
        /** @var ClassMetadata $object */
182
        /** @var PropertyMetadata[] $mergedMetadata */
183 6
        $mergedMetadata = $this->propertyMetadata;
184
185 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...
186
            /** @var PropertyMetadata $otherMetadata */
187 6
            if (array_key_exists($otherMetadata->name, $mergedMetadata)) {
188 4
                $mergedMetadata[$otherMetadata->name] = $mergedMetadata[$otherMetadata->name]->merge($otherMetadata);
189
            } else {
190 6
                $mergedMetadata[$otherMetadata->name] = $otherMetadata;
191
            }
192
        }
193
194 6
        return $mergedMetadata;
195
    }
196
197 24
    public function getMethod(string $methodName): ?Method
198
    {
199 24
        if (null === $this->methods) {
200
            return null;
201
        }
202
203 24
        foreach ($this->methods as $method) {
204 24
            if ($methodName === $method->name) {
205 24
                return $method;
206
            }
207
        }
208
209 4
        return null;
210
    }
211
}
212