Completed
Push — standalone ( 46923f...8c7d4e )
by Philip
03:55
created

AnnotationDriver   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.8%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 7
dl 0
loc 122
ccs 56
cts 61
cp 0.918
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
F loadMetadataForClass() 0 103 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 46
    public function __construct(Reader $reader, DriverInterface $doctrineDriver)
25
    {
26 46
        $this->reader = $reader;
27 46
        $this->doctrineDriver = $doctrineDriver;
28 46
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 40
    public function loadMetadataForClass(\ReflectionClass $class)
34
    {
35
        /** @var ClassMetadata $ddrRestClassMetadata */
36 40
        $ddrRestClassMetadata = $this->doctrineDriver->loadMetadataForClass($class);
37 40
        if (null === $ddrRestClassMetadata) {
38
            $ddrRestClassMetadata = new ClassMetadata($class->getName());
39
        }
40
41
        /** @var RootResource $restResourceAnnotation */
42 40
        $restResourceAnnotation = $this->reader->getClassAnnotation($class, RootResource::class);
43 40
        if (null !== $restResourceAnnotation) {
44
45 38
            $ddrRestClassMetadata->setRestResource(true);
46
47 38
            if (null !== $restResourceAnnotation->namePrefix) {
48
                $ddrRestClassMetadata->setNamePrefix($restResourceAnnotation->namePrefix);
49
            }
50
51 38
            if (null !== $restResourceAnnotation->pathPrefix) {
52 34
                $ddrRestClassMetadata->setPathPrefix($restResourceAnnotation->pathPrefix);
53
            }
54
55 38
            if (null !== $restResourceAnnotation->service) {
56
                $ddrRestClassMetadata->setService($restResourceAnnotation->service);
57
            }
58
59 38
            if (null !== $restResourceAnnotation->controller) {
60
                $ddrRestClassMetadata->setController($restResourceAnnotation->controller);
61
            }
62
63 38
            $ddrRestClassMetadata->idField = $restResourceAnnotation->idField;
64
65 38
            if (null !== $restResourceAnnotation->methods) {
66 38
                $methods = [];
67 38
                $methodAnnotations = $restResourceAnnotation->methods;
68 38
                foreach ($methodAnnotations as $methodAnnotation) {
69 38
                    $methods[$methodAnnotation->name] = $methodAnnotation;
70
                }
71 38
                $ddrRestClassMetadata->setMethods($methods);
72
            }
73
74 38
            if (null !== $restResourceAnnotation->methods) {
75 38
                $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...
76
            }
77
        }
78
79 40
        foreach ($class->getProperties() as $reflectionProperty) {
80
81 40
            $propertyMetadata = $ddrRestClassMetadata->getPropertyMetadata($reflectionProperty->getName());
82 40
            if (null === $propertyMetadata) {
83 38
                $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
84
            }
85
86 40
            $puttableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Puttable::class);
87 40
            if (null !== $puttableAnnotation) {
88 34
                $propertyMetadata->setPuttable(true);
89
            }
90
91 40
            $postableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Postable::class);
92 40
            if (null !== $postableAnnotation) {
93 34
                $propertyMetadata->setPostable(true);
94
            }
95
96 40
            $includableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Includable::class);
97 40
            if (null !== $includableAnnotation) {
98 38
                $paths = $includableAnnotation->paths;
99 38
                if (null === $paths) {
100 34
                    $paths = [$reflectionProperty->name];
101
                }
102 38
                $propertyMetadata->setIncludable(true);
103 38
                $propertyMetadata->setIncludablePaths($paths);
104
            }
105
106 40
            $excludedAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Excluded::class);
107 40
            if (null !== $excludedAnnotation) {
108 38
                $propertyMetadata->setExcluded(true);
109
            }
110
111
            /** @var SubResource $subResourceAnnotation */
112 40
            $subResourceAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, SubResource::class);
113 40
            if (null !== $subResourceAnnotation) {
114
115 34
                $propertyMetadata->setSubResource(true);
116
117 34
                if (null !== $subResourceAnnotation->path) {
118
                    $propertyMetadata->setSubResourcePath($subResourceAnnotation->path);
119
                }
120
121 34
                if (null !== $subResourceAnnotation->methods) {
122 34
                    $methods = [];
123 34
                    $methodAnnotations = $subResourceAnnotation->methods;
124 34
                    foreach ($methodAnnotations as $methodAnnotation) {
125 34
                        $methods[$methodAnnotation->name] = $methodAnnotation;
126
                    }
127 34
                    $propertyMetadata->setMethods($methods);
128
                }
129
            }
130
131 40
            $ddrRestClassMetadata->addPropertyMetadata($propertyMetadata);
132
        }
133
134 40
        return $ddrRestClassMetadata;
135
    }
136
}
137