1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration; |
6
|
|
|
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator; |
8
|
|
|
use ReflectionException; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use ts\Reflection\ReflectionClass; |
11
|
|
|
|
12
|
|
|
use function array_slice; |
13
|
|
|
use function str_replace; |
14
|
|
|
|
15
|
|
|
class ReflectionHelper |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var NamespaceHelper |
20
|
|
|
*/ |
21
|
|
|
protected $namespaceHelper; |
22
|
|
|
|
23
|
5 |
|
public function __construct(NamespaceHelper $namespaceHelper) |
24
|
|
|
{ |
25
|
5 |
|
$this->namespaceHelper = $namespaceHelper; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ReflectionClass $fieldTraitReflection |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
1 |
|
public function getFakerProviderFqnFromFieldTraitReflection(ReflectionClass $fieldTraitReflection): string |
34
|
|
|
{ |
35
|
1 |
|
return str_replace( |
36
|
|
|
[ |
37
|
1 |
|
'\\Traits\\', |
38
|
|
|
'FieldTrait', |
39
|
|
|
], |
40
|
|
|
[ |
41
|
1 |
|
'\\FakerData\\', |
42
|
|
|
'FakerData', |
43
|
|
|
], |
44
|
1 |
|
$fieldTraitReflection->getName() |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Work out the entity namespace root from a single entity reflection object. |
50
|
|
|
* |
51
|
|
|
* @param ReflectionClass $entityReflection |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
1 |
|
public function getEntityNamespaceRootFromEntityReflection( |
56
|
|
|
ReflectionClass $entityReflection |
57
|
|
|
): string { |
58
|
1 |
|
return $this->namespaceHelper->tidy( |
59
|
1 |
|
$this->namespaceHelper->getNamespaceRootToDirectoryFromFqn( |
|
|
|
|
60
|
1 |
|
$entityReflection->getName(), |
61
|
1 |
|
AbstractGenerator::ENTITIES_FOLDER_NAME |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Find which trait is implementing a method in a class |
68
|
|
|
* |
69
|
|
|
* @param ReflectionClass $class |
70
|
|
|
* @param string $methodName |
71
|
|
|
* |
72
|
|
|
* @return ReflectionClass |
73
|
|
|
* @throws ReflectionException |
74
|
|
|
*/ |
75
|
|
|
public function getTraitImplementingMethod(ReflectionClass $class, string $methodName): ReflectionClass |
76
|
|
|
{ |
77
|
|
|
$traitsWithMethod = []; |
78
|
|
|
foreach ($class->getTraits() as $trait) { |
79
|
|
|
try { |
80
|
|
|
$trait->getMethod($methodName); |
81
|
|
|
$traitsWithMethod[] = $trait; |
82
|
|
|
} catch (ReflectionException $e) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
if (count($traitsWithMethod) > 1) { |
87
|
|
|
throw new RuntimeException( |
88
|
|
|
'Found more than one trait implementing the method ' . $methodName . ' in ' . |
89
|
|
|
$class->getShortName() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
if ([] === $traitsWithMethod) { |
93
|
|
|
throw new RuntimeException( |
94
|
|
|
'Failed finding trait implementing the method ' . $methodName . ' in ' . |
95
|
|
|
$class->getShortName() |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return current($traitsWithMethod); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Find which trait is implementing a method in a class |
104
|
|
|
* |
105
|
|
|
* @param ReflectionClass $class |
106
|
|
|
* @param string $propertyName |
107
|
|
|
* |
108
|
|
|
* @return ReflectionClass |
109
|
|
|
* @throws ReflectionException |
110
|
|
|
*/ |
111
|
1 |
|
public function getTraitProvidingProperty(ReflectionClass $class, string $propertyName): ReflectionClass |
112
|
|
|
{ |
113
|
1 |
|
$traitsWithProperty = []; |
114
|
1 |
|
foreach ($class->getTraits() as $trait) { |
115
|
|
|
try { |
116
|
1 |
|
$trait->getProperty($propertyName); |
117
|
1 |
|
$traitsWithProperty[] = $trait; |
118
|
1 |
|
} catch (ReflectionException $e) { |
119
|
1 |
|
continue; |
120
|
|
|
} |
121
|
|
|
} |
122
|
1 |
|
if ([] === $traitsWithProperty) { |
123
|
|
|
if ($class->isTrait() && $class->hasProperty($propertyName)) { |
124
|
|
|
return $class; |
125
|
|
|
} |
126
|
|
|
throw new RuntimeException( |
127
|
|
|
'Failed finding trait providing the property ' . $propertyName . ' in ' . |
128
|
|
|
$class->getShortName() |
129
|
|
|
); |
130
|
|
|
} |
131
|
1 |
|
if (count($traitsWithProperty) > 1) { |
132
|
|
|
throw new RuntimeException( |
133
|
|
|
'Found more than one trait providing the property ' . $propertyName . ' in ' . |
134
|
|
|
$class->getShortName() |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return current($traitsWithProperty); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the full method body using reflection |
143
|
|
|
* |
144
|
|
|
* @param string $methodName |
145
|
|
|
* @param ReflectionClass $reflectionClass |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
public function getMethodBody(string $methodName, ReflectionClass $reflectionClass): string |
150
|
|
|
{ |
151
|
1 |
|
$method = $reflectionClass->getMethod($methodName); |
152
|
1 |
|
$startLine = $method->getStartLine() - 1; |
153
|
1 |
|
$length = $method->getEndLine() - $startLine; |
154
|
1 |
|
$lines = file($reflectionClass->getFileName()); |
155
|
1 |
|
$methodLines = array_slice($lines, $startLine, $length); |
|
|
|
|
156
|
|
|
|
157
|
1 |
|
return implode('', $methodLines); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param ReflectionClass $reflectionClass |
162
|
|
|
* |
163
|
|
|
* @return array|string[] |
164
|
|
|
*/ |
165
|
1 |
|
public function getUseStatements(ReflectionClass $reflectionClass): array |
166
|
|
|
{ |
167
|
1 |
|
$content = \ts\file_get_contents($reflectionClass->getFileName()); |
168
|
1 |
|
preg_match_all('%^use.+?;%m', $content, $matches); |
169
|
|
|
|
170
|
1 |
|
return $matches[0]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|