Skip to content

Commit 35c2588

Browse files
ondrejmirtesclaude
andcommitted
$scope->fileDependency() on top of value dependencies
$scope->fileDependency($path) declares that the analysis of the current file depends on the contents of another file - a data file, a template, a docblock in a PHP file - that is read without PHPStan knowing about it. It is a value dependency through FileResultCacheValueExtension: the value is the hash of the file, or that the file does not exist, so creating, changing or deleting it re-analyses the files that declared it. The path is stored relative to the same directory as the other paths in the result cache. A dependency of the analysed file on itself is dropped, the file is re-analysed when it changes anyway. RequireFileExistsRule declares the paths on the scope now, and the paths from the deprecated RuleErrorBuilder::fileDependency() are declared on the scope of the rule's node. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GdwtgiEHwkF5BTz73jgHQQ
1 parent 68326a9 commit 35c2588

37 files changed

Lines changed: 712 additions & 26 deletions

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,131 @@ jobs:
497497
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
498498
echo "$OUTPUT"
499499
../bashunit -a not_contains 'Holder.php' "$OUTPUT"
500+
- script: |
501+
cd e2e/result-cache-file-dependency
502+
composer install
503+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
504+
echo "$OUTPUT"
505+
../bashunit -a contains "UsesFunction.php:10:Function ResultCacheE2EFileDependency\usesFunction() should return 'none' but returns 'function-1'." "$OUTPUT"
506+
../bashunit -a contains "UsesMethod.php:10:Function ResultCacheE2EFileDependency\usesMethod() should return 'none' but returns 'method-1'." "$OUTPUT"
507+
../bashunit -a contains "UsesStaticMethod.php:10:Function ResultCacheE2EFileDependency\usesStaticMethod() should return 'none' but returns 'static-method-1'." "$OUTPUT"
508+
../bashunit -a contains 'Holder.php:17:Method ResultCacheE2EFileDependency\Holder::get() should return int but returns string.' "$OUTPUT"
509+
../bashunit -a contains 'UsesHolder.php:9:Function ResultCacheE2EFileDependency\usesHolder() should return int but returns string.' "$OUTPUT"
510+
../bashunit -a contains 'UsesDeprecatedConfig.php:7:Deprecated config asks for an error.' "$OUTPUT"
511+
../bashunit -a not_contains 'Config asks for an error.' "$OUTPUT"
512+
# The paths declared with the deprecated RuleErrorBuilder::fileDependency() are declared on the
513+
# scope of the rule's node.
514+
printf 'quiet\n' > data/deprecated.txt
515+
INFO=$(../../bin/phpstan result-cache-info --json)
516+
echo "$INFO"
517+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
518+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
519+
echo "$OUTPUT"
520+
../bashunit -a not_contains 'Deprecated config asks for an error.' "$OUTPUT"
521+
# A rule declares its dependency on src/Config.php whether it reports an error or not. The file
522+
# declares no symbols, so no change of it reaches its exported nodes - any change re-analyses the
523+
# file of the rule's node anyway, next to the changed file itself.
524+
printf "<?php\n\nreturn 'report';\n" > src/Config.php
525+
INFO=$(../../bin/phpstan result-cache-info --json)
526+
echo "$INFO"
527+
../bashunit -a equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
528+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
529+
echo "$OUTPUT"
530+
../bashunit -a contains 'UsesConfig.php:7:Config asks for an error.' "$OUTPUT"
531+
printf "<?php\n\n// a change that keeps the error\nreturn 'report';\n" > src/Config.php
532+
INFO=$(../../bin/phpstan result-cache-info --json)
533+
echo "$INFO"
534+
../bashunit -a equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
535+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
536+
echo "$OUTPUT"
537+
../bashunit -a contains 'UsesConfig.php:7:Config asks for an error.' "$OUTPUT"
538+
# Dynamic return type extensions of a function, a method and a static method declared in
539+
# src/functions.php and src/Repository.php read a data file each. A change of one re-analyses the
540+
# file with the call - not the one declaring the function or the method.
541+
printf 'function-2\n' > data/function.txt
542+
INFO=$(../../bin/phpstan result-cache-info --json)
543+
echo "$INFO"
544+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
545+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
546+
echo "$OUTPUT"
547+
../bashunit -a contains "UsesFunction.php:10:Function ResultCacheE2EFileDependency\usesFunction() should return 'none' but returns 'function-2'." "$OUTPUT"
548+
printf 'method-2\n' > data/method.txt
549+
INFO=$(../../bin/phpstan result-cache-info --json)
550+
echo "$INFO"
551+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
552+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
553+
echo "$OUTPUT"
554+
../bashunit -a contains "UsesMethod.php:10:Function ResultCacheE2EFileDependency\usesMethod() should return 'none' but returns 'method-2'." "$OUTPUT"
555+
printf 'static-method-2\n' > data/static-method.txt
556+
INFO=$(../../bin/phpstan result-cache-info --json)
557+
echo "$INFO"
558+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
559+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
560+
echo "$OUTPUT"
561+
../bashunit -a contains "UsesStaticMethod.php:10:Function ResultCacheE2EFileDependency\usesStaticMethod() should return 'none' but returns 'static-method-2'." "$OUTPUT"
562+
# Scope::getFile() is not the file to re-analyse when the extension runs outside the walk of the
563+
# analysed file: the type of Holder::$value is inferred from the constructor on a scope of
564+
# src/Holder.php, whenever the property is first needed - in the analysis of src/Holder.php, or of
565+
# src/UsesHolder.php reading it - and it is cached. So it is what src/Holder.php declares that
566+
# depends on data/holder.txt: a change of it re-analyses src/Holder.php and the files depending on it.
567+
printf 'int\n' > data/holder.txt
568+
INFO=$(../../bin/phpstan result-cache-info --json)
569+
echo "$INFO"
570+
../bashunit -a equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
571+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
572+
echo "$OUTPUT"
573+
../bashunit -a not_contains 'Holder.php:17:Method ResultCacheE2EFileDependency\Holder::get() should return int but returns string.' "$OUTPUT"
574+
../bashunit -a not_contains 'UsesHolder.php:9:Function ResultCacheE2EFileDependency\usesHolder() should return int but returns string.' "$OUTPUT"
575+
# Deleting a declared file re-analyses the files depending on it too, and it stays watched while
576+
# it is missing: creating it again re-analyses them again.
577+
rm data/function.txt
578+
INFO=$(../../bin/phpstan result-cache-info --json)
579+
echo "$INFO"
580+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
581+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
582+
echo "$OUTPUT"
583+
../bashunit -a contains "UsesFunction.php:10:Function ResultCacheE2EFileDependency\usesFunction() should return 'none' but returns 'missing'." "$OUTPUT"
584+
printf 'function-3\n' > data/function.txt
585+
INFO=$(../../bin/phpstan result-cache-info --json)
586+
echo "$INFO"
587+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
588+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
589+
echo "$OUTPUT"
590+
../bashunit -a contains "UsesFunction.php:10:Function ResultCacheE2EFileDependency\usesFunction() should return 'none' but returns 'function-3'." "$OUTPUT"
591+
# The same for what src/Holder.php declares.
592+
rm data/holder.txt
593+
INFO=$(../../bin/phpstan result-cache-info --json)
594+
echo "$INFO"
595+
../bashunit -a equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
596+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
597+
echo "$OUTPUT"
598+
../bashunit -a contains 'Holder.php:17:Method ResultCacheE2EFileDependency\Holder::get() should return int but returns string.' "$OUTPUT"
599+
../bashunit -a contains 'UsesHolder.php:9:Function ResultCacheE2EFileDependency\usesHolder() should return int but returns string.' "$OUTPUT"
600+
printf 'int\n' > data/holder.txt
601+
INFO=$(../../bin/phpstan result-cache-info --json)
602+
echo "$INFO"
603+
../bashunit -a equals '2' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
604+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
605+
echo "$OUTPUT"
606+
../bashunit -a not_contains 'Holder.php:17:Method ResultCacheE2EFileDependency\Holder::get() should return int but returns string.' "$OUTPUT"
607+
../bashunit -a not_contains 'UsesHolder.php:9:Function ResultCacheE2EFileDependency\usesHolder() should return int but returns string.' "$OUTPUT"
608+
# And for a declared file that is analysed itself: deleted, it is no longer analysed, and the file
609+
# of the rule's node is re-analysed; created again, both are - and as with any new file, so are
610+
# the three files with errors.
611+
rm src/Config.php
612+
INFO=$(../../bin/phpstan result-cache-info --json)
613+
echo "$INFO"
614+
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
615+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
616+
echo "$OUTPUT"
617+
../bashunit -a not_contains 'Config asks for an error.' "$OUTPUT"
618+
printf "<?php\n\nreturn 'report';\n" > src/Config.php
619+
INFO=$(../../bin/phpstan result-cache-info --json)
620+
echo "$INFO"
621+
../bashunit -a equals '5' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
622+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
623+
echo "$OUTPUT"
624+
../bashunit -a contains 'UsesConfig.php:7:Config asks for an error.' "$OUTPUT"
500625
- script: |
501626
cd e2e/result-cache-scanned-trait
502627
../../bin/phpstan analyse
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"autoload-dev": {
3+
"classmap": ["extension/"]
4+
}
5+
}

‎e2e/result-cache-file-dependency/composer.lock‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
report
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function-1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
text
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
method-1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static-method-1
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2EFileDependency;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\FuncCall;
7+
use PhpParser\Node\Name;
8+
use PHPStan\Analyser\DependencyEmitter;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
12+
13+
/**
14+
* Reports calls to checkConfig() when src/Config.php asks for it. The file is read whether an
15+
* error is reported or not, so the dependency is declared either way.
16+
*
17+
* @implements Rule<FuncCall>
18+
*/
19+
final class ConfigRule implements Rule
20+
{
21+
22+
public function getNodeType(): string
23+
{
24+
return FuncCall::class;
25+
}
26+
27+
/**
28+
* @param Scope&DependencyEmitter $scope
29+
*/
30+
public function processNode(Node $node, Scope $scope): array
31+
{
32+
if (!$node->name instanceof Name || $node->name->getLast() !== 'checkConfig') {
33+
return [];
34+
}
35+
36+
$configFile = dirname(__DIR__) . '/src/Config.php';
37+
$scope->fileDependency($configFile);
38+
39+
if (!is_file($configFile)) {
40+
return [];
41+
}
42+
43+
$contents = file_get_contents($configFile);
44+
if ($contents === false || !str_contains($contents, 'report')) {
45+
return [];
46+
}
47+
48+
return [
49+
RuleErrorBuilder::message('Config asks for an error.')->identifier('resultCacheE2E.config')->build(),
50+
];
51+
}
52+
53+
}

0 commit comments

Comments
 (0)