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