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

AnnotationDriver   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 7
dl 0
loc 120
ccs 54
cts 60
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F loadMetadataForClass() 0 101 21
1
<?php
2
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
3
4
use Doctrine\Common\Annotations\Reader;
5
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Excluded;
6
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Includable;
7
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Postable;
8
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Puttable;
9
use Dontdrinkandroot\RestBundle\Metadata\Annotation\RootResource;
10
use Dontdrinkandroot\RestBundle\Metadata\Annotation\SubResource;
11
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
12
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
13
use Metadata\Driver\DriverInterface;
14
15
class AnnotationDriver implements DriverInterface
16
{
17
    private $reader;
18
19
    /**
20
     * @var DriverInterface
21
     */
22
    private $doctrineDriver;
23
24 34
    public function __construct(Reader $reader, DriverInterface $doctrineDriver)
25
    {
26 34
        $this->reader = $reader;
27 34
        $this->doctrineDriver = $doctrineDriver;
28 34
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 30
    public function loadMetadataForClass(\ReflectionClass $class)
34
    {
35
        /** @var ClassMetadata $ddrRestClassMetadata */
36 30
        $ddrRestClassMetadata = $this->doctrineDriver->loadMetadataForClass($class);
37 30
        if (null === $ddrRestClassMetadata) {
38
            $ddrRestClassMetadata = new ClassMetadata($class->getName());
39
        }
40
41
        /** @var RootResource $restResourceAnnotation */
42 30
        $restResourceAnnotation = $this->reader->getClassAnnotation($class, RootResource::class);
43 30
        if (null !== $restResourceAnnotation) {
44
45 28
            $ddrRestClassMetadata->setRestResource(true);
46
47 28
            if (null !== $restResourceAnnotation->namePrefix) {
48
                $ddrRestClassMetadata->setNamePrefix($restResourceAnnotation->namePrefix);
49
            }
50
51 28
            if (null !== $restResourceAnnotation->pathPrefix) {
52 24
                $ddrRestClassMetadata->setPathPrefix($restResourceAnnotation->pathPrefix);
53
            }
54
55 28
            if (null !== $restResourceAnnotation->service) {
56
                $ddrRestClassMetadata->setService($restResourceAnnotation->service);
57
            }
58
59 28
            if (null !== $restResourceAnnotation->controller) {
60
                $ddrRestClassMetadata->setController($restResourceAnnotation->controller);
61
            }
62
63 28
            if (null !== $restResourceAnnotation->methods) {
64 28
                $methods = [];
65 28
                $methodAnnotations = $restResourceAnnotation->methods;
66 28
                foreach ($methodAnnotations as $methodAnnotation) {
67 28
                    $methods[$methodAnnotation->name] = $methodAnnotation;
68
                }
69 28
                $ddrRestClassMetadata->setMethods($methods);
70
            }
71
72 28
            if (null !== $restResourceAnnotation->methods) {
73 28
                $ddrRestClassMetadata->setMethods($restResourceAnnotation->methods);
0 ignored issues
show
Documentation introduced by
$restResourceAnnotation->methods is of type array<integer,object<Don...ata\Annotation\Method>>, but the function expects a array<integer,string>.

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...
74
            }
75
        }
76
77 30
        foreach ($class->getProperties() as $reflectionProperty) {
78
79 30
            $propertyMetadata = $ddrRestClassMetadata->getPropertyMetadata($reflectionProperty->getName());
80 30
            if (null === $propertyMetadata) {
81 28
                $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
82
            }
83
84 30
            $puttableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Puttable::class);
85 30
            if (null !== $puttableAnnotation) {
86 24
                $propertyMetadata->setPuttable(true);
87
            }
88
89 30
            $postableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Postable::class);
90 30
            if (null !== $postableAnnotation) {
91
                $propertyMetadata->setPostable(true);
92
            }
93
94 30
            $includableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Includable::class);
95 30
            if (null !== $includableAnnotation) {
96 28
                $paths = $includableAnnotation->paths;
97 28
                if (null === $paths) {
98 24
                    $paths = [$reflectionProperty->name];
99
                }
100 28
                $propertyMetadata->setIncludable(true);
101 28
                $propertyMetadata->setIncludablePaths($paths);
102
            }
103
104 30
            $excludedAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Excluded::class);
105 30
            if (null !== $excludedAnnotation) {
106 28
                $propertyMetadata->setExcluded(true);
107
            }
108
109
            /** @var SubResource $subResourceAnnotation */
110 30
            $subResourceAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, SubResource::class);
111 30
            if (null !== $subResourceAnnotation) {
112
113 24
                $propertyMetadata->setSubResource(true);
114
115 24
                if (null !== $subResourceAnnotation->path) {
116
                    $propertyMetadata->setSubResourcePath($subResourceAnnotation->path);
117
                }
118
119 24
                if (null !== $subResourceAnnotation->methods) {
120 24
                    $methods = [];
121 24
                    $methodAnnotations = $subResourceAnnotation->methods;
122 24
                    foreach ($methodAnnotations as $methodAnnotation) {
123 24
                        $methods[$methodAnnotation->name] = $methodAnnotation;
124
                    }
125 24
                    $propertyMetadata->setMethods($methods);
126
                }
127
            }
128
129 30
            $ddrRestClassMetadata->addPropertyMetadata($propertyMetadata);
130
        }
131
132 30
        return $ddrRestClassMetadata;
133
    }
134
}
135