This repository was archived by the owner on Feb 17, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceProxier.php
More file actions
264 lines (223 loc) · 7.81 KB
/
InterfaceProxier.php
File metadata and controls
264 lines (223 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
declare(strict_types=1);
namespace ReactParallel\ObjectProxy\Composer;
use Doctrine\Common\Annotations\AnnotationReader;
use PhpParser\Builder\Method;
use PhpParser\Comment;
use PhpParser\Node;
use ReactParallel\ObjectProxy\AbstractGeneratedProxy;
use ReactParallel\ObjectProxy\Attribute\Defer;
use ReflectionMethod;
use Traversable;
use function array_key_exists;
use function count;
use function implode;
use function is_array;
use function iterator_to_array;
use function property_exists;
use function str_replace;
use function strpos;
use const WyriHaximus\Constants\Boolean\FALSE_;
final class InterfaceProxier
{
public const GENERATED_NAMESPACE = [
'ReactParallel',
'ObjectProxy',
'Generated',
];
private const NAMESPACE_GLUE = '\\';
/** @var array<Node\Stmt> */
private array $stmts;
private string $namespace = '';
private string $className = '';
private string $interfaceName = '';
/** @var array<string, string> */
private array $uses = [];
/**
* @param Node\Stmt[] $stmts
*/
public function __construct(array $stmts)
{
$this->stmts = $this->iterateStmts($stmts);
}
/**
* @return array<Node\Stmt>
*/
public function stmts(): array
{
return $this->stmts;
}
public function namespace(): string
{
return $this->namespace;
}
public function className(): string
{
return $this->className;
}
public function interfaceName(): string
{
return $this->interfaceName;
}
/**
* @param array<Node\Stmt> $stmts
*
* @return array<Node\Stmt>
*/
private function iterateStmts(array $stmts): array
{
$nodes = [];
foreach ($stmts as $index => $node) {
$nodes[$index] = $this->inspectNode($node);
}
return $nodes;
}
private function inspectNode(Node\Stmt $node): Node\Stmt
{
if ($node instanceof Node\Stmt\Interface_) {
$this->className = str_replace(self::NAMESPACE_GLUE, '__', $this->namespace) . '_' . $node->name . 'Proxy';
$this->interfaceName = $this->namespace . self::NAMESPACE_GLUE . $node->name;
}
if ($node instanceof Node\Stmt\Namespace_) {
$node = $this->replaceNamespace($node);
}
if ($node instanceof Node\Stmt\Use_) {
$this->gatherUses($node);
}
/**
* @psalm-suppress RedundantConditiondkp
* @psalm-suppress UndefinedPropertyFetch
*/
if (property_exists($node, 'stmts') && is_array($node->stmts)) {
/** @psalm-suppress UndefinedPropertyAssignment */
$node->stmts = $this->iterateStmts($node->stmts);
}
if ($node instanceof Node\Stmt\Interface_) {
return $this->transformInterfaceIntoClass($node);
}
if ($node instanceof Node\Stmt\ClassMethod) {
return $this->populateMethod($node);
}
return $node;
}
private function replaceNamespace(Node\Stmt\Namespace_ $namespace): Node\Stmt\Namespace_
{
/**
* @psalm-suppress PossiblyNullArgument
* @psalm-suppress PossiblyNullPropertyFetch
* @phpstan-ignore-next-line
*/
$this->namespace = implode(self::NAMESPACE_GLUE, $namespace->name->parts);
/**
* @psalm-suppress PossiblyNullPropertyAssignment
* @phpstan-ignore-next-line
*/
$namespace->name->parts = self::GENERATED_NAMESPACE;
$namespace->stmts = $this->iterateStmts($namespace->stmts);
return $namespace;
}
private function gatherUses(Node\Stmt\Use_ $use): void
{
foreach ($use->uses as $singleUse) {
/** @phpstan-ignore-next-line */
$this->uses[$singleUse->alias ?? $singleUse->name->parts[count($singleUse->name->parts) - 1]] = $singleUse->name->toString();
}
}
private function transformInterfaceIntoClass(Node\Stmt\Interface_ $interface): Node\Stmt\Class_
{
$stmts = $this->iterateStmts($interface->stmts);
$stmts[] = (new Method('__destruct'))->addStmt(
new Node\Stmt\Expression(
new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
'notifyMainThreadAboutDestruction',
)
),
)->makePublic()->makeFinal()->getNode();
return new Node\Stmt\Class_(
$this->className,
[
'extends' => new Node\Name(self::NAMESPACE_GLUE . AbstractGeneratedProxy::class),
'flags' => Node\Stmt\Class_::MODIFIER_FINAL,
'implements' => [
new Node\Name(self::NAMESPACE_GLUE . $this->interfaceName),
],
'stmts' => $stmts,
],
);
}
private function populateMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassMethod
{
if ((string) $method->name === '__destruct') {
return $method;
}
foreach ($method->getParams() as $param) {
if (! ($param->type instanceof Node\Name) || array_key_exists((string) $param->type, $this->uses)) {
continue;
}
$namespacedClassType = self::NAMESPACE_GLUE . $this->namespace . self::NAMESPACE_GLUE . (string) $param->type;
$param->type = new Node\Name($namespacedClassType);
$this->uses[(string) $param->type] = $namespacedClassType;
}
$methodBody = new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
$this->isMethodVoid($method) ? ($this->isDeferrable($method) ? 'deferNotifyMainThread' : 'proxyNotifyMainThread') : ($this->isDeferrable($method) ? 'deferCallToMainThread' : 'proxyCallToMainThread'),
iterator_to_array($this->methodCallArguments($method))
);
$method->stmts = [
$this->wrapMethodBody($method, $methodBody),
];
return $method;
}
/**
* @return Traversable<Node\Arg>
*/
private function methodCallArguments(Node\Stmt\ClassMethod $method): iterable
{
yield new Node\Arg(
new Node\Expr\ConstFetch(
new Node\Name('__FUNCTION__'),
),
);
yield new Node\Arg(
new Node\Expr\FuncCall(
new Node\Name('\func_get_args'),
),
);
if (! $this->isDeferrable($method)) {
return;
}
yield new Node\Arg(
new Node\Scalar\String_($this->interfaceName),
);
}
private function wrapMethodBody(Node\Stmt\ClassMethod $method, Node\Expr\MethodCall $methodBody): Node\Stmt
{
if ($this->isMethodVoid($method)) {
return new Node\Stmt\Expression($methodBody);
}
return new Node\Stmt\Return_($methodBody);
}
private function isMethodVoid(Node\Stmt\ClassMethod $method): bool
{
/**
* @psalm-suppress PossiblyInvalidCast
* @phpstan-ignore-next-line
*/
return (string) $method->getReturnType() === 'void' ? true : $this->isMethodVoidFromDocBlock($method);
}
private function isMethodVoidFromDocBlock(Node\Stmt\ClassMethod $method): bool
{
/**
* @psalm-suppress PossiblyNullReference
*/
return $method->getDocComment() instanceof Comment && strpos($method->getDocComment()->getText(), '@return void') !== FALSE_;
}
private function isDeferrable(Node\Stmt\ClassMethod $method): bool
{
if (! ($method->getDocComment() instanceof Comment)) {
return false;
}
return (new AnnotationReader())->getMethodAnnotation(new ReflectionMethod($this->interfaceName . '::' . $method->name), Defer::class) instanceof Defer;
}
}