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 ( 78595b...b10bb2 )
by Sergey
06:33
created

AnnotationLoader::loadDiscriminatorMetadata()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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