Passed
Pull Request — master (#61)
by Daniel
06:02
created

AnnotationReader   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 95.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 102
ccs 41
cts 43
cp 0.9535
rs 10
wmc 22

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfigurationFromParentClasses() 0 13 3
A findAnnotationConfiguration() 0 15 5
A isConfigured() 0 9 2
A getConfigurationFromTraits() 0 12 3
B getClassAnnotationConfiguration() 0 16 8
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\AnnotationReader;
15
16
use Doctrine\Common\Annotations\Reader;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
19
use Silverback\ApiComponentsBundle\Utility\ClassMetadataTrait;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 * @author Daniel West <[email protected]>
24
 */
25
abstract class AnnotationReader implements AnnotationReaderInterface
26
{
27
    use ClassMetadataTrait;
28
29
    protected Reader $reader;
30
31
    private array $configurationCache = [];
32
33 11
    public function __construct(Reader $reader, ManagerRegistry $managerRegistry)
34
    {
35 11
        $this->reader = $reader;
36 11
        $this->initRegistry($managerRegistry);
37 11
    }
38
39
    abstract public function getConfiguration($class);
40
41
    /**
42
     * @param object|string $class
43
     */
44 11
    public function isConfigured($class): bool
45
    {
46
        try {
47 11
            $this->getConfiguration($class);
48 11
        } catch (InvalidArgumentException $e) {
49 11
            return false;
50
        }
51
52 11
        return true;
53
    }
54
55
    /**
56
     * @param object|string $class
57
     *
58
     * @throws \ReflectionException
59
     */
60 11
    protected function getClassAnnotationConfiguration($class, string $annotationClass): ?object
61
    {
62 11
        if (null === $class || (!\is_object($class) && !\is_string($class)) || (\is_string($class) && !class_exists($class))) {
0 ignored issues
show
introduced by
The condition is_string($class) is always true.
Loading history...
63
            throw new InvalidArgumentException(sprintf('$class passed to %s must be a valid class FQN or object.', __CLASS__));
64
        }
65
66 11
        $className = \is_object($class) ? \get_class($class) : $class;
67 11
        if (\array_key_exists($className, $this->configurationCache)) {
68 11
            return $this->configurationCache[$className];
69
        }
70
71 11
        $annotation = $this->findAnnotationConfiguration($class, $annotationClass);
72
73 11
        $this->configurationCache[$className] = $annotation;
74
75 11
        return $annotation;
76
    }
77
78
    /**
79
     * @param string|object $class
80
     *
81
     * @throws \ReflectionException
82
     */
83 11
    private function findAnnotationConfiguration($class, string $annotationClass): ?object
84
    {
85 11
        $reflection = new \ReflectionClass($class);
86 11
        $annotation = $this->reader->getClassAnnotation($reflection, $annotationClass);
87 11
        if (!$annotation) {
88 11
            $annotation = $this->getConfigurationFromParentClasses($reflection, $annotationClass);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $annotation is correct as $this->getConfigurationF...tion, $annotationClass) targeting Silverback\ApiComponents...tionFromParentClasses() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
89 11
            if (!$annotation) {
0 ignored issues
show
introduced by
$annotation is of type null, thus it always evaluated to false.
Loading history...
90 11
                $annotation = $this->getConfigurationFromTraits($reflection, $annotationClass);
91 11
                if (!$annotation) {
92 11
                    throw new InvalidArgumentException(sprintf('%s does not have %s annotation', \is_object($class) ? \get_class($class) : $class, $annotationClass));
93
                }
94
            }
95
        }
96
97 11
        return $annotation;
98
    }
99
100 11
    private function getConfigurationFromParentClasses(\ReflectionClass $reflection, string $annotationClass): ?object
101
    {
102 11
        $annotation = null;
103
104 11
        $parentReflection = $reflection->getParentClass();
105
        while (
106 11
            $parentReflection &&
107 11
            !$annotation = $this->reader->getClassAnnotation($parentReflection, $annotationClass)
108
        ) {
109 11
            $parentReflection = $parentReflection->getParentClass();
110
        }
111
112 11
        return $annotation;
113
    }
114
115 11
    private function getConfigurationFromTraits(\ReflectionClass $reflection, string $annotationClass): ?object
116
    {
117 11
        $annotation = null;
118 11
        $traits = $reflection->getTraits();
119 11
        foreach ($traits as $trait) {
120 11
            $annotation = $this->reader->getClassAnnotation($trait, $annotationClass);
121 11
            if ($annotation) {
122
                break;
123
            }
124
        }
125
126 11
        return $annotation;
127
    }
128
}
129