|
34 | 34 | use PHPStan\Php\PhpVersion; |
35 | 35 | use PHPStan\PhpDoc\StubFilesProvider; |
36 | 36 | use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; |
| 37 | +use PHPStan\Reflection\BetterReflection\SourceLocator\SymbolFinderInFiles; |
37 | 38 | use PHPStan\Reflection\BetterReflection\SourceStubber\ExtensionVersionProvider; |
38 | 39 | use PHPStan\ShouldNotHappenException; |
39 | 40 | use ReflectionClass; |
@@ -264,6 +265,7 @@ public function __construct( |
264 | 265 | private ComposerPhpVersionFactory $composerPhpVersionFactory, |
265 | 266 | private FileStatSignatures $fileStatSignatures, |
266 | 267 | private ValueDependencyCollector $valueDependencyCollector, |
| 268 | + private SymbolFinderInFiles $symbolFinderInFiles, |
267 | 269 | private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerSourceLocatorMaker, |
268 | 270 | #[AutowiredParameter] |
269 | 271 | private string $tmpDir, |
@@ -325,6 +327,7 @@ private function fullAnalysis( |
325 | 327 | cachedExportedNodes: CachedExportedNodes::createEmpty(), |
326 | 328 | projectExtensionFiles: [], |
327 | 329 | currentFileHashes: $currentFileHashes, |
| 330 | + appearedSymbols: AppearedSymbols::anything(), |
328 | 331 | ); |
329 | 332 | } |
330 | 333 |
|
@@ -488,6 +491,9 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
488 | 491 | // make anything be re-analysed. A scanned file can change without any of its symbols changing, |
489 | 492 | // and then nothing here has to happen at all. |
490 | 493 | $scannedFilesWithChangedSymbols = []; |
| 494 | + // what may declare a symbol a lookup did not find last time - see AppearedSymbols |
| 495 | + $anySymbolMayHaveAppeared = false; |
| 496 | + $changedDeclaringFiles = []; |
491 | 497 | $metaDifferent = $this->isMetaDifferent($data['meta'], $meta); |
492 | 498 | if ($metaDifferent) { |
493 | 499 | $diffs = $this->getMetaKeyDifferences($data['meta'], $meta); |
@@ -540,6 +546,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
540 | 546 | ); |
541 | 547 | } |
542 | 548 |
|
| 549 | + $changedDeclaringFiles[$changedScannedFile] = true; |
543 | 550 | } |
544 | 551 | } |
545 | 552 |
|
@@ -617,6 +624,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
617 | 624 | // The files depending on a changed package are seeded below. A symbol a package declares |
618 | 625 | // now was looked up and not found by the files waiting for it, which recorded that as a |
619 | 626 | // value dependency. |
| 627 | + $anySymbolMayHaveAppeared = true; |
620 | 628 | $changedPackagesLookup = array_fill_keys(array_merge($changedPackages, $changedExtensionPackages), true); |
621 | 629 | foreach ($packageDependencies as $packageDependentFile => $filePackages) { |
622 | 630 | foreach ($filePackages as $filePackage) { |
@@ -785,6 +793,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
785 | 793 | } |
786 | 794 |
|
787 | 795 | $changedAnalysedFiles[$analysedFile] = true; |
| 796 | + $changedDeclaringFiles[$analysedFile] = true; |
788 | 797 | } |
789 | 798 | foreach ($allAnalysedFiles as $analysedFile) { |
790 | 799 | if (array_key_exists($analysedFile, $errors)) { |
@@ -906,6 +915,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
906 | 915 |
|
907 | 916 | if (is_file($notAnalysedFile) && $wasMissing) { |
908 | 917 | $dependencyFilesChanged = true; |
| 918 | + $changedDeclaringFiles[$notAnalysedFile] = true; |
909 | 919 | // It exists now. Whether it holds any symbol is beside the point - a file that was named |
910 | 920 | // and was not there is now there, and that alone changes what the analysis says. |
911 | 921 | $invertedDependenciesToReturn[$notAnalysedFile] = $dependentFiles; |
@@ -936,6 +946,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
936 | 946 | ?? ($cachedExportedNodes->has($notAnalysedFile) ? $decodeCachedExportedNodes($notAnalysedFile) : null); |
937 | 947 |
|
938 | 948 | $dependencyFilesChanged = true; |
| 949 | + $changedDeclaringFiles[$notAnalysedFile] = true; |
939 | 950 | // Edited: the same rule as for an analysed file. Nothing the files depending on it can |
940 | 951 | // see changed unless its exported nodes did, so a body-only edit re-analyses nothing - |
941 | 952 | // except the classes using a trait declared here, whose body is analysed in their |
@@ -997,10 +1008,22 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
997 | 1008 | $allAnalysedFilesSet = array_fill_keys($allAnalysedFiles, true); |
998 | 1009 | $valueDependenciesToReturn = []; |
999 | 1010 | $valueDependenciesChanged = false; |
| 1011 | + $appearedSymbols = $anySymbolMayHaveAppeared |
| 1012 | + ? AppearedSymbols::anything() |
| 1013 | + : $this->getSymbolsDeclaredIn(array_keys($changedDeclaringFiles)); |
1000 | 1014 | foreach ($cachedValueDependencies as $cachedValueDependency) { |
1001 | 1015 | $extension = $this->valueDependencyCollector->getExtension($cachedValueDependency['extensionClass']); |
1002 | 1016 | $key = $extension?->keyFromResultCache($cachedValueDependency['storedKey']); |
1003 | | - $currentValue = $extension !== null && $key !== null ? $extension->getValue($key) : null; |
| 1017 | + if ($extension === null || $key === null) { |
| 1018 | + $currentValue = null; |
| 1019 | + } elseif ( |
| 1020 | + $cachedValueDependency['value'] === ValueDependencyCollector::MISSING |
| 1021 | + && $appearedSymbols->isStillMissing($cachedValueDependency['extensionClass'], $key) |
| 1022 | + ) { |
| 1023 | + $currentValue = ValueDependencyCollector::MISSING; |
| 1024 | + } else { |
| 1025 | + $currentValue = $extension->getValue($key); |
| 1026 | + } |
1004 | 1027 | if ($currentValue !== $cachedValueDependency['value']) { |
1005 | 1028 | $valueDependenciesChanged = true; |
1006 | 1029 | foreach ($this->getValueDependentFilesToAnalyse($cachedValueDependency['analysis'], $cachedValueDependency['declarations'], $invertedDependencies, $allAnalysedFilesSet) as $valueDependentFile) { |
@@ -1086,6 +1109,7 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
1086 | 1109 | cachedExportedNodes: $cachedExportedNodes->only($keptCachedExportedNodes), |
1087 | 1110 | projectExtensionFiles: $data['projectExtensionFiles'], |
1088 | 1111 | currentFileHashes: $currentFileHashes, |
| 1112 | + appearedSymbols: $appearedSymbols, |
1089 | 1113 | collectedDataRuleResults: $collectedDataRuleResults, |
1090 | 1114 | ); |
1091 | 1115 | } |
@@ -3493,6 +3517,36 @@ private function getFileHash(string $path): string |
3493 | 3517 | return $hash; |
3494 | 3518 | } |
3495 | 3519 |
|
| 3520 | + /** |
| 3521 | + * The names a new or changed file declares, scanned the way the directory locators scan them - |
| 3522 | + * see AppearedSymbols. |
| 3523 | + * |
| 3524 | + * @param list<string> $changedFiles |
| 3525 | + */ |
| 3526 | + private function getSymbolsDeclaredIn(array $changedFiles): AppearedSymbols |
| 3527 | + { |
| 3528 | + $existingFiles = []; |
| 3529 | + foreach ($changedFiles as $changedFile) { |
| 3530 | + $changedFile = $this->fileReplacements[$changedFile] ?? $changedFile; |
| 3531 | + if (!is_file($changedFile)) { |
| 3532 | + continue; |
| 3533 | + } |
| 3534 | + |
| 3535 | + $existingFiles[] = $changedFile; |
| 3536 | + } |
| 3537 | + |
| 3538 | + $names = []; |
| 3539 | + foreach ($this->symbolFinderInFiles->findSymbols($existingFiles, $this->phpVersion->supportsEnums()) as $symbols) { |
| 3540 | + foreach ($symbols as $symbolsOfKind) { |
| 3541 | + foreach ($symbolsOfKind as $name) { |
| 3542 | + $names[] = $name; |
| 3543 | + } |
| 3544 | + } |
| 3545 | + } |
| 3546 | + |
| 3547 | + return AppearedSymbols::fromNames($names); |
| 3548 | + } |
| 3549 | + |
3496 | 3550 | /** |
3497 | 3551 | * The files the analysis reads symbols from without analysing them, other than the installed |
3498 | 3552 | * packages: scanFiles, scanDirectories, the files excluded from the analysis in an analysed |
|
0 commit comments