From 0db819510dc6f9919c44f7917f0e41dd49462b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 13:46:00 +0200 Subject: [PATCH 01/13] feat: centralized Cache management --- src/Psalm/Config.php | 8 +++ src/Psalm/Internal/Cache.php | 69 +++++++++++++++++++ .../Provider/FileStorageCacheProvider.php | 53 +++----------- 3 files changed, 88 insertions(+), 42 deletions(-) create mode 100644 src/Psalm/Internal/Cache.php diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index fcc1fbeadca..e1237fbc3c6 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -68,6 +68,7 @@ use function file_get_contents; use function flock; use function fopen; +use function function_exists; use function get_class; use function get_defined_constants; use function get_defined_functions; @@ -331,6 +332,9 @@ class Config /** @var bool */ public $use_igbinary = false; + /** @var bool */ + public $use_gzip = true; + /** * @var bool */ @@ -1192,6 +1196,10 @@ private static function fromXmlAndPaths( $config->use_igbinary = version_compare($igbinary_version, '2.0.5') >= 0; } + if ($config->use_gzip) { + $config->use_gzip = function_exists('gzinflate'); + } + if (!isset($config_xml['findUnusedBaselineEntry'])) { $config->config_warnings[] = '"findUnusedBaselineEntry" will be defaulted to "true" in Psalm 6.' . ' You should explicitly enable or disable this setting.'; diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php new file mode 100644 index 00000000000..234e78ba1fe --- /dev/null +++ b/src/Psalm/Internal/Cache.php @@ -0,0 +1,69 @@ +config = $config; + $this->use_igbinary = $config->use_igbinary; + } + + public function getItem(string $path): ?object + { + if (!file_exists($path)) { + return null; + } + + $cache = Providers::safeFileGetContents($path); + if ($this->config->use_igbinary) { + /** @var object|false $unserialized */ + $unserialized = igbinary_unserialize($cache); + } else { + /** @var object|false $unserialized */ + $unserialized = @unserialize($cache); + } + + return $unserialized !== false ? $unserialized : null; + } + + public function deleteItem(string $path): void + { + if (file_exists($path)) { + unlink($path); + } + } + + public function saveItem(string $path, object $item): void + { + if ($this->config->use_igbinary) { + file_put_contents($path, igbinary_serialize($item), LOCK_EX); + } else { + file_put_contents($path, serialize($item), LOCK_EX); + } + } + + public function getCacheDirectory(): ?string + { + return $this->config->getCacheDirectory(); + } +} diff --git a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php index 10f2ef1476e..6bca718c0a1 100644 --- a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php @@ -3,6 +3,7 @@ namespace Psalm\Internal\Provider; use Psalm\Config; +use Psalm\Internal\Cache; use Psalm\Storage\FileStorage; use RuntimeException; use UnexpectedValueException; @@ -10,21 +11,14 @@ use function array_merge; use function dirname; use function file_exists; -use function file_put_contents; use function filemtime; use function get_class; use function hash; -use function igbinary_serialize; -use function igbinary_unserialize; use function is_dir; use function mkdir; -use function serialize; use function strtolower; -use function unlink; -use function unserialize; use const DIRECTORY_SEPARATOR; -use const LOCK_EX; use const PHP_VERSION_ID; /** @@ -34,13 +28,13 @@ class FileStorageCacheProvider { private string $modified_timestamps = ''; - private Config $config; + private Cache $cache; private const FILE_STORAGE_CACHE_DIRECTORY = 'file_cache'; public function __construct(Config $config) { - $this->config = $config; + $this->cache = new Cache($config); $storage_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'Storage' . DIRECTORY_SEPARATOR; @@ -64,7 +58,7 @@ public function __construct(Config $config) $this->modified_timestamps .= ' ' . filemtime($dependent_file_path); } - $this->modified_timestamps .= $this->config->computeHash(); + $this->modified_timestamps .= $config->computeHash(); } public function writeToCache(FileStorage $storage, string $file_contents): void @@ -73,11 +67,7 @@ public function writeToCache(FileStorage $storage, string $file_contents): void $cache_location = $this->getCacheLocationForPath($file_path, true); $storage->hash = $this->getCacheHash($file_path, $file_contents); - if ($this->config->use_igbinary) { - file_put_contents($cache_location, igbinary_serialize($storage), LOCK_EX); - } else { - file_put_contents($cache_location, serialize($storage), LOCK_EX); - } + $this->cache->saveItem($cache_location, $storage); } public function getLatestFromCache(string $file_path, string $file_contents): ?FileStorage @@ -105,11 +95,7 @@ public function getLatestFromCache(string $file_path, string $file_contents): ?F public function removeCacheForFile(string $file_path): void { - $cache_path = $this->getCacheLocationForPath($file_path); - - if (file_exists($cache_path)) { - unlink($cache_path); - } + $this->cache->deleteItem($this->getCacheLocationForPath($file_path)); } private function getCacheHash(string $_unused_file_path, string $file_contents): string @@ -126,26 +112,9 @@ private function getCacheHash(string $_unused_file_path, string $file_contents): */ private function loadFromCache(string $file_path): ?FileStorage { - $cache_location = $this->getCacheLocationForPath($file_path); - - if (file_exists($cache_location)) { - if ($this->config->use_igbinary) { - $storage = igbinary_unserialize(Providers::safeFileGetContents($cache_location)); - - if ($storage instanceof FileStorage) { - return $storage; - } - - return null; - } - - $storage = unserialize(Providers::safeFileGetContents($cache_location)); - - if ($storage instanceof FileStorage) { - return $storage; - } - - return null; + $cache = $this->cache->getItem($this->getCacheLocationForPath($file_path)); + if ($cache instanceof FileStorage) { + return $cache; } return null; @@ -153,7 +122,7 @@ private function loadFromCache(string $file_path): ?FileStorage private function getCacheLocationForPath(string $file_path, bool $create_directory = false): string { - $root_cache_directory = $this->config->getCacheDirectory(); + $root_cache_directory = $this->cache->getCacheDirectory(); if (!$root_cache_directory) { throw new UnexpectedValueException('No cache directory defined'); @@ -188,6 +157,6 @@ private function getCacheLocationForPath(string $file_path, bool $create_directo return $parser_cache_directory . DIRECTORY_SEPARATOR . $hash - . ($this->config->use_igbinary ? '-igbinary' : ''); + . ($this->cache->use_igbinary ? '-igbinary' : ''); } } From bd8313a46cbeac2119e4bd124a577a2fb8312bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 13:48:09 +0200 Subject: [PATCH 02/13] fix: PHP 7 compat --- src/Psalm/Internal/Cache.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index 234e78ba1fe..12ecec39d4c 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -20,7 +20,8 @@ class Cache { private Config $config; - public bool $use_igbinary; + /** @var bool */ + public $use_igbinary; public function __construct(Config $config) { From dd5c3ac241bf127073eef8404dbaf5b373189309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 14:32:56 +0200 Subject: [PATCH 03/13] feat: Gzip support --- src/Psalm/Internal/Cache.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index 12ecec39d4c..f7dfc772f13 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -36,6 +36,13 @@ public function getItem(string $path): ?object } $cache = Providers::safeFileGetContents($path); + if ($this->config->use_gzip) { + $inflated = @gzinflate($cache); + if ($inflated !== false) { + $cache = $inflated; + } + } + if ($this->config->use_igbinary) { /** @var object|false $unserialized */ $unserialized = igbinary_unserialize($cache); @@ -57,10 +64,16 @@ public function deleteItem(string $path): void public function saveItem(string $path, object $item): void { if ($this->config->use_igbinary) { - file_put_contents($path, igbinary_serialize($item), LOCK_EX); + $serialized = igbinary_serialize($item); } else { - file_put_contents($path, serialize($item), LOCK_EX); + $serialized = serialize($item); } + + if ($this->config->use_gzip) { + $serialized = gzdeflate($serialized); + } + + file_put_contents($path, $serialized, LOCK_EX); } public function getCacheDirectory(): ?string From 3e95ecd71360c56e37607e37425a164a833a1a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 14:44:29 +0200 Subject: [PATCH 04/13] feat: Gzip support in ClassStorage cache --- .../ClassLikeStorageCacheProvider.php | 40 +++++-------------- .../Provider/FileStorageCacheProvider.php | 6 +-- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php index 7b03bb3e4e6..38d4e083d2f 100644 --- a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php @@ -3,6 +3,7 @@ namespace Psalm\Internal\Provider; use Psalm\Config; +use Psalm\Internal\Cache; use Psalm\Storage\ClassLikeStorage; use RuntimeException; use UnexpectedValueException; @@ -33,7 +34,7 @@ */ class ClassLikeStorageCacheProvider { - private Config $config; + private Cache $cache; private string $modified_timestamps = ''; @@ -41,7 +42,7 @@ class ClassLikeStorageCacheProvider public function __construct(Config $config) { - $this->config = $config; + $this->cache = new Cache($config); $storage_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'Storage' . DIRECTORY_SEPARATOR; @@ -64,7 +65,7 @@ public function __construct(Config $config) $this->modified_timestamps .= ' ' . filemtime($dependent_file_path); } - $this->modified_timestamps .= $this->config->computeHash(); + $this->modified_timestamps .= $config->computeHash(); } public function writeToCache(ClassLikeStorage $storage, string $file_path, string $file_contents): void @@ -80,11 +81,7 @@ public function writeToCache(ClassLikeStorage $storage, string $file_path, strin } $cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true); - if ($this->config->use_igbinary) { - file_put_contents($cache_location, igbinary_serialize($storage), LOCK_EX); - } else { - file_put_contents($cache_location, serialize($storage), LOCK_EX); - } + $this->cache->saveItem($cache_location, $storage); } public function getLatestFromCache( @@ -123,26 +120,9 @@ private function getCacheHash(?string $_unused_file_path, ?string $file_contents */ private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path): ?ClassLikeStorage { - $cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path); - - if (file_exists($cache_location)) { - if ($this->config->use_igbinary) { - $storage = igbinary_unserialize(Providers::safeFileGetContents($cache_location)); - - if ($storage instanceof ClassLikeStorage) { - return $storage; - } - - return null; - } - - $storage = unserialize(Providers::safeFileGetContents($cache_location)); - - if ($storage instanceof ClassLikeStorage) { - return $storage; - } - - return null; + $storage = $this->cache->getItem($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path)); + if ($storage instanceof ClassLikeStorage) { + return $storage; } return null; @@ -153,7 +133,7 @@ private function getCacheLocationForClass( ?string $file_path, bool $create_directory = false ): string { - $root_cache_directory = $this->config->getCacheDirectory(); + $root_cache_directory = $this->cache->getCacheDirectory(); if (!$root_cache_directory) { throw new UnexpectedValueException('No cache directory defined'); @@ -186,6 +166,6 @@ private function getCacheLocationForClass( return $parser_cache_directory . DIRECTORY_SEPARATOR . $file_path_sha - . ($this->config->use_igbinary ? '-igbinary' : ''); + . ($this->cache->use_igbinary ? '-igbinary' : ''); } } diff --git a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php index 6bca718c0a1..79065d77f69 100644 --- a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php @@ -112,9 +112,9 @@ private function getCacheHash(string $_unused_file_path, string $file_contents): */ private function loadFromCache(string $file_path): ?FileStorage { - $cache = $this->cache->getItem($this->getCacheLocationForPath($file_path)); - if ($cache instanceof FileStorage) { - return $cache; + $storage = $this->cache->getItem($this->getCacheLocationForPath($file_path)); + if ($storage instanceof FileStorage) { + return $storage; } return null; From dc3e0455de647f98f4ef9a177fcc1a3c1b22dd5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 14:58:27 +0200 Subject: [PATCH 05/13] feat: Gzip support in PHP parser cache --- src/Psalm/Internal/Cache.php | 12 +++++- .../Internal/Provider/ParserCacheProvider.php | 37 +++++++------------ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index f7dfc772f13..6248c9f3533 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -29,7 +29,12 @@ public function __construct(Config $config) $this->use_igbinary = $config->use_igbinary; } - public function getItem(string $path): ?object + /** + * @param string $path + * + * @return array|object|null + */ + public function getItem(string $path) { if (!file_exists($path)) { return null; @@ -61,7 +66,10 @@ public function deleteItem(string $path): void } } - public function saveItem(string $path, object $item): void + /** + * @param array|object $item + */ + public function saveItem(string $path, $item): void { if ($this->config->use_igbinary) { $serialized = igbinary_serialize($item); diff --git a/src/Psalm/Internal/Provider/ParserCacheProvider.php b/src/Psalm/Internal/Provider/ParserCacheProvider.php index 17aa7e5e2a0..01912097346 100644 --- a/src/Psalm/Internal/Provider/ParserCacheProvider.php +++ b/src/Psalm/Internal/Provider/ParserCacheProvider.php @@ -6,6 +6,7 @@ use PhpParser; use PhpParser\Node\Stmt; use Psalm\Config; +use Psalm\Internal\Cache; use RuntimeException; use UnexpectedValueException; @@ -45,7 +46,7 @@ class ParserCacheProvider private const PARSER_CACHE_DIRECTORY = 'php-parser'; private const FILE_CONTENTS_CACHE_DIRECTORY = 'file-caches'; - private Config $config; + private Cache $cache; /** * A map of filename hashes to contents hashes @@ -65,7 +66,7 @@ class ParserCacheProvider public function __construct(Config $config, bool $use_file_cache = true) { - $this->config = $config; + $this->cache = new Cache($config); $this->use_file_cache = $use_file_cache; } @@ -92,13 +93,8 @@ public function loadStatementsFromCache( && is_readable($cache_location) && filemtime($cache_location) > $file_modified_time ) { - if ($this->config->use_igbinary) { - /** @var list */ - $stmts = igbinary_unserialize(Providers::safeFileGetContents($cache_location)); - } else { - /** @var list */ - $stmts = unserialize(Providers::safeFileGetContents($cache_location)); - } + /** @var list $stmts */ + $stmts = $this->cache->getItem($cache_location); return $stmts; } @@ -118,13 +114,10 @@ public function loadExistingStatementsFromCache(string $file_path): ?array $cache_location = $this->getCacheLocationForPath($file_path, self::PARSER_CACHE_DIRECTORY); if (is_readable($cache_location)) { - if ($this->config->use_igbinary) { - /** @var list */ - return igbinary_unserialize(Providers::safeFileGetContents($cache_location)) ?: null; - } + /** @var list $stmts */ + $stmts = $this->cache->getItem($cache_location); - /** @var list */ - return unserialize(Providers::safeFileGetContents($cache_location)) ?: null; + return $stmts; } return null; @@ -155,7 +148,7 @@ private function getExistingFileContentHashes(): array } if ($this->existing_file_content_hashes === null) { - $root_cache_directory = $this->config->getCacheDirectory(); + $root_cache_directory = $this->cache->getCacheDirectory(); $file_hashes_path = $root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_HASHES; if (!$root_cache_directory) { @@ -234,11 +227,7 @@ public function saveStatementsToCache( if ($touch_only) { touch($cache_location); } else { - if ($this->config->use_igbinary) { - file_put_contents($cache_location, igbinary_serialize($stmts), LOCK_EX); - } else { - file_put_contents($cache_location, serialize($stmts), LOCK_EX); - } + $this->cache->saveItem($cache_location, $stmts); $file_cache_key = $this->getParserCacheKey($file_path); $this->new_file_content_hashes[$file_cache_key] = $file_content_hash; @@ -267,7 +256,7 @@ public function saveFileContentHashes(): void return; } - $root_cache_directory = $this->config->getCacheDirectory(); + $root_cache_directory = $this->cache->getCacheDirectory(); if (!$root_cache_directory) { return; @@ -346,7 +335,7 @@ private function getParserCacheKey(string $file_path): string $hash = hash('md4', $file_path); } - return $hash . ($this->config->use_igbinary ? '-igbinary' : '') . '-r'; + return $hash . ($this->cache->use_igbinary ? '-igbinary' : '') . '-r'; } @@ -355,7 +344,7 @@ private function getCacheLocationForPath( string $subdirectory, bool $create_directory = false ): string { - $root_cache_directory = $this->config->getCacheDirectory(); + $root_cache_directory = $this->cache->getCacheDirectory(); if (!$root_cache_directory) { throw new UnexpectedValueException('No cache directory defined'); From 5ffaa117c82192e9f602dfd9b6c2a3deb50c2f91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 14:59:49 +0200 Subject: [PATCH 06/13] cs --- src/Psalm/Internal/Cache.php | 7 +++---- .../Internal/Provider/ClassLikeStorageCacheProvider.php | 6 ------ src/Psalm/Internal/Provider/ParserCacheProvider.php | 4 ---- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index 6248c9f3533..809ba653aef 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -7,6 +7,8 @@ use function file_exists; use function file_put_contents; +use function gzdeflate; +use function gzinflate; use function serialize; use function unlink; use function unserialize; @@ -20,8 +22,7 @@ class Cache { private Config $config; - /** @var bool */ - public $use_igbinary; + public bool $use_igbinary; public function __construct(Config $config) { @@ -30,8 +31,6 @@ public function __construct(Config $config) } /** - * @param string $path - * * @return array|object|null */ public function getItem(string $path) diff --git a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php index 38d4e083d2f..ca28ff11d9e 100644 --- a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php @@ -11,22 +11,16 @@ use function array_merge; use function dirname; use function file_exists; -use function file_put_contents; use function filemtime; use function get_class; use function hash; -use function igbinary_serialize; -use function igbinary_unserialize; use function is_dir; use function is_null; use function mkdir; -use function serialize; use function strtolower; use function unlink; -use function unserialize; use const DIRECTORY_SEPARATOR; -use const LOCK_EX; use const PHP_VERSION_ID; /** diff --git a/src/Psalm/Internal/Provider/ParserCacheProvider.php b/src/Psalm/Internal/Provider/ParserCacheProvider.php index 01912097346..90a388497b0 100644 --- a/src/Psalm/Internal/Provider/ParserCacheProvider.php +++ b/src/Psalm/Internal/Provider/ParserCacheProvider.php @@ -16,8 +16,6 @@ use function filemtime; use function gettype; use function hash; -use function igbinary_serialize; -use function igbinary_unserialize; use function is_array; use function is_dir; use function is_readable; @@ -26,10 +24,8 @@ use function json_encode; use function mkdir; use function scandir; -use function serialize; use function touch; use function unlink; -use function unserialize; use const DIRECTORY_SEPARATOR; use const JSON_THROW_ON_ERROR; From d4f384e13f7bb5f6f856fac6905bc1e5dcd5abf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 15:48:56 +0200 Subject: [PATCH 07/13] cs --- .../Provider/FileReferenceCacheProvider.php | 765 ++---------------- 1 file changed, 72 insertions(+), 693 deletions(-) diff --git a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php index fde0770246c..76942328bfd 100644 --- a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php @@ -3,6 +3,7 @@ namespace Psalm\Internal\Provider; use Psalm\Config; +use Psalm\Internal\Cache; use Psalm\Internal\Codebase\Analyzer; use Psalm\Internal\Provider\Providers; use RuntimeException; @@ -10,14 +11,10 @@ use function file_exists; use function file_put_contents; -use function igbinary_serialize; -use function igbinary_unserialize; use function is_array; use function is_dir; use function is_readable; use function mkdir; -use function serialize; -use function unserialize; use const DIRECTORY_SEPARATOR; use const LOCK_EX; @@ -53,10 +50,12 @@ class FileReferenceCacheProvider private const METHOD_PARAM_USE_CACHE_NAME = 'method_param_uses'; protected Config $config; + protected Cache $cache; public function __construct(Config $config) { $this->config = $config; + $this->cache = new Cache($config); } public function hasConfigChanged(): bool @@ -70,29 +69,7 @@ public function hasConfigChanged(): bool */ public function getCachedFileReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME; - - if (!is_readable($reference_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $reference_cache = igbinary_unserialize(Providers::safeFileGetContents($reference_cache_location)); - } else { - $reference_cache = unserialize(Providers::safeFileGetContents($reference_cache_location)); - } - - if (!is_array($reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $reference_cache; + return $this->getCacheItem(self::REFERENCE_CACHE_NAME); } /** @@ -100,29 +77,7 @@ public function getCachedFileReferences(): ?array */ public function getCachedClassLikeFiles(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASSLIKE_FILE_CACHE_NAME; - - if (!is_readable($reference_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $reference_cache = igbinary_unserialize(Providers::safeFileGetContents($reference_cache_location)); - } else { - $reference_cache = unserialize(Providers::safeFileGetContents($reference_cache_location)); - } - - if (!is_array($reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $reference_cache; + return $this->getCacheItem(self::CLASSLIKE_FILE_CACHE_NAME); } /** @@ -130,29 +85,7 @@ public function getCachedClassLikeFiles(): ?array */ public function getCachedNonMethodClassReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::NONMETHOD_CLASS_REFERENCE_CACHE_NAME; - - if (!is_readable($reference_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $reference_cache = igbinary_unserialize(Providers::safeFileGetContents($reference_cache_location)); - } else { - $reference_cache = unserialize(Providers::safeFileGetContents($reference_cache_location)); - } - - if (!is_array($reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $reference_cache; + return $this->getCacheItem(self::NONMETHOD_CLASS_REFERENCE_CACHE_NAME); } /** @@ -160,29 +93,7 @@ public function getCachedNonMethodClassReferences(): ?array */ public function getCachedMethodClassReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_CLASS_REFERENCE_CACHE_NAME; - - if (!is_readable($reference_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $reference_cache = igbinary_unserialize(Providers::safeFileGetContents($reference_cache_location)); - } else { - $reference_cache = unserialize(Providers::safeFileGetContents($reference_cache_location)); - } - - if (!is_array($reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $reference_cache; + return $this->getCacheItem(self::METHOD_CLASS_REFERENCE_CACHE_NAME); } /** @@ -190,30 +101,7 @@ public function getCachedMethodClassReferences(): ?array */ public function getCachedMethodMemberReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $class_member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_METHOD_CACHE_NAME; - - if (!is_readable($class_member_cache_location)) { - return null; - } - - $class_member_reference_cache = Providers::safeFileGetContents($class_member_cache_location); - if ($this->config->use_igbinary) { - $class_member_reference_cache = igbinary_unserialize($class_member_reference_cache); - } else { - $class_member_reference_cache = unserialize($class_member_reference_cache); - } - - if (!is_array($class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $class_member_reference_cache; + return $this->getCacheItem(self::CLASS_METHOD_CACHE_NAME); } /** @@ -221,31 +109,7 @@ public function getCachedMethodMemberReferences(): ?array */ public function getCachedMethodDependencies(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $method_dependencies_cache_location - = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_DEPENDENCIES_CACHE_NAME; - - if (!is_readable($method_dependencies_cache_location)) { - return null; - } - - $method_dependencies_cache = Providers::safeFileGetContents($method_dependencies_cache_location); - if ($this->config->use_igbinary) { - $method_dependencies_cache = igbinary_unserialize($method_dependencies_cache); - } else { - $method_dependencies_cache = unserialize($method_dependencies_cache); - } - - if (!is_array($method_dependencies_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $method_dependencies_cache; + return $this->getCacheItem(self::METHOD_DEPENDENCIES_CACHE_NAME); } /** @@ -253,30 +117,7 @@ public function getCachedMethodDependencies(): ?array */ public function getCachedMethodPropertyReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $class_member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_PROPERTY_CACHE_NAME; - - if (!is_readable($class_member_cache_location)) { - return null; - } - - $class_member_reference_cache = Providers::safeFileGetContents($class_member_cache_location); - if ($this->config->use_igbinary) { - $class_member_reference_cache = igbinary_unserialize($class_member_reference_cache); - } else { - $class_member_reference_cache = unserialize($class_member_reference_cache); - } - - if (!is_array($class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $class_member_reference_cache; + return $this->getCacheItem(self::CLASS_PROPERTY_CACHE_NAME); } /** @@ -284,30 +125,7 @@ public function getCachedMethodPropertyReferences(): ?array */ public function getCachedMethodMethodReturnReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $class_member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_METHOD_RETURN_CACHE_NAME; - - if (!is_readable($class_member_cache_location)) { - return null; - } - - $class_member_reference_cache = Providers::safeFileGetContents($class_member_cache_location); - if ($this->config->use_igbinary) { - $class_member_reference_cache = igbinary_unserialize($class_member_reference_cache); - } else { - $class_member_reference_cache = unserialize($class_member_reference_cache); - } - - if (!is_array($class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $class_member_reference_cache; + return $this->getCacheItem(self::CLASS_METHOD_RETURN_CACHE_NAME); } /** @@ -315,30 +133,7 @@ public function getCachedMethodMethodReturnReferences(): ?array */ public function getCachedMethodMissingMemberReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $class_member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_MISSING_MEMBER_CACHE_NAME; - - if (!is_readable($class_member_cache_location)) { - return null; - } - - $class_member_reference_cache = Providers::safeFileGetContents($class_member_cache_location); - if ($this->config->use_igbinary) { - $class_member_reference_cache = igbinary_unserialize($class_member_reference_cache); - } else { - $class_member_reference_cache = unserialize($class_member_reference_cache); - } - - if (!is_array($class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $class_member_reference_cache; + return $this->getCacheItem(self::METHOD_MISSING_MEMBER_CACHE_NAME); } /** @@ -346,30 +141,7 @@ public function getCachedMethodMissingMemberReferences(): ?array */ public function getCachedFileMemberReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $file_class_member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_CLASS_MEMBER_CACHE_NAME; - - if (!is_readable($file_class_member_cache_location)) { - return null; - } - - $file_class_member_reference_cache = Providers::safeFileGetContents($file_class_member_cache_location); - if ($this->config->use_igbinary) { - $file_class_member_reference_cache = igbinary_unserialize($file_class_member_reference_cache); - } else { - $file_class_member_reference_cache = unserialize($file_class_member_reference_cache); - } - - if (!is_array($file_class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $file_class_member_reference_cache; + return $this->getCacheItem(self::FILE_CLASS_MEMBER_CACHE_NAME); } /** @@ -377,32 +149,7 @@ public function getCachedFileMemberReferences(): ?array */ public function getCachedFilePropertyReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $file_class_member_cache_location = $cache_directory - . DIRECTORY_SEPARATOR - . self::FILE_CLASS_PROPERTY_CACHE_NAME; - - if (!is_readable($file_class_member_cache_location)) { - return null; - } - - $file_class_member_reference_cache = Providers::safeFileGetContents($file_class_member_cache_location); - if ($this->config->use_igbinary) { - $file_class_member_reference_cache = igbinary_unserialize($file_class_member_reference_cache); - } else { - $file_class_member_reference_cache = unserialize($file_class_member_reference_cache); - } - - if (!is_array($file_class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $file_class_member_reference_cache; + return $this->getCacheItem(self::FILE_CLASS_PROPERTY_CACHE_NAME); } /** @@ -410,32 +157,7 @@ public function getCachedFilePropertyReferences(): ?array */ public function getCachedFileMethodReturnReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $file_class_member_cache_location = $cache_directory - . DIRECTORY_SEPARATOR - . self::FILE_METHOD_RETURN_CACHE_NAME; - - if (!is_readable($file_class_member_cache_location)) { - return null; - } - - $file_class_member_reference_cache = Providers::safeFileGetContents($file_class_member_cache_location); - if ($this->config->use_igbinary) { - $file_class_member_reference_cache = igbinary_unserialize($file_class_member_reference_cache); - } else { - $file_class_member_reference_cache = unserialize($file_class_member_reference_cache); - } - - if (!is_array($file_class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $file_class_member_reference_cache; + return $this->getCacheItem(self::FILE_METHOD_RETURN_CACHE_NAME); } /** @@ -443,31 +165,7 @@ public function getCachedFileMethodReturnReferences(): ?array */ public function getCachedFileMissingMemberReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $file_class_member_cache_location - = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_MISSING_MEMBER_CACHE_NAME; - - if (!is_readable($file_class_member_cache_location)) { - return null; - } - - $file_class_member_reference_cache = Providers::safeFileGetContents($file_class_member_cache_location); - if ($this->config->use_igbinary) { - $file_class_member_reference_cache = igbinary_unserialize($file_class_member_reference_cache); - } else { - $file_class_member_reference_cache = unserialize($file_class_member_reference_cache); - } - - if (!is_array($file_class_member_reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $file_class_member_reference_cache; + return $this->getCacheItem(self::FILE_MISSING_MEMBER_CACHE_NAME); } /** @@ -475,29 +173,7 @@ public function getCachedFileMissingMemberReferences(): ?array */ public function getCachedMixedMemberNameReferences(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::UNKNOWN_MEMBER_CACHE_NAME; - - if (!is_readable($reference_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $reference_cache = igbinary_unserialize(Providers::safeFileGetContents($reference_cache_location)); - } else { - $reference_cache = unserialize(Providers::safeFileGetContents($reference_cache_location)); - } - - if (!is_array($reference_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $reference_cache; + return $this->getCacheItem(self::UNKNOWN_MEMBER_CACHE_NAME); } /** @@ -505,29 +181,7 @@ public function getCachedMixedMemberNameReferences(): ?array */ public function getCachedMethodParamUses(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_PARAM_USE_CACHE_NAME; - - if (!is_readable($reference_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $reference_cache = igbinary_unserialize(Providers::safeFileGetContents($reference_cache_location)); - } else { - $reference_cache = unserialize(Providers::safeFileGetContents($reference_cache_location)); - } - - if (!is_array($reference_cache)) { - throw new UnexpectedValueException('The method param use cache must be an array'); - } - - return $reference_cache; + return $this->getCacheItem(self::METHOD_PARAM_USE_CACHE_NAME); } /** @@ -535,301 +189,87 @@ public function getCachedMethodParamUses(): ?array */ public function getCachedIssues(): ?array { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return null; - } - - $issues_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::ISSUES_CACHE_NAME; - - if (!is_readable($issues_cache_location)) { - return null; - } - - if ($this->config->use_igbinary) { - $issues_cache = igbinary_unserialize(Providers::safeFileGetContents($issues_cache_location)); - } else { - $issues_cache = unserialize(Providers::safeFileGetContents($issues_cache_location)); - } - - if (!is_array($issues_cache)) { - throw new UnexpectedValueException('The reference cache must be an array'); - } - - return $issues_cache; + return $this->getCacheItem(self::ISSUES_CACHE_NAME); } public function setCachedFileReferences(array $file_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::REFERENCE_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($reference_cache_location, igbinary_serialize($file_references), LOCK_EX); - } else { - file_put_contents($reference_cache_location, serialize($file_references), LOCK_EX); - } + $this->saveCacheItem(self::REFERENCE_CACHE_NAME, $file_references); } public function setCachedClassLikeFiles(array $file_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASSLIKE_FILE_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($reference_cache_location, igbinary_serialize($file_references), LOCK_EX); - } else { - file_put_contents($reference_cache_location, serialize($file_references), LOCK_EX); - } + $this->saveCacheItem(self::CLASSLIKE_FILE_CACHE_NAME, $file_references); } public function setCachedNonMethodClassReferences(array $file_class_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::NONMETHOD_CLASS_REFERENCE_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($reference_cache_location, igbinary_serialize($file_class_references), LOCK_EX); - } else { - file_put_contents($reference_cache_location, serialize($file_class_references), LOCK_EX); - } + $this->saveCacheItem(self::NONMETHOD_CLASS_REFERENCE_CACHE_NAME, $file_class_references); } public function setCachedMethodClassReferences(array $method_class_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_CLASS_REFERENCE_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($reference_cache_location, igbinary_serialize($method_class_references), LOCK_EX); - } else { - file_put_contents($reference_cache_location, serialize($method_class_references), LOCK_EX); - } + $this->saveCacheItem(self::METHOD_CLASS_REFERENCE_CACHE_NAME, $method_class_references); } public function setCachedMethodMemberReferences(array $member_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_METHOD_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($member_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($member_references), LOCK_EX); - } + $this->saveCacheItem(self::CLASS_METHOD_CACHE_NAME, $member_references); } public function setCachedMethodDependencies(array $member_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_DEPENDENCIES_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($member_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($member_references), LOCK_EX); - } + $this->saveCacheItem(self::METHOD_DEPENDENCIES_CACHE_NAME, $member_references); } public function setCachedMethodPropertyReferences(array $property_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_PROPERTY_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($property_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($property_references), LOCK_EX); - } + $this->saveCacheItem(self::CLASS_PROPERTY_CACHE_NAME, $property_references); } public function setCachedMethodMethodReturnReferences(array $method_return_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::CLASS_METHOD_RETURN_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($method_return_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($method_return_references), LOCK_EX); - } + $this->saveCacheItem(self::CLASS_METHOD_RETURN_CACHE_NAME, $method_return_references); } public function setCachedMethodMissingMemberReferences(array $member_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_MISSING_MEMBER_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($member_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($member_references), LOCK_EX); - } + $this->saveCacheItem(self::METHOD_MISSING_MEMBER_CACHE_NAME, $member_references); } public function setCachedFileMemberReferences(array $member_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_CLASS_MEMBER_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($member_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($member_references), LOCK_EX); - } + $this->saveCacheItem(self::FILE_CLASS_MEMBER_CACHE_NAME, $member_references); } public function setCachedFilePropertyReferences(array $property_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_CLASS_PROPERTY_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($property_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($property_references), LOCK_EX); - } + $this->saveCacheItem(self::FILE_CLASS_PROPERTY_CACHE_NAME, $property_references); } public function setCachedFileMethodReturnReferences(array $method_return_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_METHOD_RETURN_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($method_return_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($method_return_references), LOCK_EX); - } + $this->saveCacheItem(self::FILE_METHOD_RETURN_CACHE_NAME, $method_return_references); } public function setCachedFileMissingMemberReferences(array $member_references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $member_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_MISSING_MEMBER_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($member_cache_location, igbinary_serialize($member_references), LOCK_EX); - } else { - file_put_contents($member_cache_location, serialize($member_references), LOCK_EX); - } + $this->saveCacheItem(self::FILE_MISSING_MEMBER_CACHE_NAME, $member_references); } public function setCachedMixedMemberNameReferences(array $references): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::UNKNOWN_MEMBER_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($reference_cache_location, igbinary_serialize($references), LOCK_EX); - } else { - file_put_contents($reference_cache_location, serialize($references), LOCK_EX); - } + $this->saveCacheItem(self::UNKNOWN_MEMBER_CACHE_NAME, $references); } public function setCachedMethodParamUses(array $uses): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $reference_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::METHOD_PARAM_USE_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($reference_cache_location, igbinary_serialize($uses), LOCK_EX); - } else { - file_put_contents($reference_cache_location, serialize($uses), LOCK_EX); - } + $this->saveCacheItem(self::METHOD_PARAM_USE_CACHE_NAME, $uses); } public function setCachedIssues(array $issues): void { - $cache_directory = $this->config->getCacheDirectory(); - - if (!$cache_directory) { - return; - } - - $issues_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::ISSUES_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($issues_cache_location, igbinary_serialize($issues), LOCK_EX); - } else { - file_put_contents($issues_cache_location, serialize($issues), LOCK_EX); - } + $this->saveCacheItem(self::ISSUES_CACHE_NAME, $issues); } /** @@ -837,23 +277,7 @@ public function setCachedIssues(array $issues): void */ public function getAnalyzedMethodCache() { - $cache_directory = $this->config->getCacheDirectory(); - - $analyzed_methods_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::ANALYZED_METHODS_CACHE_NAME; - - if ($cache_directory - && file_exists($analyzed_methods_cache_location) - ) { - if ($this->config->use_igbinary) { - /** @var array> */ - return igbinary_unserialize(Providers::safeFileGetContents($analyzed_methods_cache_location)); - } else { - /** @var array> */ - return unserialize(Providers::safeFileGetContents($analyzed_methods_cache_location)); - } - } - - return false; + return $this->getCacheItem(self::ANALYZED_METHODS_CACHE_NAME) ?? false; } /** @@ -861,19 +285,7 @@ public function getAnalyzedMethodCache() */ public function setAnalyzedMethodCache(array $analyzed_methods): void { - $cache_directory = Config::getInstance()->getCacheDirectory(); - - if ($cache_directory) { - $analyzed_methods_cache_location = $cache_directory - . DIRECTORY_SEPARATOR - . self::ANALYZED_METHODS_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($analyzed_methods_cache_location, igbinary_serialize($analyzed_methods), LOCK_EX); - } else { - file_put_contents($analyzed_methods_cache_location, serialize($analyzed_methods), LOCK_EX); - } - } + $this->saveCacheItem(self::ANALYZED_METHODS_CACHE_NAME, $analyzed_methods); } /** @@ -881,29 +293,7 @@ public function setAnalyzedMethodCache(array $analyzed_methods): void */ public function getFileMapCache() { - $cache_directory = $this->config->getCacheDirectory(); - - $file_maps_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_MAPS_CACHE_NAME; - - if ($cache_directory - && file_exists($file_maps_cache_location) - ) { - if ($this->config->use_igbinary) { - /** - * @var array - */ - $file_maps_cache = igbinary_unserialize(Providers::safeFileGetContents($file_maps_cache_location)); - } else { - /** - * @var array - */ - $file_maps_cache = unserialize(Providers::safeFileGetContents($file_maps_cache_location)); - } - - return $file_maps_cache; - } - - return false; + return $this->getCacheItem(self::FILE_MAPS_CACHE_NAME) ?? false; } /** @@ -911,17 +301,7 @@ public function getFileMapCache() */ public function setFileMapCache(array $file_maps): void { - $cache_directory = Config::getInstance()->getCacheDirectory(); - - if ($cache_directory) { - $file_maps_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::FILE_MAPS_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($file_maps_cache_location, igbinary_serialize($file_maps), LOCK_EX); - } else { - file_put_contents($file_maps_cache_location, serialize($file_maps), LOCK_EX); - } - } + $this->saveCacheItem(self::FILE_MAPS_CACHE_NAME, $file_maps); } //phpcs:disable -- Remove this once the phpstan phpdoc parser MR is merged @@ -931,29 +311,7 @@ public function setFileMapCache(array $file_maps): void public function getTypeCoverage() { //phpcs:enable -- Remove this once the phpstan phpdoc parser MR is merged - $cache_directory = Config::getInstance()->getCacheDirectory(); - - $type_coverage_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::TYPE_COVERAGE_CACHE_NAME; - - if ($cache_directory - && file_exists($type_coverage_cache_location) - ) { - if ($this->config->use_igbinary) { - /** @var array */ - $type_coverage_cache = igbinary_unserialize( - Providers::safeFileGetContents($type_coverage_cache_location), - ); - } else { - /** @var array */ - $type_coverage_cache = unserialize( - Providers::safeFileGetContents($type_coverage_cache_location), - ); - } - - return $type_coverage_cache; - } - - return false; + return $this->getCacheItem(self::TYPE_COVERAGE_CACHE_NAME) ?? false; } /** @@ -961,17 +319,7 @@ public function getTypeCoverage() */ public function setTypeCoverage(array $mixed_counts): void { - $cache_directory = Config::getInstance()->getCacheDirectory(); - - if ($cache_directory) { - $type_coverage_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::TYPE_COVERAGE_CACHE_NAME; - - if ($this->config->use_igbinary) { - file_put_contents($type_coverage_cache_location, igbinary_serialize($mixed_counts), LOCK_EX); - } else { - file_put_contents($type_coverage_cache_location, serialize($mixed_counts), LOCK_EX); - } - } + $this->saveCacheItem(self::TYPE_COVERAGE_CACHE_NAME, $mixed_counts); } /** @@ -1030,4 +378,35 @@ public function setConfigHashCache(string $hash = ''): void LOCK_EX, ); } + + private function getCacheItem(string $type): ?array + { + $cache_directory = $this->config->getCacheDirectory(); + if (!$cache_directory) { + return null; + } + + $cache_location = $cache_directory . DIRECTORY_SEPARATOR . $type; + if (!is_readable($cache_location)) { + return null; + } + + $cache_item = $this->cache->getItem($cache_location); + if (!is_array($cache_item)) { + throw new UnexpectedValueException('The reference cache must be an array'); + } + + return $cache_item; + } + + private function saveCacheItem(string $type, array $cache_item): void + { + $cache_directory = $this->config->getCacheDirectory(); + if (!$cache_directory) { + return; + } + $cache_location = $cache_directory . DIRECTORY_SEPARATOR . $type; + + $this->cache->saveItem($cache_location, $cache_item); + } } From 495a466c5e68880daf1ba3e8f0dd08a45af578bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 15:57:29 +0200 Subject: [PATCH 08/13] init cache in child classes --- .../LanguageServer/Provider/FileReferenceCacheProvider.php | 2 +- src/Psalm/Internal/Provider/ParserCacheProvider.php | 2 +- tests/Internal/Provider/FakeFileReferenceCacheProvider.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/LanguageServer/Provider/FileReferenceCacheProvider.php b/src/Psalm/Internal/LanguageServer/Provider/FileReferenceCacheProvider.php index 79d54534448..ca12912ec0a 100644 --- a/src/Psalm/Internal/LanguageServer/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Internal/LanguageServer/Provider/FileReferenceCacheProvider.php @@ -62,7 +62,7 @@ class FileReferenceCacheProvider extends InternalFileReferenceCacheProvider public function __construct(Config $config) { - $this->config = $config; + parent::__construct($config); } public function getCachedFileReferences(): ?array diff --git a/src/Psalm/Internal/Provider/ParserCacheProvider.php b/src/Psalm/Internal/Provider/ParserCacheProvider.php index 90a388497b0..bde25ec8252 100644 --- a/src/Psalm/Internal/Provider/ParserCacheProvider.php +++ b/src/Psalm/Internal/Provider/ParserCacheProvider.php @@ -290,7 +290,7 @@ public function cacheFileContents(string $file_path, string $file_contents): voi public function deleteOldParserCaches(float $time_before): int { - $cache_directory = $this->config->getCacheDirectory(); + $cache_directory = $this->cache->getCacheDirectory(); $this->existing_file_content_hashes = null; $this->new_file_content_hashes = []; diff --git a/tests/Internal/Provider/FakeFileReferenceCacheProvider.php b/tests/Internal/Provider/FakeFileReferenceCacheProvider.php index e3893528d73..83e641e86c6 100644 --- a/tests/Internal/Provider/FakeFileReferenceCacheProvider.php +++ b/tests/Internal/Provider/FakeFileReferenceCacheProvider.php @@ -60,7 +60,7 @@ class FakeFileReferenceCacheProvider extends FileReferenceCacheProvider public function __construct() { - $this->config = Config::getInstance(); + parent::__construct(Config::getInstance()); } public function getCachedFileReferences(): ?array From 59f45480c4471c330a47dcf00914b6e302cdb3bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 16:03:25 +0200 Subject: [PATCH 09/13] feat: Gzip support in PHP parser cache --- src/Psalm/Internal/Cache.php | 4 ++-- src/Psalm/Internal/Provider/ParserCacheProvider.php | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index 809ba653aef..3a9b22081a5 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -31,7 +31,7 @@ public function __construct(Config $config) } /** - * @return array|object|null + * @return array|object|string|null */ public function getItem(string $path) { @@ -66,7 +66,7 @@ public function deleteItem(string $path): void } /** - * @param array|object $item + * @param array|object|string $item */ public function saveItem(string $path, $item): void { diff --git a/src/Psalm/Internal/Provider/ParserCacheProvider.php b/src/Psalm/Internal/Provider/ParserCacheProvider.php index bde25ec8252..e180b39fd53 100644 --- a/src/Psalm/Internal/Provider/ParserCacheProvider.php +++ b/src/Psalm/Internal/Provider/ParserCacheProvider.php @@ -127,11 +127,7 @@ public function loadExistingFileContentsFromCache(string $file_path): ?string $cache_location = $this->getCacheLocationForPath($file_path, self::FILE_CONTENTS_CACHE_DIRECTORY); - if (is_readable($cache_location)) { - return Providers::safeFileGetContents($cache_location); - } - - return null; + return $this->cache->getItem($cache_location); } /** @@ -284,8 +280,7 @@ public function cacheFileContents(string $file_path, string $file_contents): voi } $cache_location = $this->getCacheLocationForPath($file_path, self::FILE_CONTENTS_CACHE_DIRECTORY, true); - - file_put_contents($cache_location, $file_contents, LOCK_EX); + $this->cache->saveItem($cache_location, $file_contents); } public function deleteOldParserCaches(float $time_before): int From dd0402b6d75ac564400162546ab078ee087d783e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 16:07:41 +0200 Subject: [PATCH 10/13] SA: only possible return type is a string --- src/Psalm/Internal/Provider/ParserCacheProvider.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Provider/ParserCacheProvider.php b/src/Psalm/Internal/Provider/ParserCacheProvider.php index e180b39fd53..a2f19679395 100644 --- a/src/Psalm/Internal/Provider/ParserCacheProvider.php +++ b/src/Psalm/Internal/Provider/ParserCacheProvider.php @@ -127,7 +127,10 @@ public function loadExistingFileContentsFromCache(string $file_path): ?string $cache_location = $this->getCacheLocationForPath($file_path, self::FILE_CONTENTS_CACHE_DIRECTORY); - return $this->cache->getItem($cache_location); + /** @var string $cache_item */ + $cache_item = $this->cache->getItem($cache_location); + + return $cache_item; } /** From 5e49ebb8e60e66273780263ec57904b6e7924f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 16:28:43 +0200 Subject: [PATCH 11/13] SA: remove obsolete suppresses --- .../ClassLikeStorageCacheProvider.php | 3 -- .../Provider/FileReferenceCacheProvider.php | 48 ------------------- .../Provider/FileStorageCacheProvider.php | 3 -- 3 files changed, 54 deletions(-) diff --git a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php index ca28ff11d9e..8c16dc8b6b8 100644 --- a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php @@ -109,9 +109,6 @@ private function getCacheHash(?string $_unused_file_path, ?string $file_contents return PHP_VERSION_ID >= 8_01_00 ? hash('xxh128', $data) : hash('md4', $data); } - /** - * @psalm-suppress MixedAssignment - */ private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path): ?ClassLikeStorage { $storage = $this->cache->getItem($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path)); diff --git a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php index 76942328bfd..3eb90a186fd 100644 --- a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php @@ -64,129 +64,81 @@ public function hasConfigChanged(): bool return $new_hash !== $this->getConfigHashCache(); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedFileReferences(): ?array { return $this->getCacheItem(self::REFERENCE_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedClassLikeFiles(): ?array { return $this->getCacheItem(self::CLASSLIKE_FILE_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedNonMethodClassReferences(): ?array { return $this->getCacheItem(self::NONMETHOD_CLASS_REFERENCE_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodClassReferences(): ?array { return $this->getCacheItem(self::METHOD_CLASS_REFERENCE_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodMemberReferences(): ?array { return $this->getCacheItem(self::CLASS_METHOD_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodDependencies(): ?array { return $this->getCacheItem(self::METHOD_DEPENDENCIES_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodPropertyReferences(): ?array { return $this->getCacheItem(self::CLASS_PROPERTY_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodMethodReturnReferences(): ?array { return $this->getCacheItem(self::CLASS_METHOD_RETURN_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodMissingMemberReferences(): ?array { return $this->getCacheItem(self::METHOD_MISSING_MEMBER_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedFileMemberReferences(): ?array { return $this->getCacheItem(self::FILE_CLASS_MEMBER_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedFilePropertyReferences(): ?array { return $this->getCacheItem(self::FILE_CLASS_PROPERTY_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedFileMethodReturnReferences(): ?array { return $this->getCacheItem(self::FILE_METHOD_RETURN_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedFileMissingMemberReferences(): ?array { return $this->getCacheItem(self::FILE_MISSING_MEMBER_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMixedMemberNameReferences(): ?array { return $this->getCacheItem(self::UNKNOWN_MEMBER_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedMethodParamUses(): ?array { return $this->getCacheItem(self::METHOD_PARAM_USE_CACHE_NAME); } - /** - * @psalm-suppress MixedAssignment - */ public function getCachedIssues(): ?array { return $this->getCacheItem(self::ISSUES_CACHE_NAME); diff --git a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php index 79065d77f69..6117d9634e3 100644 --- a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php @@ -107,9 +107,6 @@ private function getCacheHash(string $_unused_file_path, string $file_contents): return PHP_VERSION_ID >= 8_01_00 ? hash('xxh128', $data) : hash('md4', $data); } - /** - * @psalm-suppress MixedAssignment - */ private function loadFromCache(string $file_path): ?FileStorage { $storage = $this->cache->getItem($this->getCacheLocationForPath($file_path)); From ddacfeaa3e54ee68c70dc1ecd9843f4a079a7a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 16:33:23 +0200 Subject: [PATCH 12/13] SA: add expected types --- .../Provider/FileReferenceCacheProvider.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php index 3eb90a186fd..d769dced756 100644 --- a/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileReferenceCacheProvider.php @@ -229,7 +229,10 @@ public function setCachedIssues(array $issues): void */ public function getAnalyzedMethodCache() { - return $this->getCacheItem(self::ANALYZED_METHODS_CACHE_NAME) ?? false; + /** @var null|array> $cache_item */ + $cache_item = $this->getCacheItem(self::ANALYZED_METHODS_CACHE_NAME); + + return $cache_item ?? false; } /** @@ -245,7 +248,10 @@ public function setAnalyzedMethodCache(array $analyzed_methods): void */ public function getFileMapCache() { - return $this->getCacheItem(self::FILE_MAPS_CACHE_NAME) ?? false; + /** @var array|null $cache_item */ + $cache_item = $this->getCacheItem(self::FILE_MAPS_CACHE_NAME); + + return $cache_item ?? false; } /** @@ -263,7 +269,10 @@ public function setFileMapCache(array $file_maps): void public function getTypeCoverage() { //phpcs:enable -- Remove this once the phpstan phpdoc parser MR is merged - return $this->getCacheItem(self::TYPE_COVERAGE_CACHE_NAME) ?? false; + /** @var array|null $cache_item */ + $cache_item = $this->getCacheItem(self::TYPE_COVERAGE_CACHE_NAME); + + return $cache_item ?? false; } /** From 8d1197427e87f6ecbb6331472608dc35237c677e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 9 Jun 2023 17:12:35 +0200 Subject: [PATCH 13/13] fix: import igbinary_* functions --- src/Psalm/Internal/Cache.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index 3a9b22081a5..20c9fe3bb57 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -9,6 +9,8 @@ use function file_put_contents; use function gzdeflate; use function gzinflate; +use function igbinary_serialize; +use function igbinary_unserialize; use function serialize; use function unlink; use function unserialize;