Completed
Push — master ( d569a5...28eb0b )
by Vitaly
02:23
created

resolveClassPropertyAnnotations()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.0534
cc 4
eloc 10
nc 6
nop 2
crap 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 1
    public function resolve(\ReflectionClass $classData, ClassMetadata $classMetadata)
24
    {
25
        /** @var \ReflectionProperty $property */
26 1
        foreach ($classData->getProperties() as $property) {
27 1
            $this->resolveClassPropertyAnnotations($property, $this->classMetadata);
28
        }
29
30 1
        return $this->classMetadata;
31
    }
32
33
    /**
34
     * Resolve class property annotations.
35
     *
36
     * @param \ReflectionProperty $property
37
     * @param ClassMetadata       $classMetadata
38
     */
39 1
    protected function resolveClassPropertyAnnotations(\ReflectionProperty $property, ClassMetadata $classMetadata)
40
    {
41
        // Create method metadata instance
42 1
        $propertyMetadata = new PropertyMetadata($classMetadata);
43 1
        $propertyMetadata->name = $property->getName();
44 1
        $propertyMetadata->modifiers = $property->getModifiers();
45
46
        // Parse property type hint if present
47 1
        if (preg_match(self::P_PROPERTY_TYPE_HINT, $property->getDocComment(), $matches)) {
48 1
            list(, $propertyMetadata->typeHint) = $matches;
49
        }
50
51
        /** @var PropertyInterface $annotation Read class annotations */
52 1
        foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
53 1
            if ($annotation instanceof PropertyInterface) {
54 1
                $annotation->toPropertyMetadata($propertyMetadata);
55
            }
56
        }
57
58 1
        $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata;
59 1
    }
60
}
61