Completed
Push — master ( 019907...013ee6 )
by Vitaly
03:05
created

AnnotationPropertyResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 47
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
B resolveClassPropertyAnnotations() 0 22 4
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 07.08.16 at 13:04
5
 */
6
namespace samsonframework\container\annotation;
7
8
use samsonframework\container\configurator\PropertyConfiguratorInterface;
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 11
    public function resolve(\ReflectionClass $classReflection, ClassMetadata $classMetadata)
24
    {
25
        /** @var \ReflectionProperty $property */
26 11
        foreach ($classReflection->getProperties() as $property) {
27 10
            $this->resolveClassPropertyAnnotations($property, $classMetadata);
28
        }
29
30 11
        return $classMetadata;
31
    }
32
33
    /**
34
     * Resolve class property annotations.
35
     *
36
     * @param \ReflectionProperty $property
37
     * @param ClassMetadata       $classMetadata
38
     */
39 10
    protected function resolveClassPropertyAnnotations(\ReflectionProperty $property, ClassMetadata $classMetadata)
40
    {
41
        // Create method metadata instance
42 10
        $propertyMetadata = new PropertyMetadata($classMetadata);
43 10
        $propertyMetadata->name = $property->getName();
44 10
        $propertyMetadata->modifiers = $property->getModifiers();
45 10
        $propertyMetadata->isPublic = $property->isPublic();
46
47
        // Parse property type hint if present
48 10
        if (preg_match(self::P_PROPERTY_TYPE_HINT, $property->getDocComment(), $matches)) {
49 10
            list(, $propertyMetadata->typeHint) = $matches;
50
        }
51
52
        /** @var PropertyInterface $annotation Read class annotations */
53 10
        foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
54 10
            if ($annotation instanceof PropertyConfiguratorInterface) {
55 10
                $annotation->toPropertyMetadata($propertyMetadata);
56
            }
57
        }
58
59 10
        $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata;
60 10
    }
61
}
62