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

AnnotationManager::read()   D

Complexity

Conditions 9
Paths 37

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.0698

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 37
nop 1
dl 0
loc 30
ccs 19
cts 21
cp 0.9048
crap 9.0698
rs 4.909
c 0
b 0
f 0
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