Completed
Push — master ( 8f61de...6beb52 )
by Vitaly
02:52
created

PropertyResolverTrait::resolvePropertyMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
crap 1
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 18.08.16 at 11:25
5
 */
6
namespace samsonframework\container\resolver;
7
8
use samsonframework\container\metadata\ClassMetadata;
9
use samsonframework\container\metadata\PropertyMetadata;
10
11
/**
12
 * Class property resolving trait.
13
 *
14
 * @author Vitaly Iegorov <[email protected]>
15
 */
16
trait PropertyResolverTrait
17
{
18
    /**
19
     * Generic class property resolver.
20
     *
21
     * @param \ReflectionProperty $property
22
     * @param ClassMetadata       $classMetadata
23
     *
24
     * @return PropertyMetadata Resolved property metadata
25
     */
26 11
    protected function resolvePropertyMetadata(\ReflectionProperty $property, ClassMetadata $classMetadata) : PropertyMetadata
27
    {
28
        // Create method metadata instance
29 11
        $propertyMetadata = new PropertyMetadata($classMetadata);
30 11
        $propertyMetadata->name = $property->getName();
31 11
        $propertyMetadata->modifiers = $property->getModifiers();
32 11
        $propertyMetadata->isPublic = $property->isPublic();
33 11
        $propertyMetadata->typeHint = $this->getCommentTypeHint($property->getDocComment());
34
35
        // Store property metadata to class metadata
36 11
        return $classMetadata->propertiesMetadata[$propertyMetadata->name] = $propertyMetadata;
37
    }
38
39
    /**
40
     * Parse property comments and return type hint if present.
41
     *
42
     * @param string $comments Property comments
43
     *
44
     * @return string Property type hint if present
45
     */
46 11
    protected function getCommentTypeHint(string $comments) : string
47
    {
48
        // Parse property type hint if present
49 11
        if (preg_match('/@var\s+(?<class>[^\s]+)/', $comments, $matches)) {
50 11
            return $matches[1];
51
        }
52
53
        return '';
54
    }
55
}
56