ExtendReaderWithConfigReader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPropertyAnnotation() 0 3 1
A getMethodAnnotation() 0 3 1
A getMethodAnnotations() 0 3 1
A __construct() 0 4 1
A getClassAnnotations() 0 12 2
A getClassAnnotation() 0 7 3
A getPropertyAnnotations() 0 3 1
1
<?php
2
namespace Apie\FakeAnnotationsPlugin\Readers;
3
4
use Apie\Core\Annotations\ApiResource;
5
use Doctrine\Common\Annotations\Reader;
6
use ReflectionClass;
7
use ReflectionMethod;
8
use ReflectionProperty;
9
10
/**
11
 * Extend the annotation reader with a config array to override the actual annotations if required. Should only
12
 * work on the ApiResource annotation.
13
 */
14
class ExtendReaderWithConfigReader implements Reader
15
{
16
    private $reader;
17
18
    private $config;
19
20
    /**
21
     * @param Reader $reader
22
     * @param ApiResource[] $config
23
     */
24
    public function __construct(Reader $reader, array $config)
25
    {
26
        $this->reader = $reader;
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function getClassAnnotations(ReflectionClass $class)
34
    {
35
        $className = $class->getName();
36
        $annotations = $this->reader->getClassAnnotations($class);
37
        if (isset($this->config[$className])) {
38
            $annotations = array_filter($annotations, function ($annotation) {
39
                return !($annotation instanceof ApiResource);
40
            });
41
            $annotations[] = $this->config[$className];
42
        }
43
44
        return $annotations;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function getClassAnnotation(ReflectionClass $class, $annotationName)
51
    {
52
        $className = $class->getName();
53
        if ($annotationName === ApiResource::class && isset($this->config[$className])) {
54
            return $this->config[$className];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->config[$className] returns the type Apie\Core\Annotations\ApiResource which is incompatible with the return type mandated by Doctrine\Common\Annotati...r::getClassAnnotation() of Doctrine\Common\Annotations\T|null.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
55
        }
56
        return $this->reader->getClassAnnotation($class, $annotationName);
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function getMethodAnnotations(ReflectionMethod $method)
63
    {
64
        return $this->reader->getMethodAnnotations($method);
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
71
    {
72
        return $this->reader->getMethodAnnotation($method, $annotationName);
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function getPropertyAnnotations(ReflectionProperty $property)
79
    {
80
        return $this->reader->getPropertyAnnotations($property);
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
87
    {
88
        return $this->reader->getPropertyAnnotation($property, $annotationName);
89
    }
90
}
91