Skip to content

Commit 3eb12b6

Browse files
ondrejmirtesclaude
andcommitted
Compare the exported nodes of an edited non-analysed file before re-analysing its dependents
An edited scanned file re-analysed every file depending on it, and the files with errors on top, whatever the edit was. An analysed file is treated with more care: only a change in its exported nodes - a signature, a PHPDoc, a visibility, a symbol added or removed - reaches the files depending on it, because nothing else is visible to them; a body-only edit re-analyses nothing (except the classes using a trait declared in the file, whose body is analysed in their context). The non-analysed files the analysed code depends on - scanned files, files reached only through the autoloader - now get the same treatment. Nothing analyses them, so their exported nodes are fetched when the cache is saved (only for the files without an entry: an edited file has its entry refreshed at restore time, a deleted one loses it), and restore() compares them with the file's current nodes exactly as it does for an analysed file: nothing changed - nothing to do; a nested node changed - the dependents; a root symbol appeared or disappeared - the dependents and the files with errors, since a file that reported the symbol as unknown recorded no edge to the file. The scannedFiles metadata is left with what the dependency graph cannot see: a scanned file that appeared, or an edited one nothing depends on. Either re-analyses the files with errors only when it declares any symbol at all - a config file returning an array cannot define what an error is about. Without the previous nodes of a file nothing depends on, "declares a symbol" has to stand in for "declares a new symbol" there. e2e/result-cache-scanned-exported-nodes: a body-only edit of a scanned file re-analyses nothing, twice; a changed return type re-analyses the one file depending on it and leaves the file with an unrelated error alone; a file without symbols appearing in the scanned directory re-analyses nothing; a file declaring the missing class re-analyses the files with errors. The first, third and fourth steps failed before this change. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WvMWsNHbCdSgx97Y56XYcU
1 parent cd08d56 commit 3eb12b6

11 files changed

Lines changed: 274 additions & 38 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,50 @@ jobs:
312312
../bashunit -a contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
313313
../bashunit -a not_contains 'Instantiated class ResultCacheE2EScannedNewFile\NewlyScanned not found.' "$OUTPUT"
314314
../bashunit -a contains 'UsesNew.php:10:Method ResultCacheE2EScannedNewFile\UsesNew::doUsesNew() should return int but returns string.' "$OUTPUT"
315+
- script: |
316+
cd e2e/result-cache-scanned-exported-nodes
317+
# src/Bar.php refers to a class that does not exist yet, so it has an error from the start.
318+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
319+
echo "$OUTPUT"
320+
../bashunit -a contains 'Bar.php:10:Instantiated class ResultCacheE2EScannedExportedNodes\NewDep not found.' "$OUTPUT"
321+
# An edit inside a method body of a scanned file changes nothing the files depending on it
322+
# can see, so nothing is re-analysed - not the file depending on it, and not the file
323+
# with the error either. The same as for an analysed file, where only a change in the
324+
# exported nodes (a signature, a PHPDoc, a visibility) reaches the dependents.
325+
patch -b scanned/Dep.php < body.patch
326+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
327+
echo "$OUTPUT"
328+
../bashunit -a contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
329+
# The new contents are recorded: the next run has nothing to notice.
330+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
331+
echo "$OUTPUT"
332+
../bashunit -a not_contains 'Scanned files changed' "$OUTPUT"
333+
../bashunit -a contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
334+
# A changed return type is an exported node change: the file depending on it is
335+
# re-analysed. A changed signature cannot make a new symbol available, so the file with
336+
# the error is left alone.
337+
mv scanned/Dep.php.orig scanned/Dep.php
338+
patch -b scanned/Dep.php < signature.patch
339+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
340+
echo "$OUTPUT"
341+
../bashunit -a contains 'Result cache restored. 1 file will be reanalysed.' "$OUTPUT"
342+
../bashunit -a contains 'Foo.php:10:Method ResultCacheE2EScannedExportedNodes\Foo::doFoo() should return int but returns string.' "$OUTPUT"
343+
../bashunit -a contains 'Bar.php:10:Instantiated class ResultCacheE2EScannedExportedNodes\NewDep not found.' "$OUTPUT"
344+
# A file without any exported nodes appearing in the scanned directory cannot define the
345+
# missing symbol: nothing is re-analysed.
346+
mv new-file/NoSymbols.php scanned/NoSymbols.php
347+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
348+
echo "$OUTPUT"
349+
../bashunit -a contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
350+
../bashunit -a contains 'Bar.php:10:Instantiated class ResultCacheE2EScannedExportedNodes\NewDep not found.' "$OUTPUT"
351+
# A file declaring a class may define a symbol reported as unknown, so the files with
352+
# errors are re-analysed - both of them - and the error about the class is gone.
353+
mv new-file/NewDep.php scanned/NewDep.php
354+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
355+
echo "$OUTPUT"
356+
../bashunit -a contains 'Result cache restored. 2 files will be reanalysed.' "$OUTPUT"
357+
../bashunit -a not_contains 'Bar.php:10:Instantiated class ResultCacheE2EScannedExportedNodes\NewDep not found.' "$OUTPUT"
358+
../bashunit -a contains 'Foo.php:10:Method ResultCacheE2EScannedExportedNodes\Foo::doFoo() should return int but returns string.' "$OUTPUT"
315359
- script: |
316360
cd e2e/result-cache-scanned-trait
317361
../../bin/phpstan analyse
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--- scanned/Dep.php
2+
+++ scanned/Dep.php
3+
@@ -7,7 +7,9 @@
4+
5+
public function doDep(): int
6+
{
7+
- return 1;
8+
+ $result = 1;
9+
+
10+
+ return $result;
11+
}
12+
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ResultCacheE2EScannedExportedNodes;
4+
5+
class NewDep
6+
{
7+
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'foo' => 'bar',
5+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 8
3+
tmpDir: tmp
4+
paths:
5+
- src
6+
scanFiles:
7+
- scanned/Dep.php
8+
scanDirectories:
9+
- scanned
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ResultCacheE2EScannedExportedNodes;
4+
5+
class Dep
6+
{
7+
8+
public function doDep(): int
9+
{
10+
return 1;
11+
}
12+
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- scanned/Dep.php
2+
+++ scanned/Dep.php
3+
@@ -5,9 +5,9 @@
4+
class Dep
5+
{
6+
7+
- public function doDep(): int
8+
+ public function doDep(): string
9+
{
10+
- return 1;
11+
+ return 'foo';
12+
}
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ResultCacheE2EScannedExportedNodes;
4+
5+
class Bar
6+
{
7+
8+
public function doBar(): NewDep
9+
{
10+
return new NewDep();
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace ResultCacheE2EScannedExportedNodes;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(Dep $dep): int
9+
{
10+
return $dep->doDep();
11+
}
12+
13+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.*

0 commit comments

Comments
 (0)