Skip to content

Commit

Permalink
Merge pull request #7082 from dvz/fix-parser-cache-maintenance
Browse files Browse the repository at this point in the history
Fix parser cache files maintenance
  • Loading branch information
orklah committed Dec 11, 2021
2 parents 6632ddf + 348c62d commit b663841
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 83 deletions.
28 changes: 2 additions & 26 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -684,18 +684,12 @@ public function check(string $base_dir, bool $is_diff = false): void
true
);

if ($this->project_cache_provider && $this->parser_cache_provider) {
$removed_parser_files = $this->parser_cache_provider->deleteOldParserCaches(
$is_diff ? $this->project_cache_provider->getLastRun(PSALM_VERSION) : $start_checks
);
if ($this->parser_cache_provider && !$is_diff) {
$removed_parser_files = $this->parser_cache_provider->deleteOldParserCaches($start_checks);

if ($removed_parser_files) {
$this->progress->debug('Removed ' . $removed_parser_files . ' old parser caches' . "\n");
}

if ($is_diff) {
$this->parser_cache_provider->touchParserCaches($this->getAllFiles($this->config), $start_checks);
}
}
}

Expand Down Expand Up @@ -1072,24 +1066,6 @@ private function checkDirWithConfig(string $dir_name, Config $config, bool $allo
$this->codebase->addFilesToAnalyze($files_to_scan);
}

/**
* @return list<string>
*/
private function getAllFiles(Config $config): array
{
$file_extensions = $config->getFileExtensions();
$file_paths = [];

foreach ($config->getProjectDirectories() as $dir_name) {
$file_paths = array_merge(
$file_paths,
$this->file_provider->getFilesInDir($dir_name, $file_extensions)
);
}

return $file_paths;
}

public function addProjectFile(string $file_path): void
{
$this->project_files[$file_path] = $file_path;
Expand Down
54 changes: 1 addition & 53 deletions src/Psalm/Internal/Provider/ParserCacheProvider.php
Expand Up @@ -7,7 +7,6 @@
use RuntimeException;

use function error_log;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function filemtime;
Expand Down Expand Up @@ -321,7 +320,7 @@ public function deleteOldParserCaches(float $time_before): int
{
$cache_directory = Config::getInstance()->getCacheDirectory();

if ($cache_directory) {
if (!$cache_directory) {
return 0;
}

Expand Down Expand Up @@ -349,57 +348,6 @@ public function deleteOldParserCaches(float $time_before): int
return $removed_count;
}

public function processSuccessfulRun(): void
{
$cache_directory = Config::getInstance()->getCacheDirectory();

if (!$cache_directory) {
return;
}

$cache_directory .= DIRECTORY_SEPARATOR . self::PARSER_CACHE_DIRECTORY;

if (is_dir($cache_directory)) {
$directory_files = scandir($cache_directory, SCANDIR_SORT_NONE);

foreach ($directory_files as $directory_file) {
$full_path = $cache_directory . DIRECTORY_SEPARATOR . $directory_file;

if ($directory_file[0] === '.') {
continue;
}

touch($full_path);
}
}
}

/**
* @param array<string> $file_names
*/
public function touchParserCaches(array $file_names, int $min_time): void
{
$cache_directory = Config::getInstance()->getCacheDirectory();

if (!$cache_directory) {
return;
}

$cache_directory .= DIRECTORY_SEPARATOR . self::PARSER_CACHE_DIRECTORY;

if (is_dir($cache_directory)) {
foreach ($file_names as $file_name) {
$hash_file_name = $cache_directory . DIRECTORY_SEPARATOR . $this->getParserCacheKey($file_name);

if (file_exists($hash_file_name)) {
if (filemtime($hash_file_name) < $min_time) {
touch($hash_file_name, $min_time);
}
}
}
}
}

private function getParserCacheKey(string $file_name): string
{
return md5($file_name) . ($this->use_igbinary ? '-igbinary' : '') . '-r';
Expand Down
4 changes: 0 additions & 4 deletions src/Psalm/IssueBuffer.php
Expand Up @@ -746,10 +746,6 @@ function (IssueData $d1, IssueData $d2): int {
if ($project_analyzer->project_cache_provider) {
$project_analyzer->project_cache_provider->processSuccessfulRun($start_time, PSALM_VERSION);
}

if ($codebase->statements_provider->parser_cache_provider) {
$codebase->statements_provider->parser_cache_provider->processSuccessfulRun();
}
}

if ($error_count
Expand Down

0 comments on commit b663841

Please sign in to comment.