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

PropertyResolverTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 40
ccs 10
cts 11
cp 0.9091
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolvePropertyMetadata() 0 12 1
A getCommentTypeHint() 0 9 2
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