Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser cache files maintenance #7082

Merged
merged 6 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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