diff --git a/UPGRADING.md b/UPGRADING.md index a45a0257b67..4c411468d82 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -27,6 +27,9 @@ - [BC] TPositiveInt has been removed and replaced by TIntRange +- [BC] Property `Psalm\Config::$cache_directory` is now internal. Use + `Psalm\Config::getCacheDirectory()` instead. + - [BC] The parameter `$php_version` of `Psalm\Type\Atomic::create()` renamed to `$analysis_php_version_id` and changed from `array|null` to `int|null`. Previously it accepted PHP version as `array{major_version, minor_version}` diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index 3afb3f4b99c..ee172a4300a 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -204,10 +204,14 @@ class Config /** * The directory to store PHP Parser (and other) caches * + * @internal + * * @var string|null */ public $cache_directory; + private bool $cache_directory_initialized = false; + /** * The directory to store all Psalm project caches * @@ -1094,32 +1098,6 @@ private static function fromXmlAndPaths( $config->cache_directory .= DIRECTORY_SEPARATOR . sha1($base_dir); - $cwd = null; - - if ($config->resolve_from_config_file) { - $cwd = getcwd(); - chdir($config->base_dir); - } - - if (!is_dir($config->cache_directory)) { - try { - if (mkdir($config->cache_directory, 0777, true) === false) { - // any other error than directory already exists/permissions issue - throw new RuntimeException('Failed to create Psalm cache directory for unknown reasons'); - } - } catch (RuntimeException $e) { - if (!is_dir($config->cache_directory)) { - // rethrow the error with default message - // it contains the reason why creation failed - throw $e; - } - } - } - - if ($cwd) { - chdir($cwd); - } - if (isset($config_xml['serializer'])) { $attribute_text = (string) $config_xml['serializer']; $config->use_igbinary = $attribute_text === 'igbinary'; @@ -2221,6 +2199,44 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null): public function getCacheDirectory(): ?string { + if ($this->cache_directory === null) { + return null; + } + + if ($this->cache_directory_initialized) { + return $this->cache_directory; + } + + $cwd = null; + + if ($this->resolve_from_config_file) { + $cwd = getcwd(); + chdir($this->base_dir); + } + + try { + if (!is_dir($this->cache_directory)) { + try { + if (mkdir($this->cache_directory, 0777, true) === false) { + // any other error than directory already exists/permissions issue + throw new RuntimeException('Failed to create Psalm cache directory for unknown reasons'); + } + } catch (RuntimeException $e) { + if (!is_dir($this->cache_directory)) { + // rethrow the error with default message + // it contains the reason why creation failed + throw $e; + } + } + } + } finally { + if ($cwd) { + chdir($cwd); + } + } + + $this->cache_directory_initialized = true; + return $this->cache_directory; } @@ -2457,7 +2473,9 @@ public static function removeCacheDirectory(string $dir): void public function setServerMode(): void { - $this->cache_directory .= '-s'; + if ($this->cache_directory !== null) { + $this->cache_directory .= '-s'; + } } public function addStubFile(string $stub_file): void diff --git a/src/Psalm/Internal/Cli/Psalm.php b/src/Psalm/Internal/Cli/Psalm.php index 3731ba6e912..b9f754d7ffe 100644 --- a/src/Psalm/Internal/Cli/Psalm.php +++ b/src/Psalm/Internal/Cli/Psalm.php @@ -241,6 +241,10 @@ public static function run(array $argv): void $options ); + if (isset($options['no-cache'])) { + $config->cache_directory = null; + } + $config->setIncludeCollector($include_collector); $in_ci = CliUtils::runningInCI(); // disable progressbar on CI diff --git a/src/Psalm/Internal/Cli/Psalter.php b/src/Psalm/Internal/Cli/Psalter.php index a8b66e5cd62..7cb9ac925b2 100644 --- a/src/Psalm/Internal/Cli/Psalter.php +++ b/src/Psalm/Internal/Cli/Psalter.php @@ -233,6 +233,11 @@ public static function run(array $argv): void Report::TYPE_CONSOLE, $first_autoloader ); + + if (isset($options['no-cache'])) { + $config->cache_directory = null; + } + $config->setIncludeCollector($include_collector); if ($config->resolve_from_config_file) {