GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b10bb2...c22db0 )
by Sergey
07:14
created

AnnotationLoader::loadResourceMetadata()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 8.6737
cc 5
eloc 13
nc 5
nop 2
crap 5
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
12
namespace Reva2\JsonApi\Decoders\Mapping\Loader;
13
14
use Doctrine\Common\Annotations\Reader;
15
use Reva2\JsonApi\Annotations\Attribute;
16
use Reva2\JsonApi\Annotations\Document as ApiDocument;
17
use Reva2\JsonApi\Annotations\Resource as ApiResource;
18
use Reva2\JsonApi\Annotations\Object as ApiObject;
19
use Reva2\JsonApi\Annotations\Content as ApiContent;
20
use Reva2\JsonApi\Annotations\Property;
21
use Reva2\JsonApi\Annotations\Relationship;
22
use Reva2\JsonApi\Contracts\Decoders\Mapping\ClassMetadataInterface;
23
use Reva2\JsonApi\Contracts\Decoders\Mapping\Loader\LoaderInterface;
24
use Reva2\JsonApi\Decoders\Mapping\ClassMetadata;
25
use Reva2\JsonApi\Decoders\Mapping\DocumentMetadata;
26
use Reva2\JsonApi\Decoders\Mapping\ObjectMetadata;
27
use Reva2\JsonApi\Decoders\Mapping\PropertyMetadata;
28
use Reva2\JsonApi\Decoders\Mapping\ResourceMetadata;
29
30
/**
31
 * Loads JSON API metadata using a Doctrine annotations
32
 *
33
 * @package Reva2\JsonApi\Decoders\Mapping\Loader
34
 * @author Sergey Revenko <[email protected]>
35
 */
36
class AnnotationLoader implements LoaderInterface
37
{
38
    /**
39
     * @var Reader
40
     */
41
    protected $reader;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param Reader $reader
47
     */
48 5
    public function __construct(Reader $reader)
49
    {
50 5
        $this->reader = $reader;
51 5
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 5
    public function loadClassMetadata(\ReflectionClass $class)
57
    {
58 5
        if (null !== ($resource = $this->reader->getClassAnnotation($class, ApiResource::class))) {
59 2
            return $this->loadResourceMetadata($resource, $class);
60 3
        } elseif (null !== ($document = $this->reader->getClassAnnotation($class, ApiDocument::class))) {
61 1
            return $this->loadDocumentMetadata($document, $class);
62
        } else {
63 2
            $object = $this->reader->getClassAnnotation($class, ApiObject::class);
64
65 2
            return $this->loadObjectMetadata($class, $object);
1 ignored issue
show
Bug introduced by
It seems like $object defined by $this->reader->getClassA...otations\Object::class) on line 63 can also be of type object; however, Reva2\JsonApi\Decoders\M...r::loadObjectMetadata() does only seem to accept null|object<Reva2\JsonApi\Annotations\Object>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
        }
67
    }
68
69
    /**
70
     * Parse JSON API resource metadata
71
     *
72
     * @param ApiResource $resource
73
     * @param \ReflectionClass $class
74
     * @return ResourceMetadata
75
     */
76 2
    private function loadResourceMetadata(ApiResource $resource, \ReflectionClass $class)
77
    {
78 2
        $metadata = new ResourceMetadata( $class->getName());
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
79 2
        $metadata->setName($resource->name);
80
81 2
        $this->loadDiscriminatorMetadata($resource, $metadata);
82
83 2
        $properties = $class->getProperties();
84 2
        foreach ($properties as $property) {
85 2
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
86 2
                if ($annotation instanceof Attribute) {
87 2
                    $metadata->addAttribute($this->loadPropertyMetadata($annotation, $property));
88 2
                } elseif ($annotation instanceof Relationship) {
89 2
                    $metadata->addRelationship($this->loadPropertyMetadata($annotation, $property));
90 2
                }
91 2
            }
92 2
        }
93
94 2
        $this->loadParentMetadata($metadata, $class);
95
96 2
        return $metadata;
97
    }
98
99
    /**
100
     * @param \ReflectionClass $class
101
     * @param ApiObject|null $object
102
     * @return ObjectMetadata
103
     */
104 2
    private function loadObjectMetadata(\ReflectionClass $class, ApiObject $object = null)
105
    {
106 2
        $metadata = new ObjectMetadata($class->name);
107
108 2
        if (null !== $object) {
109 2
            $this->loadDiscriminatorMetadata($object, $metadata);
110 2
        }
111
112 2
        $properties = $class->getProperties();
113 2
        foreach ($properties as $property) {
114 2
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
115 2
                if ($annotation instanceof Property) {
116 2
                    $metadata->addProperty($this->loadPropertyMetadata($annotation, $property));
117 2
                }
118 2
            }
119 2
        }
120
121 2
        $this->loadParentMetadata($metadata, $class);
122
123 2
        return $metadata;
124
    }
125
126
    /**
127
     * Parse JSON API document metadata
128
     *
129
     * @param ApiDocument $document
130
     * @param \ReflectionClass $class
131
     * @return DocumentMetadata
132
     */
133 1
    private function loadDocumentMetadata(ApiDocument $document, \ReflectionClass $class)
134
    {
135 1
        $metadata = new DocumentMetadata($class->getName());
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
136 1
        $metadata->setAllowEmpty($document->allowEmpty);
137
138 1
        $properties = $class->getProperties();
139 1
        foreach ($properties as $property) {
140 1
            $content = $this->reader->getPropertyAnnotation($property, ApiContent::class);
141 1
            if (null !== $content) {
142 1
                $metadata->setContentMetadata($this->loadPropertyMetadata($content, $property));
143
144 1
                break;
145
            }
146 1
        }
147
148 1
        return $metadata;
149
    }
150
151
    /**
152
     * Parse property metadata
153
     *
154
     * @param Property $annotation
155
     * @param \ReflectionProperty $property
156
     * @return PropertyMetadata
157
     */
158 5
    private function loadPropertyMetadata(Property $annotation, \ReflectionProperty $property)
159
    {
160 5
        $metadata = new PropertyMetadata($property->name, $property->class);
161
162 5
        list($dataType, $dataTypeParams) = $this->parseDataType($annotation, $property);
163
164
        $metadata
165 5
            ->setDataType($dataType)
166 5
            ->setDataTypeParams($dataTypeParams)
167 5
            ->setOrmEntityClass($annotation->ormEntity);
168
169 5
        if ($annotation->setter) {
170
            $metadata->setSetter($annotation->setter);
171 5
        } elseif (false === $property->isPublic()) {
172 2
            $setter = 'set' . ucfirst($property->name);
173 2
            if (false == $property->getDeclaringClass()->hasMethod($setter)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
174
                throw new \RuntimeException(sprintf(
175
                    "Couldn't find setter for non public property: %s:%s",
176
                    $property->class,
177
                    $property->name
178
                ));
179
            }
180
181 2
            $metadata->setSetter($setter);
182 2
        }
183
184 5
        return $metadata;
185
    }
186
187
    /**
188
     * Parse property data type
189
     *
190
     * @param Property $annotation
191
     * @param \ReflectionProperty $property
192
     * @return array
193
     */
194 5
    private function parseDataType(Property $annotation, \ReflectionProperty $property)
195
    {
196 5
        if (!empty($annotation->parser)) {
197 1
            if (!$property->getDeclaringClass()->hasMethod($annotation->parser)) {
198
                throw new \InvalidArgumentException(sprintf(
199
                    "Custom parser function %s:%s() for property '%s' does not exist",
200
                    $property->class,
201
                    $annotation->parser,
202
                    $property->name
203
                ));
204
            }
205 1
            return ['custom', $annotation->parser];
206 5
        } elseif (!empty($annotation->type)) {
207 4
            return $this->parseDataTypeString($annotation->type);
208 4
        } elseif (preg_match('~@var\s(.*?)\s~si', $property->getDocComment(), $matches)) {
209 4
            return $this->parseDataTypeString($matches[1]);
210
        } else {
211 1
            return ['raw', null];
212
        }
213
    }
214
215
    /**
216
     * Parse data type string
217
     *
218
     * @param string $type
219
     * @return array
220
     */
221 5
    private function parseDataTypeString($type)
222
    {
223 5
        $params = null;
224
225 5
        if ($this->isScalarDataType($type)) {
226 4
            $dataType = 'scalar';
227 4
            $params = $type;
228 5
        } elseif (preg_match('~^DateTime(<(.*?)>)?$~', $type, $matches)) {
229 1
            $dataType = 'datetime';
230 1
            if (3 === count($matches)) {
231 1
                $params = $matches[2];
232 1
            }
233 1
        } elseif (
234 4
            (preg_match('~Array(<(.*?)>)?$~si', $type, $matches)) ||
235 4
            (preg_match('~^(.*?)\[\]$~si', $type, $matches))
236 4
        ) {
237 2
            $dataType = 'array';
238 2
            if (3 === count($matches)) {
239 2
                $params = $this->parseDataTypeString($matches[2]);
240 2
            } elseif (2 === count($matches)) {
241 1
                $params = $this->parseDataTypeString($matches[1]);
242 1
            } else {
243 1
                $params = ['raw', null];
244
            }
245 2
        } else {
246 4
            $type = ltrim($type, '\\');
247
248 4
            if (!class_exists($type)) {
249
                throw new \InvalidArgumentException(sprintf(
250
                    "Unknown object type '%s' specified",
251
                    $type
252
                ));
253
            }
254
255 4
            $dataType = 'object';
256 4
            $params = $type;
257
        }
258
259 5
        return [$dataType, $params];
260
    }
261
262
    /**
263
     * Returns true if specified type scalar. False otherwise.
264
     *
265
     * @param string $type
266
     * @return bool
267
     */
268 5
    private function isScalarDataType($type)
269
    {
270 5
        return in_array($type, ['string', 'bool', 'boolean', 'int', 'integer', 'float', 'double']);
271
    }
272
273
    /**
274
     * Load and merge metadata from parent class
275
     *
276
     * @param ClassMetadataInterface $metadata
277
     * @param \ReflectionClass $class
278
     */
279 4
    private function loadParentMetadata(ClassMetadataInterface $metadata, \ReflectionClass $class)
280
    {
281 4
        $parentClass = $class->getParentClass();
282 4
        if (false === $parentClass) {
283 4
            return;
284
        }
285
286 2
        $parentMetadata = $this->loadClassMetadata($parentClass);
287 2
        $metadata->mergeMetadata($parentMetadata);
288 2
    }
289
290
    /**
291
     * Load discriminator metadata
292
     *
293
     * @param ApiObject $object
294
     * @param ClassMetadata $metadata
295
     */
296 4
    private function loadDiscriminatorMetadata(ApiObject $object, ClassMetadata $metadata)
297
    {
298 4
        if (!$object->discField) {
299 1
            return;
300
        }
301
302 4
        $metadata->setDiscriminatorField($object->discField);
303 4
        $metadata->setDiscriminatorMap($object->discMap);
304
    }
305
}