Passed
Push — master ( 0756fe...706dc7 )
by Arthur
14:47
created

PropertyData   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
eloc 44
dl 0
loc 140
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getFqn() 0 3 1
A resolveOptional() 0 7 3
A resolveImmutable() 0 7 3
A resolveConstraints() 0 8 3
A isImmutable() 0 3 1
A isOptional() 0 3 1
A getType() 0 3 1
A getReader() 0 7 2
A resolveType() 0 3 1
A resolveAnnotations() 0 7 2
A boot() 0 7 1
A __construct() 0 5 1
A getName() 0 3 1
A getConstraints() 0 3 1
1
<?php
2
3
4
namespace Larapie\DataTransferObject;
5
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Annotations\AnnotationRegistry;
9
use Doctrine\Common\Annotations\Reader;
10
use Larapie\DataTransferObject\Annotations\Immutable;
11
use Larapie\DataTransferObject\Annotations\Optional;
12
use Larapie\DataTransferObject\Resolvers\AnnotationResolver;
13
use Larapie\DataTransferObject\Resolvers\TypeResolver;
14
use ReflectionProperty;
15
use Symfony\Component\Validator\Constraint;
16
17
class PropertyData
18
{
19
    /** @var string */
20
    protected $name;
21
22
    /** @var PropertyType */
23
    protected $type;
24
25
    /** @var string */
26
    protected $fqn;
27
28
    /** @var bool */
29
    protected $optional;
30
31
    /** @var bool */
32
    protected $immutable;
33
34
    /** @var array */
35
    protected $constraints;
36
37
    /** @var Reader */
38
    private static $reader;
39
40
    /**
41
     * PropertyData constructor.
42
     * @param ReflectionProperty $property
43
     */
44
    public function __construct(ReflectionProperty $property)
45
    {
46
        $this->name = $property->getName();
47
        $this->fqn = "{$property->getDeclaringClass()->getName()}::{$property->getName()}";;
48
        $this->boot($property);
49
    }
50
51
    protected function boot(reflectionProperty $reflectionProperty)
52
    {
53
        $annotations = $this->resolveAnnotations($reflectionProperty);
54
        $this->type = $this->resolveType($reflectionProperty);
55
        $this->immutable = $this->resolveImmutable($annotations);
56
        $this->constraints = $this->resolveConstraints($annotations);
57
        $this->optional = $this->resolveOptional($annotations);
58
    }
59
60
    protected function resolveType(ReflectionProperty $reflection)
61
    {
62
        return (new TypeResolver($reflection))->resolve();
63
    }
64
65
    protected function resolveAnnotations(ReflectionProperty $reflection)
66
    {
67
        $annotations = [];
68
        foreach (self::getReader()->getPropertyAnnotations($reflection) as $annotation) {
69
            $annotations[] = $annotation;
70
        }
71
        return (new AnnotationResolver($reflection))->resolve();
72
    }
73
74
    protected function resolveOptional($annotations)
75
    {
76
        foreach ($annotations as $annotation) {
77
            if ($annotation instanceof Optional)
78
                return true;
79
        }
80
        return false;
81
    }
82
83
    protected function resolveImmutable($annotations)
84
    {
85
        foreach ($annotations as $annotation) {
86
            if ($annotation instanceof Immutable)
87
                return true;
88
        }
89
        return false;
90
    }
91
92
    protected function resolveConstraints($annotations)
93
    {
94
        $constraints = [];
95
        foreach ($annotations as $annotation) {
96
            if ($annotation instanceof Constraint)
97
                $constraints[] = $annotation;
98
        }
99
        return $constraints;
100
    }
101
102
    protected static function getReader()
103
    {
104
        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

104
        /** @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...
105
        if (!isset(self::$reader)) {
106
            self::$reader = new AnnotationReader();
107
        }
108
        return self::$reader;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getName(): string
115
    {
116
        return $this->name;
117
    }
118
119
    /**
120
     * @return PropertyType
121
     */
122
    public function getType(): PropertyType
123
    {
124
        return $this->type;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getFqn(): string
131
    {
132
        return $this->fqn;
133
    }
134
135
    /**
136
     * @return bool
137
     */
138
    public function isOptional(): bool
139
    {
140
        return $this->optional;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146
    public function isImmutable(): bool
147
    {
148
        return $this->immutable;
149
    }
150
151
    /**
152
     * @return array
153
     */
154
    public function getConstraints(): array
155
    {
156
        return $this->constraints;
157
    }
158
159
}