Skip to content

Commit 4557edd

Browse files
authored
Fix test lint-comment (phpstan#4986)
1 parent 91b0c06 commit 4557edd

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

.claude/skills/regression-test/SKILL.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ Use when the bug is about **wrong inferred type, missing type narrowing, or inco
4949
Example (`tests/PHPStan/Analyser/nsrt/bug-12875.php`):
5050

5151
```php
52-
<?php declare(strict_types = 1); // lint >= 8.0
52+
<?php // lint >= 8.0
53+
54+
declare(strict_types = 1);
5355

5456
namespace Bug12875;
5557

.github/workflows/tests-levels-matrix.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
<?php declare(strict_types = 1);
22

3-
shell_exec('php vendor/bin/phpunit --group levels --list-tests-xml test-list.xml');
3+
exec('php vendor/bin/phpunit --group levels --list-tests-xml test-list.xml', $output, $return);
4+
if ($return !== 0) {
5+
throw new RuntimeException(implode("\n", $output));
6+
}
47

8+
libxml_use_internal_errors(true);
59
$simpleXml = simplexml_load_file('test-list.xml');
610
if ($simpleXml === false) {
7-
throw new RuntimeException('Error loading test-list.xml');
11+
$errors = [];
12+
foreach (libxml_get_errors() as $error) {
13+
$errors[] = $error->message;
14+
}
15+
16+
throw new RuntimeException('Error loading test-list.xml: ' . implode(', ', $errors));
817
}
918

1019
$testFilters = [];

src/Testing/TypeInferenceTestCase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Testing;
44

5+
use LogicException;
56
use PhpParser\Node;
67
use PhpParser\Node\Expr\StaticCall;
78
use PhpParser\Node\Name;
@@ -44,6 +45,7 @@
4445
use function is_string;
4546
use function preg_match;
4647
use function sprintf;
48+
use function str_contains;
4749
use function str_starts_with;
4850
use function stripos;
4951
use function strtolower;
@@ -432,8 +434,12 @@ public static function findTestDataFilesFromDirectory(string $directory): array
432434
$files = [];
433435
foreach ($finder->files()->name('*.php')->in($directory) as $fileInfo) {
434436
$path = $fileInfo->getPathname();
435-
if (self::isFileLintSkipped($path)) {
436-
continue;
437+
try {
438+
if (self::isFileLintSkipped($path)) {
439+
continue;
440+
}
441+
} catch (LogicException $e) {
442+
self::fail($e->getMessage());
437443
}
438444
$files[] = $path;
439445
}
@@ -467,6 +473,8 @@ private static function isFileLintSkipped(string $file): bool
467473

468474
if (preg_match('~<?php\\s*\\/\\/\s*lint\s*([^\d\s]+)\s*([^\s]+)\s*~i', $firstLine, $m) === 1) {
469475
return version_compare(PHP_VERSION, $m[2], $m[1]) === false;
476+
} elseif (str_contains($firstLine, 'lint')) {
477+
throw new LogicException(sprintf("'// lint' comment must immediately follow the php starting tag in %s on line 1", $file));
470478
}
471479
}
472480

tests/PHPStan/Analyser/nsrt/bug-11472.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types = 1); // lint >= 8.1
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 1);
24

35
namespace Bug11472;
46

tests/PHPStan/Analyser/nsrt/bug-12866.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types = 1); // lint >= 8.0
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
24

35
namespace Bug12866;
46

tests/PHPStan/Analyser/nsrt/bug-12875.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types = 1); // lint >= 8.0
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
24

35
namespace Bug12875a;
46

0 commit comments

Comments
 (0)