Completed
Push — standalone ( a45559...3ee90b )
by Philip
04:55
created

AnnotationDriver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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
            $ddrRestClassMetadata->idField = $restResourceAnnotation->idField;
64
65 28
            if (null !== $restResourceAnnotation->methods) {
66 28
                $methods = [];
67 28
                $methodAnnotations = $restResourceAnnotation->methods;
68 28
                foreach ($methodAnnotations as $methodAnnotation) {
69 28
                    $methods[$methodAnnotation->name] = $methodAnnotation;
70
                }
71 28
                $ddrRestClassMetadata->setMethods($methods);
72
            }
73
74 28
            if (null !== $restResourceAnnotation->methods) {
75 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...
76
            }
77
        }
78
79 30
        foreach ($class->getProperties() as $reflectionProperty) {
80
81 30
            $propertyMetadata = $ddrRestClassMetadata->getPropertyMetadata($reflectionProperty->getName());
82 30
            if (null === $propertyMetadata) {
83 28
                $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName());
84
            }
85
86 30
            $puttableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Puttable::class);
87 30
            if (null !== $puttableAnnotation) {
88 24
                $propertyMetadata->setPuttable(true);
89
            }
90
91 30
            $postableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Postable::class);
92 30
            if (null !== $postableAnnotation) {
93
                $propertyMetadata->setPostable(true);
94
            }
95
96 30
            $includableAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Includable::class);
97 30
            if (null !== $includableAnnotation) {
98 28
                $paths = $includableAnnotation->paths;
99 28
                if (null === $paths) {
100 24
                    $paths = [$reflectionProperty->name];
101
                }
102 28
                $propertyMetadata->setIncludable(true);
103 28
                $propertyMetadata->setIncludablePaths($paths);
104
            }
105
106 30
            $excludedAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, Excluded::class);
107 30
            if (null !== $excludedAnnotation) {
108 28
                $propertyMetadata->setExcluded(true);
109
            }
110
111
            /** @var SubResource $subResourceAnnotation */
112 30
            $subResourceAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, SubResource::class);
113 30
            if (null !== $subResourceAnnotation) {
114
115 24
                $propertyMetadata->setSubResource(true);
116
117 24
                if (null !== $subResourceAnnotation->path) {
118
                    $propertyMetadata->setSubResourcePath($subResourceAnnotation->path);
119
                }
120
121 24
                if (null !== $subResourceAnnotation->methods) {
122 24
                    $methods = [];
123 24
                    $methodAnnotations = $subResourceAnnotation->methods;
124 24
                    foreach ($methodAnnotations as $methodAnnotation) {
125 24
                        $methods[$methodAnnotation->name] = $methodAnnotation;
126
                    }
127 24
                    $propertyMetadata->setMethods($methods);
128
                }
129
            }
130
131 30
            $ddrRestClassMetadata->addPropertyMetadata($propertyMetadata);
132
        }
133
134 30
        return $ddrRestClassMetadata;
135
    }
136
}
137