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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
<?php
declare(strict_types=1);
namespace Shirabe\PluginClassifier;
final class Classifier
{
public const CATEGORIES = [
'rust-proxy', 'rust-snapshot', 'contract', 'two-world', 'php-native', 'unsupported',
];
private const THROWABLE_ROOTS = ['Throwable', 'Exception', 'Error'];
public SourceParser $sources;
public ReachabilityClosure $closure;
public PurityAnalyzer $purity;
public NativeFixedPoint $native;
/** @var array<string, array<string, mixed>> FQCN => row */
public array $rows = [];
/** @var list<string> */
public array $violations = [];
/** @var array<string, bool> */
private array $throwableCache = [];
/** @var array<string, bool> */
private array $valueObjectCandidates = [];
/** @var array<string, bool> */
private array $statelessPureCandidates = [];
public function __construct(private readonly string $composerSrc, private readonly Lists $lists)
{
}
public function run(): void
{
$this->sources = new SourceParser();
$this->sources->parseTree($this->composerSrc);
$this->sources->finalize();
$this->closure = new ReachabilityClosure($this->sources, $this->lists);
$this->closure->run();
$this->purity = new PurityAnalyzer($this->sources);
$this->purity->run();
$this->computeLocalCandidates();
// The unreachable fixed point needs to know which reachable classes
// are proxied (constructing one demotes), and candidate demotion
// needs to know which unreachable classes ended up unsupported.
// Iterate the two until stable; candidate sets only shrink, so this
// terminates.
do {
$reachableCategories = [];
foreach ($this->closure->reachable as $fqcn => $bits) {
$info = $this->sources->classes[$fqcn] ?? null;
if ($info !== null) {
$reachableCategories[$fqcn] = $this->categoryForReachable($fqcn, $info);
}
}
$this->native = new NativeFixedPoint($this->sources, $this->closure, $this->lists, $reachableCategories);
$this->native->run();
} while ($this->demoteCandidates());
foreach ($this->sources->classes as $fqcn => $info) {
$this->rows[$fqcn] = $this->classify($fqcn, $info);
}
$this->applyOverrides();
$this->collectViolations();
ksort($this->rows);
}
/** @return array<string, mixed> */
private function classify(string $fqcn, ClassInfo $info): array
{
$reachableBits = $this->closure->reachable[$fqcn] ?? 0;
$row = [
'fqcn' => $fqcn,
'kind' => $info->kind,
'reachable' => $reachableBits !== 0,
'direction' => $this->directionLabel($reachableBits),
'category' => null,
'reasons' => [],
'attributes' => [],
];
if ($info->writesOwnStaticProps) {
$row['attributes']['mutable-static'] =
$this->lists->staticDispositions[$fqcn] ?? 'needs-review';
}
if ($this->isThrowable($fqcn)) {
$row['attributes']['throwable'] = true;
}
if ($this->lists->isTwoWorld($fqcn)) {
$row['category'] = 'two-world';
$row['reasons'][] = 'two-world.list';
return $row;
}
if ($reachableBits === 0) {
$row['category'] = $this->native->categories[$fqcn] ?? 'unsupported';
$row['reasons'] = array_merge(
$row['reasons'],
$this->native->reasons[$fqcn] ?? ['not plugin-reachable; every reference resolves to executable code in the child'],
);
return $row;
}
$category = $this->categoryForReachable($fqcn, $info);
$row['category'] = $category;
$row['reasons'][] = match ($category) {
'php-native' => $this->isThrowable($fqcn)
? 'exception class: real definition in the child process, crossed by value'
: (($this->statelessPureCandidates[$fqcn] ?? false)
? 'stateless pure class: no instance state, all methods pure, real code answers identically in both worlds'
: 'constants-only class: pure definitions, no state to share'),
'contract' => 'interface/abstract type: declaration stub, artifacts depend on direction',
'rust-snapshot' => 'immutable value object: no state referencing living services, all methods pure',
'rust-proxy' => 'reachable concrete class holding or reaching shared state',
};
if ($category === 'rust-proxy') {
$row['attributes']['plugin-constructible'] = $this->hasPublicConstructor($info);
}
if (in_array($category, ['rust-proxy', 'rust-snapshot'], true)) {
// Stub static properties cannot be intercepted in PHP (there is
// no __getStatic); public ones need an explicit decision.
$statics = [];
foreach ($info->properties as $prop) {
if ($prop->static && $prop->visibility === 'public') {
$statics[] = $prop->name;
}
}
if ($statics !== []) {
sort($statics);
$row['attributes']['public-static-properties'] = $statics;
}
}
if (in_array($category, ['rust-proxy', 'rust-snapshot', 'contract'], true)) {
$row['methods'] = $this->methodRows($fqcn, $info);
}
return $row;
}
private function categoryForReachable(string $fqcn, ClassInfo $info): string
{
if ($this->isThrowable($fqcn)) {
return 'php-native';
}
// Contract wins over constants-only: a marker interface such as
// Capability is still implemented by plugins and needs direction
// attributes, not just its constant-free declaration.
if ($info->isContractLike() || $info->kind === 'trait') {
return 'contract';
}
if ($this->isConstantsOnly($info)) {
return 'php-native';
}
if ($this->statelessPureCandidates[$fqcn] ?? false) {
return 'php-native';
}
if ($this->valueObjectCandidates[$fqcn] ?? false) {
return 'rust-snapshot';
}
return 'rust-proxy';
}
/** @return list<array<string, mixed>> */
private function methodRows(string $fqcn, ClassInfo $info): array
{
$rows = [];
foreach ($info->methods as $lname => $method) {
if ($method->visibility === 'private') {
continue;
}
$callableParams = [];
foreach ($method->params as $i => $param) {
if ($param->callable) {
$callableParams[] = $i;
}
}
$rows[] = [
'name' => $method->name,
'visibility' => $method->visibility,
'static' => $method->static,
'purity' => $this->purity->verdicts["$fqcn::$lname"] ?? 'n/a',
'byRefParams' => $method->byRefParamPositions(),
'callableParams' => $callableParams,
];
}
usort($rows, static fn (array $a, array $b) => strcmp($a['name'], $b['name']));
return $rows;
}
private function directionLabel(int $bits): ?string
{
return match ($bits) {
0 => null,
ReachabilityClosure::PROVIDED => 'provided',
ReachabilityClosure::CONSUMED => 'consumed',
default => 'both',
};
}
private function isThrowable(string $fqcn): bool
{
if (isset($this->throwableCache[$fqcn])) {
return $this->throwableCache[$fqcn];
}
// Pre-set to break inheritance cycles (malformed input).
$this->throwableCache[$fqcn] = false;
$info = $this->sources->classes[$fqcn] ?? null;
if ($info === null) {
$isGlobal = !str_contains($fqcn, '\\');
$result = $isGlobal && (
in_array($fqcn, self::THROWABLE_ROOTS, true)
|| str_ends_with($fqcn, 'Exception')
|| str_ends_with($fqcn, 'Error')
);
return $this->throwableCache[$fqcn] = $result;
}
foreach (array_merge($info->parent !== null ? [$info->parent] : [], $info->interfaces) as $ancestor) {
if ($this->isThrowable($ancestor)) {
return $this->throwableCache[$fqcn] = true;
}
}
return false;
}
private function hasPublicConstructor(ClassInfo $info): bool
{
if ($info->abstract) {
return false;
}
$current = $info;
while (true) {
$ctor = $current->methods['__construct'] ?? null;
if ($ctor !== null) {
return $ctor->visibility === 'public';
}
if ($current->parent === null || !isset($this->sources->classes[$current->parent])) {
// No declared constructor anywhere visible: implicit public.
return true;
}
$current = $this->sources->classes[$current->parent];
}
}
private function computeLocalCandidates(): void
{
// Grow-only fixed point over two candidate kinds. Both require
// every instance method in the hierarchy to be pure and the body
// references to be locally satisfiable; they differ on state:
//
// - value objects carry copyable state (>= 1 instance property)
// none of which references a living service -> rust-snapshot
// - stateless pure classes carry no instance state at all;
// a snapshot has nothing to copy, and the real code answers
// identically in both worlds -> php-native
do {
$changed = false;
foreach ($this->closure->reachable as $fqcn => $bits) {
if (($this->valueObjectCandidates[$fqcn] ?? false) || ($this->statelessPureCandidates[$fqcn] ?? false)) {
continue;
}
$info = $this->sources->classes[$fqcn] ?? null;
if ($info === null || $info->isContractLike() || $info->kind !== 'class') {
continue;
}
if ($this->lists->isTwoWorld($fqcn) || $this->isThrowable($fqcn) || $this->isConstantsOnly($info)) {
continue;
}
if (!$this->allInstanceMethodsPure($fqcn) || !$this->bodyRefsAreLocal($info)) {
continue;
}
if ($this->hasInstanceProperties($info)) {
if ($this->stateIsValueOnly($info)) {
$this->valueObjectCandidates[$fqcn] = true;
$changed = true;
}
} else {
$this->statelessPureCandidates[$fqcn] = true;
$changed = true;
}
}
} while ($changed);
}
private function hasInstanceProperties(ClassInfo $info): bool
{
foreach ($this->hierarchyOf($info) as $level) {
foreach ($level->properties as $prop) {
if (!$prop->static) {
return true;
}
}
}
return false;
}
/**
* Snapshot classes ship their real method bodies; stateless pure
* classes run as real code. Either way, what the bodies construct must
* be locally constructible: another candidate, an exception, real
* vendor/two-world/builtin code — not a proxied service.
*/
private function bodyRefsAreLocal(ClassInfo $info): bool
{
foreach (array_unique($info->newRefs) as $ref) {
if ($ref === $info->fqcn || $this->isCandidate($ref) || $this->isThrowable($ref)) {
continue;
}
if (isset($this->closure->reachable[$ref])) {
$target = $this->sources->classes[$ref] ?? null;
if ($target !== null && ($this->isConstantsOnly($target) || $this->lists->isTwoWorld($ref))) {
continue;
}
// Constructs what will be a proxied service.
return false;
}
// Unreachable, vendor, or builtin targets are checked again
// once the unreachable fixed point has run (demoteCandidates).
}
return true;
}
private function isCandidate(string $fqcn): bool
{
return ($this->valueObjectCandidates[$fqcn] ?? false) || ($this->statelessPureCandidates[$fqcn] ?? false);
}
/**
* Re-check candidates against the unreachable fixed point's outcome:
* a candidate whose bodies construct or statically reference an
* unsupported class cannot run locally after all. Returns true when
* anything was demoted (the caller then reruns the fixed point).
*/
private function demoteCandidates(): bool
{
$demoted = false;
do {
$changed = false;
foreach (array_merge(array_keys($this->valueObjectCandidates), array_keys($this->statelessPureCandidates)) as $fqcn) {
if (!$this->isCandidate($fqcn)) {
continue;
}
$info = $this->sources->classes[$fqcn];
if ($this->bodyRefsAreLocal($info) && !$this->refsUnsupported($info)) {
continue;
}
unset($this->valueObjectCandidates[$fqcn], $this->statelessPureCandidates[$fqcn]);
$changed = true;
$demoted = true;
}
} while ($changed);
return $demoted;
}
private function refsUnsupported(ClassInfo $info): bool
{
foreach (array_unique(array_merge($info->newRefs, $info->staticRefs)) as $ref) {
if (($this->native->categories[$ref] ?? null) === 'unsupported') {
return true;
}
}
return false;
}
private function stateIsValueOnly(ClassInfo $info): bool
{
$types = [];
// State includes inherited properties: a VcsDownloader subclass
// carries its parent's ProcessExecutor even with no own fields.
foreach ($this->hierarchyOf($info) as $level) {
foreach ($level->properties as $prop) {
$types = array_merge($types, $prop->classTypes);
if ($prop->expandable || $prop->classTypes === []) {
$types = array_merge($types, $prop->docblockTypes);
}
}
}
$ctor = $info->methods['__construct'] ?? null;
if ($ctor !== null) {
foreach ($ctor->params as $param) {
$types = array_merge($types, $param->classTypes);
if ($param->expandable || $param->classTypes === []) {
$types = array_merge($types, $param->docblockTypes);
}
}
}
foreach (array_unique($types) as $type) {
if ($type === $info->fqcn) {
continue;
}
if (!isset($this->closure->reachable[$type]) && !isset($this->sources->classes[$type])) {
// Vendor or builtin: value-safe only if php-native.
$vendor = VendorPackages::lookup($type);
if ($vendor !== null && $vendor['category'] !== 'php-native') {
return false;
}
continue;
}
if ($this->isThrowable($type)) {
continue;
}
if (!$this->isCandidate($type)) {
return false;
}
}
return true;
}
/** @return list<ClassInfo> the class and its parents visible in the sources */
private function hierarchyOf(ClassInfo $info): array
{
$levels = [];
$seen = [];
$current = $info;
while (true) {
if (isset($seen[$current->fqcn])) {
break;
}
$seen[$current->fqcn] = true;
$levels[] = $current;
if ($current->parent === null || !isset($this->sources->classes[$current->parent])) {
break;
}
$current = $this->sources->classes[$current->parent];
}
return $levels;
}
private function allInstanceMethodsPure(string $fqcn): bool
{
$info = $this->sources->classes[$fqcn];
foreach ($this->hierarchyOf($info) as $level) {
foreach ($level->methods as $lname => $method) {
if ($method->static || $method->visibility === 'private' || $lname === '__construct') {
continue;
}
$verdict = $this->purity->verdicts["{$level->fqcn}::$lname"] ?? 'mutator';
if ($verdict === 'mutator') {
return false;
}
}
}
return true;
}
private function isConstantsOnly(ClassInfo $info): bool
{
return $info->methods === [] && $info->properties === []
&& $info->parent === null && $info->interfaces === [];
}
private function applyOverrides(): void
{
foreach ($this->lists->overrides as $fqcn => $override) {
if (!isset($this->rows[$fqcn])) {
$this->violations[] = "overrides.list: unknown class $fqcn";
continue;
}
$this->rows[$fqcn]['computedCategory'] = $this->rows[$fqcn]['category'];
$this->rows[$fqcn]['category'] = $override['category'];
$this->rows[$fqcn]['reasons'][] = 'override: ' . $override['reason'];
}
}
private function collectViolations(): void
{
foreach ($this->rows as $fqcn => $row) {
if (($row['attributes']['mutable-static'] ?? null) === 'needs-review') {
$this->violations[] = "$fqcn writes static properties but has no disposition in static-state.list";
}
}
foreach ($this->closure->unknownReachable as $fqcn => $bits) {
$this->violations[] = "reachable type $fqcn is neither a composer class, a known vendor package, nor a PHP builtin";
}
}
}
|