forked from PHPantom-dev/phpantom_lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_psalm_tests.php
More file actions
582 lines (486 loc) · 16.9 KB
/
Copy pathextract_psalm_tests.php
File metadata and controls
582 lines (486 loc) · 16.9 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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
/**
* Psalm Test Extractor for PHPantom
*
* Parses Psalm's PHP test classes and extracts test cases with type assertions
* into standalone .php files compatible with PHPantom's assert_type_runner.rs.
*
* Psalm format:
* 'testName' => [
* 'code' => '<?php ... $var = expr; ',
* 'assertions' => ['$var' => 'ExpectedType'],
* ],
*
* Output format (matching PHPStan assertType):
* <?php
* // Test: testName
* // Source: Psalm/Tests/TypeReconciliation/ConditionalTest.php
* ... code ...
* assertType('ExpectedType', $var);
*
* Usage:
* php scripts/extract_psalm_tests.php [psalm_test_file.php ...] [--output-dir DIR]
* php scripts/extract_psalm_tests.php --all [--output-dir DIR]
*
* With --all, processes all 3.5A priority files from the test-porting plan.
*/
declare(strict_types=1);
$outputDir = null;
$files = [];
$processAll = false;
// Parse arguments
$args = array_slice($argv, 1);
for ($i = 0; $i < count($args); $i++) {
if ($args[$i] === '--output-dir' && isset($args[$i + 1])) {
$outputDir = $args[++$i];
} elseif ($args[$i] === '--all') {
$processAll = true;
} else {
$files[] = $args[$i];
}
}
$outputDir = $outputDir ?? __DIR__ . '/../tests/psalm_assertions';
$psalmTestBase = realpath(__DIR__ . '/../references/psalm/tests');
// 3.5A priority files (clearly LSP-relevant)
$allFiles = [
'TypeReconciliation/ConditionalTest.php',
'TypeReconciliation/IssetTest.php',
'TypeReconciliation/ArrayKeyExistsTest.php',
'TypeReconciliation/TypeTest.php',
'TypeReconciliation/InArrayTest.php',
'TypeReconciliation/ScopeTest.php',
'ArrayAssignmentTest.php',
'ArrayAccessTest.php',
'ClosureTest.php',
'EnumTest.php',
'GeneratorTest.php',
'MixinAnnotationTest.php',
'MagicMethodAnnotationTest.php',
'MagicPropertyTest.php',
'MethodCallTest.php',
'PropertyTypeTest.php',
'ReturnTypeTest.php',
'Loop/ForeachTest.php',
'Loop/DoTest.php',
'Loop/WhileTest.php',
'Loop/ForTest.php',
'AnnotationTest.php',
'DocblockInheritanceTest.php',
'MatchTest.php',
'SwitchTypeTest.php',
'IntersectionTypeTest.php',
'NativeIntersectionsTest.php',
'ClassLikeStringTest.php',
'TraitTest.php',
'AssertAnnotationTest.php',
'TypeAnnotationTest.php',
'TryCatchTest.php',
'CastTest.php',
'Template/ClassTemplateTest.php',
'Template/ClassTemplateExtendsTest.php',
'Template/FunctionTemplateTest.php',
'Template/ConditionalReturnTypeTest.php',
'Template/FunctionClassStringTemplateTest.php',
'Template/FunctionTemplateAssertTest.php',
'IfThisIsTest.php',
'ThisOutTest.php',
// 3.5B: Partially relevant files
'ArrayFunctionCallTest.php',
'FunctionCallTest.php',
'BinaryOperationTest.php',
'ConstantTest.php',
'CallableTest.php',
'TypeReconciliation/EmptyTest.php',
'TypeReconciliation/RedundantConditionTest.php',
'TypeReconciliation/TypeAlgebraTest.php',
];
if ($processAll) {
foreach ($allFiles as $rel) {
$full = $psalmTestBase . '/' . $rel;
if (file_exists($full)) {
$files[] = $full;
} else {
fprintf(STDERR, "WARNING: File not found: %s\n", $full);
}
}
}
if (empty($files)) {
fprintf(STDERR, "Usage: php %s [--all | file1.php file2.php ...] [--output-dir DIR]\n", $argv[0]);
exit(1);
}
if (!is_dir($outputDir)) {
mkdir($outputDir, 0755, true);
}
$totalExtracted = 0;
$totalSkipped = 0;
$totalFiles = 0;
foreach ($files as $file) {
if (!file_exists($file)) {
fprintf(STDERR, "ERROR: File not found: %s\n", $file);
continue;
}
$source = file_get_contents($file);
$relativePath = str_replace($psalmTestBase . '/', '', realpath($file));
$testCases = extractTestCases($source);
if (empty($testCases)) {
fprintf(STDERR, " No test cases with assertions found in %s\n", $relativePath);
continue;
}
// Generate output filename from the Psalm test path
// e.g. TypeReconciliation/ConditionalTest.php -> type_reconciliation_conditional.php
$outBasename = pathToOutputName($relativePath);
$extracted = 0;
$skipped = 0;
$outputParts = [];
foreach ($testCases as $testName => $testCase) {
$code = $testCase['code'];
$assertions = $testCase['assertions'];
$phpVersion = $testCase['php_version'] ?? null;
if (empty($assertions)) {
continue;
}
// Filter out assertions with Psalm-specific types we don't support
$filteredAssertions = filterAssertions($assertions);
if (empty($filteredAssertions)) {
$skipped++;
continue;
}
// Normalize the code: strip leading indentation from Psalm's heredoc style
$code = normalizeCode($code);
// Build assertType calls
$assertCalls = [];
foreach ($filteredAssertions as $var => $type) {
$type = normalizeType($type);
$assertCalls[] = sprintf("assertType('%s', %s);", addcslashes($type, "'\\"), $var);
}
$outputParts[] = [
'name' => $testName,
'code' => $code,
'asserts' => $assertCalls,
'php_version' => $phpVersion,
];
$extracted++;
}
if (empty($outputParts)) {
fprintf(STDERR, " No usable assertions in %s (skipped %d)\n", $relativePath, $skipped);
continue;
}
// Write one file per Psalm test class, with all test cases concatenated.
// Each test case is wrapped in a namespace to avoid name collisions.
$output = "<?php\n";
$output .= sprintf("// Source: Psalm %s\n", $relativePath);
$output .= "// Auto-extracted by scripts/extract_psalm_tests.php\n";
$output .= "// Do not edit manually — re-run the extraction script instead.\n\n";
$caseIndex = 0;
foreach ($outputParts as $part) {
$caseIndex++;
$namespaceName = sprintf("PsalmTest_%s_%d", preg_replace('/[^a-zA-Z0-9]/', '_', $outBasename), $caseIndex);
$output .= sprintf("// Test: %s\n", $part['name']);
if ($part['php_version']) {
$output .= sprintf("// Requires PHP %s\n", $part['php_version']);
}
$code = $part['code'];
// If the code starts with <?php, strip it since we already have one
$code = preg_replace('/^<\?php\s*/', '', $code);
// Wrap in namespace to isolate each test case
$output .= sprintf("namespace %s {\n", $namespaceName);
// Indent the code
$codeLines = explode("\n", rtrim($code));
foreach ($codeLines as $line) {
if (trim($line) === '') {
$output .= "\n";
} else {
$output .= " " . $line . "\n";
}
}
// Add assertType calls
$output .= "\n";
foreach ($part['asserts'] as $assert) {
$output .= " " . $assert . "\n";
}
$output .= "}\n\n";
}
$outPath = $outputDir . '/' . $outBasename . '.php';
file_put_contents($outPath, $output);
$totalExtracted += $extracted;
$totalSkipped += $skipped;
$totalFiles++;
fprintf(STDERR, " %s: %d test cases extracted, %d skipped -> %s\n",
$relativePath, $extracted, $skipped, basename($outPath));
}
fprintf(STDERR, "\nTotal: %d files processed, %d test cases extracted, %d skipped\n",
$totalFiles, $totalExtracted, $totalSkipped);
fprintf(STDERR, "Output directory: %s\n", realpath($outputDir) ?: $outputDir);
// ─── Extraction functions ───────────────────────────────────────────────────
/**
* Extract test cases from a Psalm test PHP file.
*
* Looks for providerValidCodeParse() method which returns an array of test
* cases. Each test case has 'code', optional 'assertions', optional
* 'php_version', etc.
*
* @return array<string, array{code: string, assertions: array<string, string>, php_version: ?string}>
*/
function extractTestCases(string $source): array
{
$cases = [];
// We parse this with a state machine rather than eval() for safety.
// Strategy: find each test case block by matching the pattern:
// 'testName' => [
// 'code' => '...',
// 'assertions' => ['$var' => 'Type', ...],
// ],
// Find all test case blocks using a regex-based approach.
// First, locate the providerValidCodeParse method.
$validStart = strpos($source, 'providerValidCodeParse');
if ($validStart === false) {
return $cases;
}
// Work from providerValidCodeParse to the end of its return array.
$workingSource = substr($source, $validStart);
// Find test case names: lines like "'testCaseName' => ["
preg_match_all("/^\\s*'([a-zA-Z0-9_]+)'\\s*=>\\s*\\[/m", $workingSource, $nameMatches, PREG_OFFSET_CAPTURE);
for ($i = 0; $i < count($nameMatches[0]); $i++) {
$testName = $nameMatches[1][$i][0];
$blockStart = $nameMatches[0][$i][1];
// Find the end of this test case block by matching brackets
$blockEnd = findMatchingBracket($workingSource, $blockStart);
if ($blockEnd === false) {
continue;
}
$block = substr($workingSource, $blockStart, $blockEnd - $blockStart + 1);
// Extract 'code' value
$code = extractStringValue($block, 'code');
if ($code === null) {
continue;
}
// Extract 'assertions' array
$assertions = extractAssertions($block);
// Extract 'php_version' if present
$phpVersion = extractStringValue($block, 'php_version');
$cases[$testName] = [
'code' => $code,
'assertions' => $assertions,
'php_version' => $phpVersion,
];
}
return $cases;
}
/**
* Find the position of the closing bracket that matches the first '[' found
* at or after $startPos in $source.
*/
function findMatchingBracket(string $source, int $startPos): int|false
{
$pos = strpos($source, '[', $startPos);
if ($pos === false) {
return false;
}
$depth = 0;
$inSingleQuote = false;
$inDoubleQuote = false;
$len = strlen($source);
for ($i = $pos; $i < $len; $i++) {
$ch = $source[$i];
$prev = $i > 0 ? $source[$i - 1] : '';
if ($inSingleQuote) {
if ($ch === "'" && $prev !== '\\') {
$inSingleQuote = false;
}
continue;
}
if ($inDoubleQuote) {
if ($ch === '"' && $prev !== '\\') {
$inDoubleQuote = false;
}
continue;
}
if ($ch === "'") {
$inSingleQuote = true;
} elseif ($ch === '"') {
$inDoubleQuote = true;
} elseif ($ch === '[') {
$depth++;
} elseif ($ch === ']') {
$depth--;
if ($depth === 0) {
return $i;
}
}
}
return false;
}
/**
* Extract a string value for a given key from a test case block.
* Handles single-quoted strings with concatenation across lines.
*/
function extractStringValue(string $block, string $key): ?string
{
// Match: 'key' => '...'
$pattern = "/'" . preg_quote($key, '/') . "'\\s*=>\\s*'/";
if (!preg_match($pattern, $block, $m, PREG_OFFSET_CAPTURE)) {
return null;
}
$valueStart = $m[0][1] + strlen($m[0][0]) - 1; // position of opening quote
// Collect the full string value, handling multi-line strings.
// Psalm uses single-quoted strings. We need to handle escaped single quotes.
$result = '';
$i = $valueStart + 1; // skip opening quote
$len = strlen($block);
while ($i < $len) {
$ch = $block[$i];
if ($ch === '\\' && $i + 1 < $len) {
$next = $block[$i + 1];
if ($next === "'") {
$result .= "'";
$i += 2;
continue;
} elseif ($next === '\\') {
$result .= '\\';
$i += 2;
continue;
}
}
if ($ch === "'") {
// End of this string segment. Check for concatenation.
$rest = ltrim(substr($block, $i + 1));
if (str_starts_with($rest, ". '") || str_starts_with($rest, ".'")) {
// String concatenation — find the next quote and continue
$nextQuote = strpos($block, "'", $i + 1);
if ($nextQuote !== false) {
$nextQuote = strpos($block, "'", $nextQuote + 1);
}
// Actually, just skip to next single quote after the dot
$dotPos = strpos($block, '.', $i + 1);
if ($dotPos !== false) {
$nextQuoteStart = strpos($block, "'", $dotPos + 1);
if ($nextQuoteStart !== false) {
$i = $nextQuoteStart + 1;
continue;
}
}
}
break;
}
$result .= $ch;
$i++;
}
return $result;
}
/**
* Extract assertions array from a test case block.
* Format: 'assertions' => ['$var' => 'Type', '$var2' => 'Type2'],
*
* @return array<string, string>
*/
function extractAssertions(string $block): array
{
$assertions = [];
// Find 'assertions' => [
$pos = strpos($block, "'assertions'");
if ($pos === false) {
return $assertions;
}
$bracketStart = strpos($block, '[', $pos);
if ($bracketStart === false) {
return $assertions;
}
$bracketEnd = findMatchingBracket($block, $pos);
if ($bracketEnd === false) {
return $assertions;
}
$assertBlock = substr($block, $bracketStart, $bracketEnd - $bracketStart + 1);
// Match each '$var' => 'Type' pair
preg_match_all("/'(\\$[a-zA-Z_][a-zA-Z0-9_]*)'\\s*=>\\s*'([^']*)'/", $assertBlock, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$assertions[$matches[1][$i]] = $matches[2][$i];
}
return $assertions;
}
/**
* Filter out assertions that use Psalm-specific types PHPantom doesn't support.
*
* @param array<string, string> $assertions
* @return array<string, string>
*/
function filterAssertions(array $assertions): array
{
$filtered = [];
foreach ($assertions as $var => $type) {
// Skip Psalm-specific types
if (preg_match('/\b(non-empty-|non-falsy-|lowercase-|uppercase-|numeric-string|positive-int|negative-int|int<|literal-|class-string-map|closed-resource|pure-|no-return)/', $type)) {
continue;
}
// Skip literal int/string values as types (e.g. "'hello'", "0", "1")
if (preg_match("/^'[^']*'$/", $type) || preg_match('/^-?\d+$/', $type)) {
continue;
}
// Skip literal bool values
if ($type === 'true' || $type === 'false') {
continue;
}
$filtered[$var] = $type;
}
return $filtered;
}
/**
* Normalize Psalm type strings to PHPantom equivalents.
*/
function normalizeType(string $type): string
{
// Psalm uses 'list<T>' which PHPantom treats as 'array<int, T>'
// Keep it as-is for now; the runner's normalize function handles this.
// Psalm uses 'array-key' — keep as-is, runner normalizes.
// Remove 'Psalm\Tests\...' namespace prefixes if any leaked in
$type = preg_replace('/Psalm\\\\Tests\\\\[A-Za-z\\\\]*\\\\/', '', $type);
return $type;
}
/**
* Normalize code: strip common leading indentation from Psalm's indented heredoc style.
*/
function normalizeCode(string $code): string
{
$lines = explode("\n", $code);
// Find minimum indentation (ignoring empty lines and <?php line)
$minIndent = PHP_INT_MAX;
foreach ($lines as $line) {
if (trim($line) === '' || trim($line) === '<?php') {
continue;
}
$stripped = ltrim($line);
if ($stripped === '') {
continue;
}
$indent = strlen($line) - strlen($stripped);
$minIndent = min($minIndent, $indent);
}
if ($minIndent === PHP_INT_MAX || $minIndent === 0) {
return $code;
}
// Strip common indentation
$result = [];
foreach ($lines as $line) {
if (trim($line) === '') {
$result[] = '';
} else {
$result[] = substr($line, min($minIndent, strlen($line) - strlen(ltrim($line))));
}
}
return implode("\n", $result);
}
/**
* Convert a Psalm test path to an output filename.
* e.g. "TypeReconciliation/ConditionalTest.php" -> "type_reconciliation_conditional"
*/
function pathToOutputName(string $path): string
{
// Remove .php extension
$name = preg_replace('/\.php$/', '', $path);
// Remove "Test" suffix
$name = preg_replace('/Test$/', '', $name);
// Convert PascalCase to snake_case
$name = preg_replace('/([a-z])([A-Z])/', '$1_$2', $name);
// Convert path separators to underscores
$name = str_replace('/', '_', $name);
return strtolower($name);
}