Completed
Push — master ( 6ba652...6f597b )
by Vitaly
02:20
created

AnnotationPropertyResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 5
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
A resolveClassPropertyAnnotations() 0 21 4
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.08.16 at 13:04
5
 */
6
namespace samsonframework\container\resolver;
7
8
use samsonframework\container\annotation\PropertyInterface;
9
use samsonframework\container\metadata\ClassMetadata;
10
use samsonframework\container\metadata\PropertyMetadata;
11
12
/**
13
 * Class properties annotation resolver.
14
 */
15
class AnnotationPropertyResolver extends AbstractAnnotationResolver implements AnnotationResolverInterface
16
{
17
    /** Property typeHint hint pattern */
18
    const P_PROPERTY_TYPE_HINT = '/@var\s+(?<class>[^\s]+)/';
19
20
    /**
21
     * {@inheritDoc}
22
     */
23
    public function resolve(\ReflectionClass $classData, ClassMetadata $classMetadata)
24
    {
25
        /** @var \ReflectionProperty $property */
26
        foreach ($classData->getProperties() as $property) {
27
            $this->resolveClassPropertyAnnotations($property, $this->classMetadata);
28
        }
29
30
        return $this->classMetadata;
31
    }
32
33
    /**
34
     * Resolve class property annotations.
35
     *
36
     * @param \ReflectionProperty $property
37
     * @param ClassMetadata       $classMetadata
38
     */
39
    protected function resolveClassPropertyAnnotations(\ReflectionProperty $property, ClassMetadata $classMetadata)
40
    {
41
        // Create method metadata instance
42
        $propertyMetadata = new PropertyMetadata($classMetadata);
43
        $propertyMetadata->name = $property->getName();
44
        $propertyMetadata->modifiers = $property->getModifiers();
45
46
        // Parse property type hint if present
47
        if (preg_match('/@var\s+(?<class>[^\s]+)/', $property->getDocComment(), $matches)) {
48
            list(, $propertyMetadata->typeHint) = $matches;
49
        }
50
51
        /** @var PropertyInterface $annotation Read class annotations */
52
        foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
53
            if ($annotation instanceof PropertyInterface) {
54
                $annotation->toPropertyMetadata($propertyMetadata);
55
            }
56
        }
57
58
        $classMetadata->propertyMetadata[$propertyMetadata->name] = $propertyMetadata;
59
    }
60
}
61