Skip to content

Commit 2347f37

Browse files
SanderMullerclaude
andcommitted
fix: make the unchanged-files cache dependency-aware
The cache only checked each file's own content, so a clean file stayed skipped on warm runs even when one of its dependencies changed, e.g. a parent class method gaining a return type that lets a child file infer its own. A fresh run reports the new change, a warm run misses it. PHPStanNodeScopeResolver now records each file's dependencies during scope resolution using PHPStan's own DependencyResolver. Cache entries store the file's own hash plus one hash per dependency, all re-validated on load; legacy string entries self-upgrade on the next write. A failed capture skips caching entirely rather than caching a partial set. To keep capture cheap, only nodes whose resolver branch can produce dependencies are resolved, and native function calls are skipped via NativeFunctionCallAnalyzer, avoiding PHPStan's function signature map load. Two tripwire tests pin the external assumptions: one parses the bundled DependencyResolver and fails if its branch set changes on a PHPStan bump, the other asserts native function reflections stay fileless. Selective runs (--only, --only-suffix) bypass the cache write, same guard as #8029. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 78e99a4 commit 2347f37

10 files changed

Lines changed: 626 additions & 15 deletions

File tree

‎src/Application/ApplicationFileProcessor.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ private function processFile(File $file, Configuration $configuration): FileProc
173173
if ($fileProcessResult->getSystemErrors() !== []) {
174174
$this->changedFilesDetector->invalidateFile($file->getFilePath());
175175
} elseif (! $configuration->isDryRun() || ! $fileProcessResult->getFileDiff() instanceof FileDiff) {
176-
$this->changedFilesDetector->cacheFile($file->getFilePath());
176+
// a file clean under a subset of rules is not necessarily clean under all rules,
177+
// caching it would hide its pending changes from the next full run
178+
if ($configuration->getOnlyRule() === null && $configuration->getOnlySuffix() === null) {
179+
$this->changedFilesDetector->cacheFile($file->getFilePath());
180+
}
177181
}
178182

179183
return $fileProcessResult;

‎src/Caching/Detector/ChangedFilesDetector.php‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Rector\Caching\Cache;
88
use Rector\Caching\Config\FileHashComputer;
99
use Rector\Caching\Enum\CacheKey;
10+
use Rector\Caching\FileDependencyCollector;
1011
use Rector\Util\FileHasher;
1112

1213
/**
@@ -24,7 +25,8 @@ final class ChangedFilesDetector
2425
public function __construct(
2526
private readonly FileHashComputer $fileHashComputer,
2627
private readonly Cache $cache,
27-
private readonly FileHasher $fileHasher
28+
private readonly FileHasher $fileHasher,
29+
private readonly FileDependencyCollector $fileDependencyCollector
2830
) {
2931
}
3032

@@ -36,9 +38,26 @@ public function cacheFile(string $filePath): void
3638
return;
3739
}
3840

39-
$hash = $this->hashFile($filePath);
41+
// a failed capture means a possibly incomplete set, skip caching so the file is reprocessed
42+
$dependencyHashes = $this->fileDependencyCollector->getDependencyFileHashes($filePath);
43+
if ($dependencyHashes === null) {
44+
return;
45+
}
4046

41-
$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, $hash);
47+
// the file may have just been written, recompute its hash fresh
48+
// rather than trusting a memo entry from the pre-write filter pass
49+
$this->fileDependencyCollector->forgetContentHash($filePath);
50+
$ownHash = $this->fileDependencyCollector->contentHash($filePath);
51+
if ($ownHash === null) {
52+
return;
53+
}
54+
55+
// store the own content hash plus one per dependency, so a dependency change
56+
// invalidates this file even when its own content is unchanged
57+
$this->cache->save($filePathCacheKey, CacheKey::FILE_HASH_KEY, [
58+
'hash' => $ownHash,
59+
'deps' => $dependencyHashes,
60+
]);
4261
}
4362

4463
public function addCacheableFile(string $filePath): void
@@ -52,13 +71,27 @@ public function hasFileChanged(string $filePath): bool
5271
$fileInfoCacheKey = $this->getFilePathCacheKey($filePath);
5372
$cachedValue = $this->cache->load($fileInfoCacheKey, CacheKey::FILE_HASH_KEY);
5473

55-
if ($cachedValue !== null) {
56-
$currentFileHash = $this->hashFile($filePath);
57-
return $currentFileHash !== $cachedValue;
74+
// no value to compare against → be defensive and assume changed
75+
if ($cachedValue === null) {
76+
return true;
77+
}
78+
79+
// legacy string entry → own-hash comparison only, rewritten in the new format on next cacheFile()
80+
if (is_string($cachedValue)) {
81+
return $this->fileDependencyCollector->contentHash($filePath) !== $cachedValue;
5882
}
5983

60-
// we don't have a value to compare against. Be defensive and assume its changed
61-
return true;
84+
if (! is_array($cachedValue)) {
85+
return true;
86+
}
87+
88+
// own content changed
89+
if (($cachedValue['hash'] ?? null) !== $this->fileDependencyCollector->contentHash($filePath)) {
90+
return true;
91+
}
92+
93+
// any recorded dependency changed
94+
return $this->fileDependencyCollector->hasAnyChangedDependency($cachedValue['deps'] ?? []);
6295
}
6396

6497
public function invalidateFile(string $filePath): void
@@ -98,11 +131,6 @@ private function getFilePathCacheKey(string $filePath): string
98131
return $this->fileHasher->hash($this->resolvePath($filePath));
99132
}
100133

101-
private function hashFile(string $filePath): string
102-
{
103-
return $this->fileHasher->hashFiles([$this->resolvePath($filePath)]);
104-
}
105-
106134
private function storeConfigurationDataHash(string $filePath, string $configurationHash): void
107135
{
108136
$key = CacheKey::CONFIGURATION_HASH_KEY . '_' . $this->getFilePathCacheKey($filePath);
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Caching;
6+
7+
use Rector\Contract\DependencyInjection\ResettableInterface;
8+
use Rector\Util\FileHasher;
9+
10+
/**
11+
* Collects the files each processed file depends on, as surfaced by PHPStan's
12+
* DependencyResolver during scope resolution, and provides memoized content hashes
13+
* for dependency validation by the unchanged-files cache.
14+
*/
15+
final class FileDependencyCollector implements ResettableInterface
16+
{
17+
/**
18+
* @var array<string, array<string, true>>
19+
*/
20+
private array $dependenciesByFile = [];
21+
22+
/**
23+
* files whose capture threw, their possibly partial set must never be cached
24+
*
25+
* @var array<string, true>
26+
*/
27+
private array $failedFiles = [];
28+
29+
/**
30+
* keyed by the given path, so memo hits skip realpath() as well
31+
*
32+
* @var array<string, string|null>
33+
*/
34+
private array $contentHashMemo = [];
35+
36+
/**
37+
* a function's signature dependencies are identical at every call site
38+
*
39+
* @var array<string, string[]>
40+
*/
41+
private array $functionDependencyFilesMemo = [];
42+
43+
public function __construct(
44+
private readonly FileHasher $fileHasher,
45+
) {
46+
}
47+
48+
public function record(string $filePath, string $dependencyFilePath): void
49+
{
50+
if ($filePath === $dependencyFilePath) {
51+
return;
52+
}
53+
54+
$this->dependenciesByFile[$filePath][$dependencyFilePath] = true;
55+
}
56+
57+
public function markFailed(string $filePath): void
58+
{
59+
$this->failedFiles[$filePath] = true;
60+
}
61+
62+
/**
63+
* @return string[]|null
64+
*/
65+
public function getMemoizedFunctionDependencyFiles(string $functionKey): ?array
66+
{
67+
return $this->functionDependencyFilesMemo[$functionKey] ?? null;
68+
}
69+
70+
/**
71+
* @param string[] $dependencyFiles
72+
*/
73+
public function memoizeFunctionDependencyFiles(string $functionKey, array $dependencyFiles): void
74+
{
75+
$this->functionDependencyFilesMemo[$functionKey] = $dependencyFiles;
76+
}
77+
78+
/**
79+
* @return array<string, string>|null null when capture failed and the set cannot be trusted
80+
*/
81+
public function getDependencyFileHashes(string $filePath): ?array
82+
{
83+
if (isset($this->failedFiles[$filePath])) {
84+
return null;
85+
}
86+
87+
$dependencyHashes = [];
88+
foreach (array_keys($this->dependenciesByFile[$filePath] ?? []) as $dependencyFile) {
89+
$dependencyHash = $this->contentHash($dependencyFile);
90+
if ($dependencyHash !== null) {
91+
$dependencyHashes[$dependencyFile] = $dependencyHash;
92+
}
93+
}
94+
95+
return $dependencyHashes;
96+
}
97+
98+
/**
99+
* @param array<string, string> $recordedDependencyHashes
100+
*/
101+
public function hasAnyChangedDependency(array $recordedDependencyHashes): bool
102+
{
103+
foreach ($recordedDependencyHashes as $dependencyFile => $recordedHash) {
104+
if ($this->contentHash($dependencyFile) !== $recordedHash) {
105+
return true;
106+
}
107+
}
108+
109+
return false;
110+
}
111+
112+
/**
113+
* null when the file does not exist, e.g. a deleted dependency, which callers treat as changed
114+
*/
115+
public function contentHash(string $filePath): ?string
116+
{
117+
if (array_key_exists($filePath, $this->contentHashMemo)) {
118+
return $this->contentHashMemo[$filePath];
119+
}
120+
121+
$resolvedPath = $this->resolvePath($filePath);
122+
if (! is_file($resolvedPath)) {
123+
return $this->contentHashMemo[$filePath] = null;
124+
}
125+
126+
return $this->contentHashMemo[$filePath] = $this->fileHasher->hashFiles([$resolvedPath]);
127+
}
128+
129+
/**
130+
* drop a memoized hash, e.g. after the file has been written mid-run
131+
*/
132+
public function forgetContentHash(string $filePath): void
133+
{
134+
unset($this->contentHashMemo[$filePath]);
135+
}
136+
137+
public function reset(): void
138+
{
139+
$this->dependenciesByFile = [];
140+
$this->failedFiles = [];
141+
$this->contentHashMemo = [];
142+
$this->functionDependencyFilesMemo = [];
143+
}
144+
145+
private function resolvePath(string $filePath): string
146+
{
147+
$realPath = realpath($filePath);
148+
if ($realPath === false) {
149+
return $filePath;
150+
}
151+
152+
return $realPath;
153+
}
154+
}

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Lexer;
1111
use PHPStan\Analyser\NodeScopeResolver;
1212
use PHPStan\Analyser\ScopeFactory;
13+
use PHPStan\Dependency\DependencyResolver;
1314
use PHPStan\Parser\Parser;
1415
use PHPStan\PhpDoc\TypeNodeResolver;
1516
use PHPStan\PhpDocParser\ParserConfig;
@@ -38,6 +39,7 @@
3839
use Rector\Caching\CacheFactory;
3940
use Rector\Caching\Config\FileHashComputer;
4041
use Rector\Caching\Contract\CacheMetaExtensionInterface;
42+
use Rector\Caching\FileDependencyCollector;
4143
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
4244
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
4345
use Rector\ChangesReporting\Output\GitHubOutputFormatter;
@@ -347,6 +349,7 @@ final class LazyContainerFactory
347349
TypeNodeResolver::class,
348350
NodeScopeResolver::class,
349351
ReflectionProvider::class,
352+
DependencyResolver::class,
350353
];
351354

352355
/**
@@ -452,6 +455,7 @@ public function create(): RectorConfig
452455

453456
$rectorConfig->singleton(FileProcessor::class);
454457
$rectorConfig->singleton(PostFileProcessor::class);
458+
$rectorConfig->singleton(FileDependencyCollector::class);
455459

456460
$rectorConfig->when(RectorNodeTraverser::class)
457461
->needs('$rectors')
@@ -476,6 +480,7 @@ static function (Container $container): DynamicSourceLocatorProvider {
476480
// resettable
477481
$rectorConfig->tag(DynamicSourceLocatorProvider::class, ResettableInterface::class);
478482
$rectorConfig->tag(RenamedClassesDataCollector::class, ResettableInterface::class);
483+
$rectorConfig->tag(FileDependencyCollector::class, ResettableInterface::class);
479484

480485
// caching
481486
$rectorConfig->singleton(Cache::class, static function (Container $container): Cache {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\NodeAnalyzer;
6+
7+
use PhpParser\Node\Name;
8+
9+
/**
10+
* Whether a called function name can only resolve to a native PHP function.
11+
* Qualified and relative names resolve strictly inside a namespace, use-function
12+
* imports are honored via the resolvedName attribute. Known limitation: an
13+
* unimported userland function shadowing a native name in its own namespace is
14+
* treated as native anyway. Unlike FuncCallNameResolver this stays off
15+
* ReflectionProvider, as it runs per node during scope resolution.
16+
*
17+
* @see \Rector\NodeNameResolver\NodeNameResolver\FuncCallNameResolver
18+
* @see \Rector\Tests\NodeAnalyzer\NativeFunctionCallAnalyzerTest
19+
*/
20+
final class NativeFunctionCallAnalyzer
21+
{
22+
/**
23+
* @var array<string, int>|null
24+
*/
25+
private array|null $internalFunctionNames = null;
26+
27+
public function isNativeFunctionCall(Name $name): bool
28+
{
29+
if ($name->isQualified() || $name->isRelative()) {
30+
return false;
31+
}
32+
33+
$this->internalFunctionNames ??= array_flip(get_defined_functions()['internal']);
34+
35+
$resolvedName = $name->getAttribute('resolvedName');
36+
if ($resolvedName instanceof Name) {
37+
return isset($this->internalFunctionNames[$resolvedName->toLowerString()]);
38+
}
39+
40+
return isset($this->internalFunctionNames[$name->toLowerString()]);
41+
}
42+
}

0 commit comments

Comments
 (0)