|
40 | 40 | use RuntimeException; |
41 | 41 | use Throwable; |
42 | 42 | use function array_diff; |
| 43 | +use function array_diff_key; |
43 | 44 | use function array_fill_keys; |
44 | 45 | use function array_filter; |
45 | 46 | use function array_intersect; |
@@ -899,9 +900,18 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ? |
899 | 900 | // the file may have gained its first symbol, which is exactly what the files with errors are |
900 | 901 | // waiting for. Comparing against an empty list says so, and says nothing changed when the |
901 | 902 | // file still declares nothing. |
| 903 | + $fileExportedNodes = $this->exportedNodeFetcher->fetchNodesIfParsable($this->fileReplacements[$analysedFile] ?? $analysedFile); |
| 904 | + if ($fileExportedNodes === null) { |
| 905 | + // A parse error, typically code saved half typed. The file is re-analysed and reports it, |
| 906 | + // but its symbols count as unchanged: the files using them keep their results until it is |
| 907 | + // fixed, instead of being analysed against a file that declares nothing, and so do the |
| 908 | + // files with errors. process() keeps the symbols it had, which the fixed file is compared |
| 909 | + // with. |
| 910 | + continue; |
| 911 | + } |
902 | 912 | $cachedFileExportedNodes = $filteredExportedNodes[$analysedFile] |
903 | 913 | ?? ($cachedExportedNodes->has($analysedFile) ? $decodeCachedExportedNodes($analysedFile) : []); |
904 | | - $exportedNodesChanged = $this->exportedNodesChanged($analysedFile, $cachedFileExportedNodes); |
| 914 | + $exportedNodesChanged = $this->compareExportedNodes($cachedFileExportedNodes, $fileExportedNodes); |
905 | 915 | if ($exportedNodesChanged === null) { |
906 | 916 | if (count($cachedFileExportedNodes) === 0) { |
907 | 917 | continue; |
@@ -1244,19 +1254,6 @@ private function areAllChanged(array $files, array $changedFiles): bool |
1244 | 1254 | return true; |
1245 | 1255 | } |
1246 | 1256 |
|
1247 | | - /** |
1248 | | - * @param array<int, RootExportedNode> $cachedFileExportedNodes |
1249 | | - * @return bool|null null means nothing changed, true means new root symbol appeared, false means nested node changed |
1250 | | - */ |
1251 | | - private function exportedNodesChanged(string $analysedFile, array $cachedFileExportedNodes): ?bool |
1252 | | - { |
1253 | | - if (array_key_exists($analysedFile, $this->fileReplacements)) { |
1254 | | - $analysedFile = $this->fileReplacements[$analysedFile]; |
1255 | | - } |
1256 | | - |
1257 | | - return $this->compareExportedNodes($cachedFileExportedNodes, $this->exportedNodeFetcher->fetchNodes($analysedFile)); |
1258 | | - } |
1259 | | - |
1260 | 1257 | /** |
1261 | 1258 | * @param array<int, RootExportedNode> $cachedFileExportedNodes |
1262 | 1259 | * @param array<int, RootExportedNode> $fileExportedNodes |
@@ -1521,8 +1518,27 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache |
1521 | 1518 | $usedTraitDependencies = $this->mergeDependencies($resultCache->getUsedTraitDependencies(), $resultCache->getFilesToAnalyse(), $analyserResult->getUsedTraitDependencies()); |
1522 | 1519 | $valueDependencies = $this->mergeValueDependencies($resultCache->getValueDependencies(), $resultCache->getFilesToAnalyse(), $analyserResult->getValueDependencies()); |
1523 | 1520 | $packageDependencies = $this->mergePackageDependencies($resultCache->getPackageDependencies(), $resultCache->getFilesToAnalyse(), $analyserResult->getPackageDependencies()); |
| 1521 | + // A re-analysed file with a parse error keeps the symbols it had before, see restore(), so that |
| 1522 | + // fixing it re-analyses nothing else when they are the same. But only while no other file is |
| 1523 | + // analysed: that file sees the broken file's symbols missing, and its result has to be renewed |
| 1524 | + // when they come back, which comparing the fixed file with no symbols does. |
| 1525 | + $filesToReplaceExportedNodes = array_fill_keys($resultCache->getFilesToAnalyse(), true); |
| 1526 | + $filesWithParseError = []; |
| 1527 | + foreach ($freshErrorsByFile as $file => $errors) { |
| 1528 | + foreach ($errors as $error) { |
| 1529 | + if ($error->getIdentifier() !== 'phpstan.parse' || !$error->hasNonIgnorableException()) { |
| 1530 | + continue; |
| 1531 | + } |
| 1532 | + |
| 1533 | + $filesWithParseError[$file] = true; |
| 1534 | + } |
| 1535 | + } |
| 1536 | + if (array_diff_key($filesToReplaceExportedNodes, $filesWithParseError) === []) { |
| 1537 | + $filesToReplaceExportedNodes = []; |
| 1538 | + } |
| 1539 | + |
1524 | 1540 | // the re-analysed files take their fresh nodes, what is left of the cached ones stays undecoded |
1525 | | - $cachedExportedNodes = $resultCache->getCachedExportedNodes()->without(array_fill_keys($resultCache->getFilesToAnalyse(), true)); |
| 1541 | + $cachedExportedNodes = $resultCache->getCachedExportedNodes()->without($filesToReplaceExportedNodes); |
1526 | 1542 | $exportedNodes = $this->addNonAnalysedExportedNodes($this->mergeExportedNodes($resultCache, $analyserResult->getExportedNodes()), $dependencies, $usedTraitDependencies, $cachedExportedNodes); |
1527 | 1543 | $linesToIgnore = $this->mergeLinesToIgnore($resultCache, $analyserResult->getLinesToIgnore()); |
1528 | 1544 | $unmatchedLineIgnores = $this->mergeUnmatchedLineIgnores($resultCache, $analyserResult->getUnmatchedLineIgnores()); |
|
0 commit comments