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