Passed
Push — master ( cbecdb...34e2d9 )
by Arthur
07:30
created

PropertyData::getAnnotations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Larapie\DataTransferObject\Property;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Doctrine\Common\Annotations\Reader;
8
use Larapie\DataTransferObject\Annotations\Immutable;
9
use Larapie\DataTransferObject\Annotations\Optional;
10
use Larapie\DataTransferObject\Resolvers\AnnotationResolver;
11
use Larapie\DataTransferObject\Resolvers\ConstraintsResolver;
12
use Larapie\DataTransferObject\Resolvers\PropertyTypeResolver;
13
use ReflectionProperty;
14
15
class PropertyData
16
{
17
    /** @var string */
18
    protected $name;
19
20
    /** @var PropertyType */
21
    protected $type;
22
23
    /** @var string */
24
    protected $fqn;
25
26
    /** @var bool */
27
    protected $optional;
28
29
    /** @var bool */
30
    protected $immutable;
31
32
    /** @var array */
33
    protected $constraints;
34
35
    /** @var array */
36
    protected $annotations;
37
38
    /** @var Reader */
39
    private static $reader;
40
41
    /**
42
     * PropertyData constructor.
43
     * @param ReflectionProperty $property
44
     */
45
    public function __construct(ReflectionProperty $property)
46
    {
47
        $this->name = $property->getName();
48
        $this->fqn = "{$property->getDeclaringClass()->getName()}::{$property->getName()}";
49
        $this->boot($property);
50
    }
51
52
    protected function boot(reflectionProperty $reflectionProperty)
53
    {
54
        $this->annotations = $this->resolveAnnotations($reflectionProperty);
55
        $this->constraints = $this->resolveConstraints($reflectionProperty);
56
        $this->type = $this->resolveType($reflectionProperty);
57
        $this->immutable = $this->resolveImmutable();
58
        $this->optional = $this->resolveOptional();
59
    }
60
61
    protected function resolveType(ReflectionProperty $reflection)
62
    {
63
        return (new PropertyTypeResolver($reflection, $this->annotations))->resolve();
64
    }
65
66
    protected function resolveAnnotations(ReflectionProperty $reflection)
67
    {
68
        $annotations = [];
69
        foreach (self::getReader()->getPropertyAnnotations($reflection) as $annotation) {
70
            $annotations[] = $annotation;
71
        }
72
73
        return (new AnnotationResolver($reflection))->resolve();
74
    }
75
76
    protected function resolveOptional()
77
    {
78
        foreach ($this->annotations as $annotation) {
79
            if ($annotation instanceof Optional) {
80
                return true;
81
            }
82
        }
83
84
        return false;
85
    }
86
87
    protected function resolveImmutable()
88
    {
89
        foreach ($this->annotations as $annotation) {
90
            if ($annotation instanceof Immutable) {
91
                return true;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
    protected function resolveConstraints(ReflectionProperty $reflection)
99
    {
100
        return (new ConstraintsResolver($reflection, $this->annotations))->resolve();
101
    }
102
103
    protected static function getReader()
104
    {
105
        AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

105
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
106
        if (! isset(self::$reader)) {
107
            self::$reader = new AnnotationReader();
108
        }
109
110
        return self::$reader;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getName(): string
117
    {
118
        return $this->name;
119
    }
120
121
    /**
122
     * @return PropertyType
123
     */
124
    public function getType(): PropertyType
125
    {
126
        return $this->type;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getFqn(): string
133
    {
134
        return $this->fqn;
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    public function isOptional(): bool
141
    {
142
        return $this->optional;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function isImmutable(): bool
149
    {
150
        return $this->immutable;
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function getConstraints(): array
157
    {
158
        return $this->constraints;
159
    }
160
161
    /**
162
     * @return array
163
     */
164
    public function getAnnotations(): array
165
    {
166
        return $this->annotations;
167
    }
168
169
170
}
171