AnnotationDriver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 53
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B loadMetadataForClass() 0 30 6
A __construct() 0 8 1
1
<?php
2
3
namespace Vox\Metadata\Driver;
4
5
use Doctrine\Common\Annotations\IndexedReader;
6
use Doctrine\Common\Annotations\Reader;
7
use Metadata\ClassMetadata as BaseClassMetadata;
8
use Metadata\Driver\DriverInterface;
9
use Metadata\MethodMetadata;
10
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface;
11
use ReflectionClass;
12
use Vox\Metadata\ClassMetadata;
13
use Vox\Metadata\PropertyMetadata;
14
15
/**
16
 * Driver to create classes metadata using annotations, depends on doctrine's annotation reader
17
 * 
18
 * @author Jhonatan Teixeira <[email protected]>
19
 */
20
class AnnotationDriver implements DriverInterface
21
{
22
    use TypeFromSetterTrait;
0 ignored issues
show
introduced by
The trait Vox\Metadata\Driver\TypeFromSetterTrait requires some properties which are not provided by Vox\Metadata\Driver\AnnotationDriver: $name, $methodMetadata, $reflection
Loading history...
23
    
24
    /**
25
     * @var Reader
26
     */
27
    private $annotationReader;
28
    
29
    private $classMetadataClassName;
30
    
31
    private $propertyMetadataClassName;
32
    
33 60
    public function __construct(
34
        Reader $annotationReader,
35
        string $classMetadataClassName = ClassMetadata::class,
36
        string $propertyMetadataClassName = PropertyMetadata::class
37
    ) {
38 60
        $this->annotationReader          = new IndexedReader($annotationReader);
39 60
        $this->classMetadataClassName    = $classMetadataClassName;
40 60
        $this->propertyMetadataClassName = $propertyMetadataClassName;
41 60
    }
42
    
43 58
    public function loadMetadataForClass(ReflectionClass $class): BaseClassMetadata
44
    {
45 58
        if ($class->implementsInterface(AccessInterceptorValueHolderInterface::class)) {
46 19
            $class = $class->getParentClass();
47
        }
48
49
        /* @var $classMetadata ClassMetadata */
50 58
        $classMetadata    = (new ReflectionClass($this->classMetadataClassName))->newInstance($class->name);
51 58
        $classAnnotations = $this->annotationReader->getClassAnnotations($class);
52
53 58
        $classMetadata->setAnnotations($classAnnotations);
54
        
55 58
        foreach ($class->getMethods() as $method) {
56 48
            $classMetadata->addMethodMetadata(new MethodMetadata($class->name, $method->name));
57
        }
58
        
59 58
        foreach ($class->getProperties() as $property) {
60 58
            $propertyAnnotations = $this->annotationReader->getPropertyAnnotations($property);
61 58
            $propertyMetadata = (new ReflectionClass($this->propertyMetadataClassName))
62 58
                ->newInstance($class->name, $property->name);
63 58
            $propertyMetadata->setAnnotations($propertyAnnotations);
64
            
65 58
            if (property_exists($propertyMetadata, 'type') && empty($propertyMetadata->type)) {
66 28
                $propertyMetadata->type = $this->getTypeFromSetter($propertyMetadata, $classMetadata);
67
            }
68
            
69 58
            $classMetadata->addPropertyMetadata($propertyMetadata);
70
        }
71
        
72 58
        return $classMetadata;
73
    }
74
}
75