Skip to content

Commit 091c5a7

Browse files
committed
Do not analyse stub files with errors when the result cache is restored
The errors of the stub validation are kept in the result cache under the stub file. When a new file appeared, every file with errors was re-analysed, the stub file too, so the rules for analysed files reported errors in it, such as a missing return statement in each method without a body. Amp-Thread-ID: https://ampcode.com/threads/T-01a18c38-cedb-76c4-8a09-c5e85fea5b60
1 parent 7bca2c6 commit 091c5a7

6 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ jobs:
186186
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
187187
echo "$OUTPUT"
188188
../bashunit assert contains 'Result cache restored.' "$OUTPUT"
189+
- script: |
190+
cd e2e/result-cache-stub-files-errors
191+
# The stub file is validated on the full analysis, and its error is kept in the result cache.
192+
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse --error-format raw")
193+
echo "$OUTPUT"
194+
../bashunit assert contains 'test.stub:8:Method StubFilesResultCache\Foo::doFoo() has parameter $a with no type specified.' "$OUTPUT"
195+
../bashunit assert not_contains 'return statement is missing' "$OUTPUT"
196+
# A new file makes the files with errors re-analysed. The stub file is not one of them,
197+
# otherwise the rules for analysed files would report that doFoo() does not return.
198+
echo '<?php function doBaz(): void {}' > src/Baz.php
199+
OUTPUT=$(../bashunit assert exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
200+
echo "$OUTPUT"
201+
../bashunit assert contains 'Result cache restored.' "$OUTPUT"
202+
../bashunit assert contains 'test.stub:8:Method StubFilesResultCache\Foo::doFoo() has parameter $a with no type specified.' "$OUTPUT"
203+
../bashunit assert not_contains 'return statement is missing' "$OUTPUT"
189204
- script: |
190205
cd e2e/result-cache-worker-count
191206
OUTPUT=$(../../bin/phpstan -vvv 2>&1)

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ e2e/bashunit
2525
/e2e/result-cache-collected-data-rules/tmp
2626
/e2e/result-cache-collected-data-rules-tracking/tmp
2727
/e2e/result-cache-truncated/tmp
28+
/e2e/result-cache-stub-files-errors/tmp
2829
/e2e/result-cache-worker-count/tmp
2930
e2e/restart-manager/probe.log
3031
e2e/restart-manager/starts.log
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: 8
3+
tmpDir: tmp
4+
stubFiles:
5+
- stubs/test.stub
6+
paths:
7+
- src
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace StubFilesResultCache;
4+
5+
function doBar(): string
6+
{
7+
return '';
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace StubFilesResultCache;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo($a): string
9+
{
10+
11+
}
12+
13+
}

‎src/Analyser/ResultCache/ResultCacheManager.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,12 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
10281028

10291029
if ($newFileAppeared || $notAnalysedFileSymbolsChanged) {
10301030
foreach (array_keys($filteredErrors) as $fileWithError) {
1031+
// A stub file has errors from the stub validation, which runs on the full analysis only.
1032+
// It is not an analysed file, so analysing it would report the errors of all rules in it.
1033+
if (array_key_exists($fileWithError, $cachedStubFiles)) {
1034+
continue;
1035+
}
1036+
10311037
$filesToAnalyse[] = $fileWithError;
10321038
}
10331039
}

0 commit comments

Comments
 (0)