1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Gson\Internal\Data; |
8
|
|
|
|
9
|
|
|
use Doctrine\Common\Annotations\Reader; |
10
|
|
|
use Doctrine\Common\Cache\Cache; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use ReflectionProperty; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class AnnotationCollectionFactory |
17
|
|
|
* |
18
|
|
|
* @author Nate Brunette <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class AnnotationCollectionFactory |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Doctrine annotation reader |
24
|
|
|
* |
25
|
|
|
* @var Reader |
26
|
|
|
*/ |
27
|
|
|
private $reader; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cache for collection of annotations |
31
|
|
|
* |
32
|
|
|
* @var Cache |
33
|
|
|
*/ |
34
|
|
|
private $cache; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor |
38
|
|
|
* |
39
|
|
|
* @param Reader $reader |
40
|
|
|
* @param Cache $cache |
41
|
|
|
*/ |
42
|
10 |
|
public function __construct(Reader $reader, Cache $cache) |
43
|
|
|
{ |
44
|
10 |
|
$this->reader = $reader; |
45
|
10 |
|
$this->cache = $cache; |
46
|
10 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a set of property annotations |
50
|
|
|
* |
51
|
|
|
* @param string $className |
52
|
|
|
* @param string $propertyName |
53
|
|
|
* @return AnnotationSet |
54
|
|
|
* @throws \InvalidArgumentException If the type does not exist |
55
|
|
|
*/ |
56
|
3 |
View Code Duplication |
public function createPropertyAnnotations(string $className, string $propertyName): AnnotationSet |
|
|
|
|
57
|
|
|
{ |
58
|
3 |
|
$key = $className.':'.$propertyName; |
59
|
3 |
|
if ($this->cache->contains($key)) { |
60
|
1 |
|
return $this->cache->fetch($key); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$reflectionProperty = new ReflectionProperty($className, $propertyName); |
64
|
|
|
|
65
|
2 |
|
$annotations = new AnnotationSet(); |
66
|
|
|
|
67
|
|
|
// start with with all property annotations |
68
|
2 |
|
foreach ($this->reader->getPropertyAnnotations($reflectionProperty) as $defaultAnnotation) { |
69
|
2 |
|
$annotations->addAnnotation($defaultAnnotation, AnnotationSet::TYPE_PROPERTY); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$reflectionClass = $reflectionProperty->getDeclaringClass(); |
73
|
2 |
|
$parentClass = $reflectionClass->getParentClass(); |
74
|
|
|
|
75
|
2 |
|
while (false !== $parentClass) { |
76
|
|
|
// add parent property annotations if they exist |
77
|
1 |
|
if ($parentClass->hasProperty($reflectionProperty->getName())) { |
78
|
1 |
|
$parentProperty = $parentClass->getProperty($reflectionProperty->getName()); |
79
|
1 |
|
foreach ($this->reader->getPropertyAnnotations($parentProperty) as $parentPropertyAnnotation) { |
80
|
1 |
|
$annotations->addAnnotation($parentPropertyAnnotation, AnnotationSet::TYPE_PROPERTY); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// reset $parentClass |
85
|
1 |
|
$parentClass = $parentClass->getParentClass(); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$this->cache->save($key, $annotations); |
89
|
|
|
|
90
|
2 |
|
return $annotations; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create a set of class annotations |
95
|
|
|
* |
96
|
|
|
* @param string $className |
97
|
|
|
* @return AnnotationSet |
98
|
|
|
* @throws \InvalidArgumentException If the type does not exist |
99
|
|
|
*/ |
100
|
3 |
|
public function createClassAnnotations(string $className): AnnotationSet |
101
|
|
|
{ |
102
|
3 |
|
if ($this->cache->contains($className)) { |
103
|
1 |
|
return $this->cache->fetch($className); |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
$reflectionClass = new ReflectionClass($className); |
107
|
|
|
|
108
|
2 |
|
$annotations = new AnnotationSet(); |
109
|
2 |
|
foreach ($this->reader->getClassAnnotations($reflectionClass) as $annotation) { |
110
|
2 |
|
$annotations->addAnnotation($annotation, AnnotationSet::TYPE_CLASS); |
111
|
|
|
} |
112
|
2 |
|
$parentClass = $reflectionClass->getParentClass(); |
113
|
|
|
|
114
|
2 |
|
while (false !== $parentClass) { |
115
|
1 |
|
foreach ($this->reader->getClassAnnotations($parentClass) as $parentAnnotation) { |
116
|
1 |
|
$annotations->addAnnotation($parentAnnotation, AnnotationSet::TYPE_CLASS); |
117
|
|
|
} |
118
|
1 |
|
$parentClass = $parentClass->getParentClass(); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$this->cache->save($className, $annotations); |
122
|
|
|
|
123
|
2 |
|
return $annotations; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Create a set of method annotations |
128
|
|
|
* |
129
|
|
|
* @param string $className |
130
|
|
|
* @param string $methodName |
131
|
|
|
* @return AnnotationSet |
132
|
|
|
* @throws \InvalidArgumentException If the type does not exist |
133
|
|
|
*/ |
134
|
3 |
View Code Duplication |
public function createMethodAnnotations(string $className, string $methodName): AnnotationSet |
|
|
|
|
135
|
|
|
{ |
136
|
3 |
|
$key = $className.':'.$methodName; |
137
|
3 |
|
if ($this->cache->contains($key)) { |
138
|
1 |
|
return $this->cache->fetch($key); |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
$annotations = new AnnotationSet(); |
142
|
|
|
|
143
|
2 |
|
$reflectionMethod = new ReflectionMethod($className, $methodName); |
144
|
2 |
|
foreach ($this->reader->getMethodAnnotations($reflectionMethod) as $annotation) { |
145
|
2 |
|
$annotations->addAnnotation($annotation, AnnotationSet::TYPE_METHOD); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
$parentClass = $reflectionMethod->getDeclaringClass()->getParentClass(); |
149
|
2 |
|
while (false !== $parentClass) { |
150
|
|
|
// add parent property annotations if they exist |
151
|
1 |
|
if ($parentClass->hasMethod($reflectionMethod->getName())) { |
152
|
1 |
|
$parentMethod = $parentClass->getMethod($reflectionMethod->getName()); |
153
|
1 |
|
foreach ($this->reader->getMethodAnnotations($parentMethod) as $parentMethodAnnotation) { |
154
|
1 |
|
$annotations->addAnnotation($parentMethodAnnotation, AnnotationSet::TYPE_METHOD); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// reset $parentClass |
159
|
1 |
|
$parentClass = $parentClass->getParentClass(); |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
$this->cache->save($key, $annotations); |
163
|
|
|
|
164
|
2 |
|
return $annotations; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.