Completed
Push — master ( 3309bb...059e5b )
by Changwan
02:58
created

AnnotationManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D read() 0 30 9
1
<?php
2
namespace Wandu\Annotation;
3
4
use Doctrine\Common\Annotations\AnnotationReader;
5
use Doctrine\Common\Annotations\Reader;
6
use Psr\SimpleCache\CacheInterface;
7
use ReflectionClass;
8
9
class AnnotationManager
10
{
11
    /** @var \Doctrine\Common\Annotations\Reader */
12
    protected $reader;
13
    
14
    /** @var \Psr\SimpleCache\CacheInterface */
15
    protected $cache;
16
17
    /**
18
     * @param \Doctrine\Common\Annotations\Reader $reader
19
     * @param \Psr\SimpleCache\CacheInterface $cache
20
     */
21 1
    public function __construct(Reader $reader = null, CacheInterface $cache = null)
22
    {
23 1
        $this->reader = $reader ?? new AnnotationReader();
24 1
        $this->cache = $cache;
25 1
    }
26
27
    /**
28
     * @param string $className
29
     * @return \Wandu\Annotation\AnnotationBag
30
     */
31 1
    public function read(string $className): AnnotationBag
32
    {
33 1
        if ($this->cache && $this->cache->has($className)) {
34
            return $this->cache->get($className);
35
        }
36 1
        $reflClass = new ReflectionClass($className);
37 1
        $classAnnotations = [];
38 1
        foreach ($this->reader->getClassAnnotations($reflClass) as $classAnnotation) {
39 1
            $classAnnotations[] = $classAnnotation;
40
        }
41 1
        $propsAnnotations = [];
42 1
        foreach ($reflClass->getProperties() as $reflProperty) {
43 1
            $propsAnnotations[$reflProperty->getName()] = [];
44 1
            foreach ($this->reader->getPropertyAnnotations($reflProperty) as $propertyAnnotation) {
45 1
                $propsAnnotations[$reflProperty->getName()][] = $propertyAnnotation;
46
            }
47
        }
48 1
        $methodsAnnotations = [];
49 1
        foreach ($reflClass->getMethods() as $reflMethod) {
50 1
            $methodsAnnotations[$reflMethod->getName()] = [];
0 ignored issues
show
Bug introduced by
Consider using $reflMethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
51 1
            foreach ($this->reader->getMethodAnnotations($reflMethod) as $methodAnnotation) {
52 1
                $methodsAnnotations[$reflMethod->getName()][] = $methodAnnotation;
0 ignored issues
show
Bug introduced by
Consider using $reflMethod->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
53
            }
54
        }
55 1
        $result = new AnnotationBag($classAnnotations, $propsAnnotations, $methodsAnnotations);
56 1
        if ($this->cache) {
57
            $this->cache->set($className, $result);
58
        }
59 1
        return $result;
60
    }
61
}
62