AnnotationLoader   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D loadClassMetadata() 0 40 10
1
<?php
2
/*
3
 * This file is part of AppBundle the package.
4
 *
5
 * (c) Ruslan Muriev <[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
namespace RonteLtd\JsonApiBundle\Serializer\Mapping\Loader;
12
13
use Doctrine\Common\Annotations\Reader;
14
use RonteLtd\JsonApiBundle\Annotation\Attribute;
15
use RonteLtd\JsonApiBundle\Annotation\Relationship;
16
use RonteLtd\JsonApiBundle\Serializer\Mapping\AttributeMetadata;
17
use RonteLtd\JsonApiBundle\Serializer\Mapping\ClassMetadataInterface;
18
use RonteLtd\JsonApiBundle\Serializer\Mapping\ClassAnnotationInterface;
19
20
/**
21
 * Class AnnotationLoader
22
 *
23
 * @package RonteLtd\JsonApiBundle\Serializer\Mapping\Loader
24
 * @author Ruslan Muriev <[email protected]>
25
 */
26
class AnnotationLoader implements LoaderInterface
27
{
28
    /**
29
     * @var Reader
30
     */
31
    private $reader;
32
33
    /**
34
     * @param Reader $reader
35
     */
36
    public function __construct(Reader $reader)
37
    {
38
        $this->reader = $reader;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function loadClassMetadata(ClassMetadataInterface $classMetadata)
45
    {
46
        $reflectionClass = $classMetadata->getReflectionClass();
47
        $className = $reflectionClass->name;
48
        $loaded = false;
49
50
        // Load class annotations
51
        foreach ($this->reader->getClassAnnotations($reflectionClass) as $classAnnotation) {
52
            if ($classAnnotation instanceof ClassAnnotationInterface) {
53
                if (null === $classAnnotation->getName()) {
54
                    $classAnnotation->setName($reflectionClass->getShortName());
55
                }
56
57
                $classMetadata->addClassAnnotation($classAnnotation);
58
            }
59
        }
60
61
        // Load class attributes annotations
62
        $attributesMetadata = $classMetadata->getAttributesMetadata();
63
        foreach ($reflectionClass->getProperties() as $property) {
64
            if (!isset($attributesMetadata[$property->name])) {
65
                $attributesMetadata[$property->name] = new AttributeMetadata($property->name);
66
                $classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
67
            }
68
69
            if ($property->getDeclaringClass()->name === $className) {
70
                foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
71
                    if ($annotation instanceof Attribute) {
72
                        $attributesMetadata[$property->name]->setAttribute($annotation);
0 ignored issues
show
Documentation introduced by
$annotation is of type object<RonteLtd\JsonApiB...e\Annotation\Attribute>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
                    } elseif ($annotation instanceof Relationship) {
74
                        $attributesMetadata[$property->name]->setRelationship($annotation);
0 ignored issues
show
Documentation introduced by
$annotation is of type object<RonteLtd\JsonApiB...nnotation\Relationship>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
                    }
76
77
                    $loaded = true;
78
                }
79
            }
80
        }
81
82
        return $loaded;
83
    }
84
}