Skip to content

Commit a6c4f96

Browse files
committed
Re-analyse files with inline-ignored errors and after deleting a file declaring symbols
When a symbol appears or disappears, the files with errors are analysed again. But an error ignored by a @PHPStan-Ignore comment did not count, so declaring the class it waited for left the comment unmatched and unreported until a cold run. Files with such errors count now, also when deciding whether a changed file's symbols have to be compared. Deleting a file only re-analysed the files depending on it. A file can have an error because a symbol exists without depending on its file - a dead catch around new ReflectionFunction('App\foo'), a comparison with a class name in a string - so a deleted file that declared symbols re-analyses the files with errors too, like removing them by an edit does. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c5c8-8d5b-7459-a22c-beb8e7ba8a3b
1 parent 63d6738 commit a6c4f96

6 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎e2e/result-cache-scanned-2/result-cache-scanned-2_test.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ function test_result_cache_scanned_2() {
4141
assert_same "1" "$CODE"
4242
echo "$OUTPUT"
4343
assert_contains 'UsesExtra.php:10:Method ResultCacheE2EScanned2\UsesExtra::doUsesExtra() should return int but returns string.' "$OUTPUT"
44-
# Deleting a scanned file re-analyses the files depending on it.
44+
# Deleting a scanned file re-analyses the files depending on it, and because it declared a
45+
# symbol, the files with errors too: src/Foo.php and src/UsesExtra.php.
4546
rm scanned/Dep.php
4647
INFO=$(../../bin/phpstan result-cache-info --json)
4748
echo "$INFO"
4849
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
49-
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
50+
assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
5051
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
5152
assert_same "1" "$CODE"
5253
echo "$OUTPUT"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 8
3+
tmpDir: tmp
4+
paths:
5+
- src
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
# @tags result-cache
3+
4+
function set_up() {
5+
e2e::set_up
6+
}
7+
8+
function tear_down() {
9+
e2e::tear_down
10+
}
11+
12+
function assert_same_as_cold_run() {
13+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
14+
echo "$OUTPUT"
15+
rm -rf tmp/resultCache.php
16+
COLD_OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && COLD_CODE=0 || COLD_CODE=$?
17+
echo "$COLD_OUTPUT"
18+
assert_same "$COLD_CODE" "$CODE"
19+
assert_same "$COLD_OUTPUT" "$OUTPUT"
20+
}
21+
22+
function test_inline_ignored_error_waits_for_the_symbol() {
23+
# src/User.php has no error left after its ignore, but the ignored error waits for the class.
24+
printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doUser(): object\n{\n\treturn new Thing(); // @phpstan-ignore class.notFound\n}\n' > src/User.php
25+
../../bin/phpstan analyse > /dev/null 2>&1 || true
26+
# Declaring it re-analyses src/User.php too, where the ignore now matches nothing.
27+
printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nclass Thing\n{\n\n}\n' > src/Thing.php
28+
INFO=$(../../bin/phpstan result-cache-info --json)
29+
echo "$INFO"
30+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
31+
assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
32+
assert_same_as_cold_run
33+
assert_contains 'User.php:7:No error with identifier class.notFound is reported on line 7.' "$OUTPUT"
34+
}
35+
36+
function test_inline_ignored_error_waits_for_a_symbol_in_an_existing_file() {
37+
# src/User.php has no error left after its ignore, but the ignored error waits for the class.
38+
printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doUser(): object\n{\n\treturn new Thing(); // @phpstan-ignore class.notFound\n}\n' > src/User.php
39+
../../bin/phpstan analyse > /dev/null 2>&1 || true
40+
# The same when the class is declared in a file that already exists.
41+
printf '\nclass Thing\n{\n\n}\n' >> src/Other.php
42+
INFO=$(../../bin/phpstan result-cache-info --json)
43+
echo "$INFO"
44+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
45+
assert_equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
46+
assert_same_as_cold_run
47+
assert_contains 'User.php:7:No error with identifier class.notFound is reported on line 7.' "$OUTPUT"
48+
}
49+
50+
function test_deleted_file_removes_a_symbol() {
51+
# src/Caller.php has an error because App\doThing() exists, but it does not depend on its file:
52+
# the function is only named in a string.
53+
printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doThing(): void\n{\n}\n' > src/thing.php
54+
printf '<?php declare(strict_types = 1);\n\nnamespace ResultCacheE2ESymbolSweep;\n\nfunction doCaller(): ?\\ReflectionFunction\n{\n\ttry {\n\t\treturn new \\ReflectionFunction('"'"'ResultCacheE2ESymbolSweep\\\\doThing'"'"');\n\t} catch (\\ReflectionException $e) {\n\t\treturn null;\n\t}\n}\n' > src/Caller.php
55+
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) || true
56+
echo "$OUTPUT"
57+
assert_contains 'Caller.php:9:Dead catch - ReflectionException is never thrown in the try block.' "$OUTPUT"
58+
# Deleting the file re-analyses src/Caller.php, the file with an error.
59+
rm src/thing.php
60+
INFO=$(../../bin/phpstan result-cache-info --json)
61+
echo "$INFO"
62+
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
63+
assert_equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
64+
assert_same_as_cold_run
65+
assert_not_contains 'Dead catch' "$OUTPUT"
66+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2ESymbolSweep;
4+
5+
class Other
6+
{
7+
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.*

‎src/Analyser/ResultCache/ResultCacheManager.php‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,11 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
829829
$filesWithErrorsAllChanged = $filteredErrors === [];
830830
foreach ($allAnalysedFiles as $analysedFile) {
831831
if (
832-
(!array_key_exists($analysedFile, $errors) && !array_key_exists($analysedFile, $cachedFilesWithCollectedDataRuleErrors))
832+
(
833+
!array_key_exists($analysedFile, $errors)
834+
&& !array_key_exists($analysedFile, $locallyIgnoredErrors)
835+
&& !array_key_exists($analysedFile, $cachedFilesWithCollectedDataRuleErrors)
836+
)
833837
|| array_key_exists($analysedFile, $changedAnalysedFiles)
834838
) {
835839
continue;
@@ -1043,6 +1047,14 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
10431047
$dependentFiles = array_merge($dependentFiles, $usedTraitDependentFiles);
10441048
} else {
10451049
$dependencyFilesChanged = true;
1050+
// Deleted. The files using its symbols depend on it and are re-analysed, but a file can
1051+
// also have an error because a symbol exists - a dead catch of ReflectionException, a
1052+
// comparison with a class name that is always false - without depending on its file.
1053+
$deletedFileExportedNodes = $exportedNodes[$notAnalysedFile]
1054+
?? ($cachedExportedNodes->has($notAnalysedFile) ? $decodeCachedExportedNodes($notAnalysedFile) : []);
1055+
if (count($deletedFileExportedNodes) > 0) {
1056+
$notAnalysedFileSymbolsChanged = true;
1057+
}
10461058
}
10471059

10481060
foreach ($dependentFiles as $dependentFile) {
@@ -1104,6 +1116,11 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
11041116

11051117
$filesToAnalyse[] = $fileWithError;
11061118
}
1119+
// An error ignored by a comment on its line waits for the symbol all the same, and when it
1120+
// goes away, the comment no longer matches anything, which is reported.
1121+
foreach (array_keys($filteredLocallyIgnoredErrors) as $fileWithError) {
1122+
$filesToAnalyse[] = $fileWithError;
1123+
}
11071124
foreach ($filteredFilesWithCollectedDataRuleErrors as $fileWithError) {
11081125
$filesToAnalyse[] = $fileWithError;
11091126
}

0 commit comments

Comments
 (0)