Skip to content

Commit e840006

Browse files
SanderMullerclaude
authored andcommitted
Cache a function reflection under the file that declares it
OptimizedDirectorySourceLocator saved a function reflection under the cache key of the last file it searched. The directory scan also lists methods, so that file can be one with a method of the same name. The entry then stayed valid after the declaring file changed. Bump the variable cache key, so entries written this way are not used. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 3bc56e3 commit e840006

8 files changed

Lines changed: 69 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,15 @@ jobs:
442442
echo "$INFO"
443443
../bashunit -a equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
444444
../bashunit -a equals '1' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
445+
- script: |
446+
cd e2e/result-cache-function-shares-method-name
447+
OUTPUT=$(../bashunit -a exit_code "1" "../../bin/phpstan analyse -vv --error-format raw")
448+
echo "$OUTPUT"
449+
../bashunit -a contains 'Caller.php:10:Parameter #1 $string of function strlen expects string, int given.' "$OUTPUT"
450+
# with-method.php has a method named like the function and is scanned after functions.php,
451+
# so the cached reflection of the function must still be tied to functions.php
452+
patch -b src/functions.php < changeReturnType.patch
453+
../../bin/phpstan analyse -vv --error-format raw
445454
- script: |
446455
cd e2e/result-cache-value-dependency
447456
composer install
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- src/functions.php
2+
+++ src/functions.php
3+
@@ -2,7 +2,7 @@
4+
5+
namespace ResultCacheE2EFunctionSharesMethodName;
6+
7+
-function shared(): int
8+
+function shared(): string
9+
{
10+
- return 1;
11+
+ return 'a';
12+
}
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2EFunctionSharesMethodName;
4+
5+
class Caller
6+
{
7+
8+
public function doCall(): int
9+
{
10+
return strlen(shared());
11+
}
12+
13+
}
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 ResultCacheE2EFunctionSharesMethodName;
4+
5+
function shared(): int
6+
{
7+
return 1;
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2EFunctionSharesMethodName;
4+
5+
class WithMethod
6+
{
7+
8+
public function shared(): void
9+
{
10+
}
11+
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.*

‎src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private function getCacheKeys(string $file, Identifier $identifier): array
8181
}
8282

8383
$reflectionCacheKey = sprintf('odsl-%s-%s-%s', $file, $identifier->getType()->getName(), $identifier->getName());
84-
$variableCacheKey = sprintf('v2-%s-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString(), $fileHash);
84+
$variableCacheKey = sprintf('v3-%s-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString(), $fileHash);
8585

8686
return [$reflectionCacheKey, $variableCacheKey];
8787
}
@@ -160,6 +160,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
160160
return $classReflection;
161161
} elseif ($identifier->isFunction()) {
162162
$fetchedFunctionNode = null;
163+
$fetchedFile = null;
163164
foreach ($files as $file) {
164165
$fetchedFunctionNodes = $this->fileNodesFetcher->fetchNodes($file)->getFunctionNodes();
165166

@@ -169,13 +170,17 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
169170

170171
/** @var FetchedNode<Node\Stmt\Function_> $fetchedFunctionNode */
171172
$fetchedFunctionNode = current($fetchedFunctionNodes[$identifierName]);
173+
$fetchedFile = $file;
172174
}
173175

174-
if ($fetchedFunctionNode === null) {
176+
if ($fetchedFunctionNode === null || $fetchedFile === null) {
175177
return null;
176178
}
177179

178-
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier);
180+
// the file declaring the function, not the last one searched - the symbol scan also lists
181+
// the files with a method of the same name, and the reflection keyed to one of them would
182+
// stay in the cache after the declaring file changes
183+
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($fetchedFile, $identifier);
179184
$functionReflection = $this->nodeToReflection($reflector, $fetchedFunctionNode);
180185
$this->cache->save($reflectionCacheKey, $variableCacheKey, $functionReflection->exportToCache());
181186

0 commit comments

Comments
 (0)