Skip to content

Commit

Permalink
Result cache - fix handling deleted files
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 2, 2020
1 parent 526bb0d commit b13181e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,25 @@ public function restore(array $allAnalysedFiles, bool $debug): ResultCache
}

$invertedDependencies = $data['dependencies'];
$deletedFiles = array_fill_keys(array_keys($invertedDependencies), true);
$filesToAnalyse = [];
$invertedDependenciesToReturn = [];
$errors = $data['errors'];
$filteredErrors = [];
$newFileAppeared = false;
foreach ($allAnalysedFiles as $analysedFile) {
if (array_key_exists($analysedFile, $errors)) {
$filteredErrors[$analysedFile] = $errors[$analysedFile];
}
if (!array_key_exists($analysedFile, $invertedDependencies)) {
// new file
$filesToAnalyse[] = $analysedFile;
$newFileAppeared = true;
continue;
}

unset($deletedFiles[$analysedFile]);

$analysedFileData = $invertedDependencies[$analysedFile];
$cachedModifiedTime = $analysedFileData['modifiedTime'];
$dependentFiles = $analysedFileData['dependentFiles'];
Expand All @@ -124,6 +129,27 @@ public function restore(array $allAnalysedFiles, bool $debug): ResultCache
}
}

foreach (array_keys($deletedFiles) as $deletedFile) {
if (!array_key_exists($deletedFile, $invertedDependencies)) {
continue;
}

$deletedFileData = $invertedDependencies[$deletedFile];
$dependentFiles = $deletedFileData['dependentFiles'];
foreach ($dependentFiles as $dependentFile) {
if (!is_file($dependentFile)) {
continue;
}
$filesToAnalyse[] = $dependentFile;
}
}

if ($newFileAppeared) {
foreach (array_keys($filteredErrors) as $fileWithError) {
$filesToAnalyse[] = $fileWithError;
}
}

return new ResultCache(array_unique($filesToAnalyse), false, $data['lastFullAnalysisTime'], $filteredErrors, $invertedDependenciesToReturn);
}

Expand Down
26 changes: 25 additions & 1 deletion tests/e2e/ResultCacheEndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
class ResultCacheEndToEndTest extends TestCase
{

public function setUp(): void
{
chdir(__DIR__ . '/PHP-Parser');
}

public function tearDown(): void
{
exec(sprintf('git -C %s reset --hard 2>&1', escapeshellarg(__DIR__ . '/PHP-Parser')), $outputLines, $exitCode);
Expand All @@ -24,7 +29,6 @@ public function tearDown(): void

public function testResultCache(): void
{
chdir(__DIR__ . '/PHP-Parser');
$this->runPhpstan(0);
$this->assertResultCache(__DIR__ . '/resultCache_1.php');

Expand Down Expand Up @@ -69,6 +73,26 @@ private function runPhpstanWithErrors(): void
$this->assertResultCache(__DIR__ . '/resultCache_2.php');
}

public function testResultCacheDeleteFile(): void
{
$this->runPhpstan(0);
$this->assertResultCache(__DIR__ . '/resultCache_1.php');

$serializerPath = __DIR__ . '/PHP-Parser/lib/PhpParser/Serializer.php';
$serializerCode = FileReader::read($serializerPath);
$originalSerializerCode = $serializerCode;
unlink($serializerPath);

$result = $this->runPhpstan(1);
$this->assertSame(1, $result['totals']['file_errors']);
$this->assertSame(0, $result['totals']['errors']);
$this->assertSame('Reflection error: PhpParser\Serializer not found.', $result['files'][__DIR__ . '/PHP-Parser/lib/PhpParser/Serializer/XML.php']['messages'][0]['message']);

file_put_contents($serializerPath, $originalSerializerCode);
$this->runPhpstan(0);
$this->assertResultCache(__DIR__ . '/resultCache_1.php');
}

/**
* @param int $expectedExitCode
* @return mixed[]
Expand Down

0 comments on commit b13181e

Please sign in to comment.