|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiGen\Parser\Reflection\Extractors; |
|
4
|
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface; |
|
6
|
|
|
use ApiGen\Contracts\Parser\Reflection\Extractors\ClassTraitElementsExtractorInterface; |
|
7
|
|
|
use ApiGen\Contracts\Parser\Reflection\MethodReflectionInterface; |
|
8
|
|
|
use ApiGen\Contracts\Parser\Reflection\PropertyReflectionInterface; |
|
9
|
|
|
use TokenReflection\IReflection; |
|
10
|
|
|
|
|
11
|
|
|
final class ClassTraitElementsExtractor implements ClassTraitElementsExtractorInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ClassReflectionInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $classReflection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \TokenReflection\IReflection|ClassReflectionInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $originalReflection; |
|
22
|
|
|
|
|
23
|
127 |
|
public function __construct(ClassReflectionInterface $classReflection, IReflection $originalReflection) |
|
24
|
|
|
{ |
|
25
|
127 |
|
$this->classReflection = $classReflection; |
|
26
|
127 |
|
$this->originalReflection = $originalReflection; |
|
27
|
127 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return ClassReflectionInterface[] |
|
31
|
|
|
*/ |
|
32
|
1 |
View Code Duplication |
public function getDirectUsers(): array |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
1 |
|
$users = []; |
|
35
|
1 |
|
$name = $this->classReflection->getName(); |
|
36
|
1 |
|
foreach ($this->classReflection->getParsedClasses() as $class) { |
|
37
|
1 |
|
if (! $class->isDocumented()) { |
|
38
|
|
|
continue; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
if (in_array($name, $class->getOwnTraitNames())) { |
|
42
|
1 |
|
$users[] = $class; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
uksort($users, 'strcasecmp'); |
|
47
|
1 |
|
return $users; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return ClassReflectionInterface[] |
|
52
|
|
|
*/ |
|
53
|
1 |
View Code Duplication |
public function getIndirectUsers(): array |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
1 |
|
$users = []; |
|
56
|
1 |
|
$name = $this->classReflection->getName(); |
|
57
|
1 |
|
foreach ($this->classReflection->getParsedClasses() as $class) { |
|
58
|
1 |
|
if (! $class->isDocumented()) { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
if ($class->usesTrait($name) && ! in_array($name, $class->getOwnTraitNames())) { |
|
63
|
|
|
$users[] = $class; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
uksort($users, 'strcasecmp'); |
|
68
|
1 |
|
return $users; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return PropertyReflectionInterface[] |
|
73
|
|
|
*/ |
|
74
|
1 |
View Code Duplication |
public function getTraitProperties(): array |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
1 |
|
$properties = []; |
|
77
|
1 |
|
$traitProperties = $this->originalReflection->getTraitProperties($this->classReflection->getVisibilityLevel()); |
|
|
|
|
|
|
78
|
1 |
|
foreach ($traitProperties as $property) { |
|
79
|
1 |
|
$apiProperty = $this->classReflection->getReflectionFactory()->createFromReflection($property); |
|
80
|
1 |
|
if (! $this->classReflection->isDocumented() || $apiProperty->isDocumented()) { |
|
81
|
1 |
|
$properties[$property->getName()] = $apiProperty; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return $properties; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return MethodReflectionInterface[] |
|
90
|
|
|
*/ |
|
91
|
1 |
View Code Duplication |
public function getTraitMethods(): array |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
1 |
|
$methods = []; |
|
94
|
1 |
|
foreach ($this->originalReflection->getTraitMethods($this->classReflection->getVisibilityLevel()) as $method) { |
|
|
|
|
|
|
95
|
1 |
|
$apiMethod = $this->classReflection->getReflectionFactory()->createFromReflection($method); |
|
96
|
1 |
|
if (! $this->classReflection->isDocumented() || $apiMethod->isDocumented()) { |
|
97
|
1 |
|
$methods[$method->getName()] = $apiMethod; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $methods; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return PropertyReflectionInterface[] |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getUsedProperties(): array |
|
108
|
|
|
{ |
|
109
|
2 |
|
$allProperties = array_flip(array_map(function (PropertyReflectionInterface $property) { |
|
110
|
1 |
|
return $property->getName(); |
|
111
|
2 |
|
}, $this->classReflection->getOwnProperties())); |
|
112
|
|
|
|
|
113
|
2 |
|
$properties = []; |
|
114
|
2 |
|
foreach ($this->classReflection->getTraits() as $trait) { |
|
115
|
1 |
|
if (! $trait instanceof ClassReflectionInterface) { |
|
116
|
1 |
|
continue; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
$usedProperties = []; |
|
120
|
1 |
|
foreach ($trait->getOwnProperties() as $property) { |
|
121
|
1 |
|
if (! array_key_exists($property->getName(), $allProperties)) { |
|
122
|
1 |
|
$usedProperties[$property->getName()] = $property; |
|
123
|
1 |
|
$allProperties[$property->getName()] = null; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
if (! empty($usedProperties)) { |
|
128
|
1 |
|
ksort($usedProperties); |
|
129
|
1 |
|
$properties[$trait->getName()] = array_values($usedProperties); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
return $properties; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return MethodReflectionInterface[] |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function getUsedMethods(): array |
|
140
|
|
|
{ |
|
141
|
2 |
|
$usedMethods = []; |
|
142
|
2 |
|
foreach ($this->classReflection->getMethods() as $methodReflection) { |
|
143
|
1 |
|
if ($methodReflection->getDeclaringTraitName() === '' |
|
144
|
1 |
|
|| $methodReflection->getDeclaringTraitName() === $this->classReflection->getName() |
|
145
|
|
|
) { |
|
146
|
1 |
|
continue; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
$traitName = $methodReflection->getDeclaringTraitName(); |
|
150
|
1 |
|
$methodName = $methodReflection->getName(); |
|
151
|
|
|
|
|
152
|
1 |
|
$usedMethods[$traitName][$methodName]['method'] = $methodReflection; |
|
153
|
1 |
|
if ($this->wasMethodNameAliased($methodReflection)) { |
|
|
|
|
|
|
154
|
1 |
|
$usedMethods[$traitName][$methodName]['aliases'][$methodReflection->getName()] = $methodReflection; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
return $usedMethods; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
private function wasMethodNameAliased(MethodReflectionInterface $methodReflection): bool |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $methodReflection->getOriginalName() !== null |
|
164
|
1 |
|
&& $methodReflection->getOriginalName() !== $methodReflection->getName(); |
|
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.