Completed
Push — master ( 845de9...0081d5 )
by Arthur
09:56 queued 10s
created

PropertyData   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 18
eloc 34
dl 0
loc 134
ccs 42
cts 44
cp 0.9545
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolveOptional() 0 9 3
A getAnnotations() 0 3 1
A resolveType() 0 3 1
A boot() 0 7 1
A resolveConstraints() 0 3 1
A resolveImmutable() 0 9 3
A getConstraints() 0 3 1
A getName() 0 3 1
A getFqn() 0 3 1
A isOptional() 0 3 1
A resolveAnnotations() 0 3 1
A isImmutable() 0 3 1
A getType() 0 3 1
1
<?php
2
3
namespace Larapie\DataTransferObject\Property;
4
5
use Larapie\DataTransferObject\Annotations\Immutable;
6
use Larapie\DataTransferObject\Annotations\Optional;
7
use Larapie\DataTransferObject\Resolvers\AnnotationResolver;
8
use Larapie\DataTransferObject\Resolvers\ConstraintsResolver;
9
use Larapie\DataTransferObject\Resolvers\PropertyTypeResolver;
10
use ReflectionProperty;
11
12
class PropertyData
13
{
14
    /** @var string */
15
    protected $name;
16
17
    /** @var PropertyType */
18
    protected $type;
19
20
    /** @var string */
21
    protected $fqn;
22
23
    /** @var bool */
24
    protected $optional;
25
26
    /** @var bool */
27
    protected $immutable;
28
29
    /** @var array */
30
    protected $constraints;
31
32
    /** @var array */
33
    protected $annotations;
34
35
    /**
36
     * PropertyData constructor.
37
     * @param ReflectionProperty $property
38
     */
39
    public function __construct(ReflectionProperty $property)
40
    {
41
        $this->name = $property->getName();
42
        $this->fqn = "{$property->getDeclaringClass()->getName()}::{$property->getName()}";
43
        $this->boot($property);
44
    }
45 36
46
    protected function boot(reflectionProperty $reflectionProperty)
47 36
    {
48 36
        $this->annotations = $this->resolveAnnotations($reflectionProperty);
49 36
        $this->constraints = $this->resolveConstraints($reflectionProperty);
50 35
        $this->type = $this->resolveType($reflectionProperty);
51
        $this->immutable = $this->resolveImmutable();
52 36
        $this->optional = $this->resolveOptional();
53
    }
54 36
55 36
    protected function resolveType(ReflectionProperty $reflection)
56 36
    {
57 35
        return (new PropertyTypeResolver($reflection, $this->annotations))->resolve();
58 35
    }
59 35
60
    protected function resolveAnnotations(ReflectionProperty $reflection)
61 36
    {
62
        return (new AnnotationResolver($reflection))->resolve();
63 36
    }
64
65
    protected function resolveOptional()
66 36
    {
67
        foreach ($this->annotations as $annotation) {
68 36
            if ($annotation instanceof Optional) {
69 36
                return true;
70 7
            }
71
        }
72
73 36
        return false;
74
    }
75
76 35
    protected function resolveImmutable()
77
    {
78 35
        foreach ($this->annotations as $annotation) {
79 7
            if ($annotation instanceof Immutable) {
80 7
                return true;
81
            }
82
        }
83
84 34
        return false;
85
    }
86
87 35
    protected function resolveConstraints(ReflectionProperty $reflection)
88
    {
89 35
        return (new ConstraintsResolver($reflection, $this->annotations))->resolve();
90 7
    }
91 7
92
    /**
93
     * @return string
94
     */
95 34
    public function getName(): string
96
    {
97
        return $this->name;
98 36
    }
99
100 36
    /**
101
     * @return PropertyType
102
     */
103 36
    public function getType(): PropertyType
104
    {
105 36
        return $this->type;
106 36
    }
107 1
108
    /**
109
     * @return string
110 36
     */
111
    public function getFqn(): string
112
    {
113
        return $this->fqn;
114
    }
115
116 44
    /**
117
     * @return bool
118 44
     */
119
    public function isOptional(): bool
120
    {
121
        return $this->optional;
122
    }
123
124 41
    /**
125
     * @return bool
126 41
     */
127
    public function isImmutable(): bool
128
    {
129
        return $this->immutable;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getConstraints(): array
136
    {
137
        return $this->constraints;
138
    }
139
140 44
    /**
141
     * @return array
142 44
     */
143
    public function getAnnotations(): array
144
    {
145
        return $this->annotations;
146
    }
147
148 36
149
}
150