From 23d5974a88b96e9361b733e1f97e0b28459bb915 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Thu, 3 Nov 2016 20:39:04 -0400 Subject: [PATCH 1/2] Swap codeinspector with psalm --- examples/psalm.default.xml | 4 ++-- psalm.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/psalm.default.xml b/examples/psalm.default.xml index 42161be04..8d946caf8 100644 --- a/examples/psalm.default.xml +++ b/examples/psalm.default.xml @@ -1,5 +1,5 @@ - - + diff --git a/psalm.xml b/psalm.xml index c29dc9ae7..854d2d47b 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,5 +1,5 @@ - - + From ea4daa7b7e520c59fee21a6825af8c6b7d2e313a Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 4 Nov 2016 17:45:12 -0400 Subject: [PATCH 2/2] Move cache dir into better config --- bin/psalm | 3 -- src/Psalm/Checker/FileChecker.php | 54 +++++++++++++++---------------- src/Psalm/Config.php | 21 +++++++++++- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/bin/psalm b/bin/psalm index d19c62dfc..e51612339 100755 --- a/bin/psalm +++ b/bin/psalm @@ -51,9 +51,6 @@ $show_info = isset($options['show-info']) : true; $is_diff = isset($options['diff']); -// set the cache directory for the file checker -FileChecker::setCacheDir('/var/tmp/php-parser'); - // initialise custom config, if passed if ($path_to_config) { ProjectChecker::setConfigXML($path_to_config); diff --git a/src/Psalm/Checker/FileChecker.php b/src/Psalm/Checker/FileChecker.php index 69c4f023c..b520b952e 100644 --- a/src/Psalm/Checker/FileChecker.php +++ b/src/Psalm/Checker/FileChecker.php @@ -52,11 +52,6 @@ class FileChecker implements StatementsSource */ protected $suppressed_issues = []; - /** - * @var string|null - */ - protected static $cache_dir = null; - /** * @var array */ @@ -329,10 +324,12 @@ class FileChecker implements StatementsSource $cache_location = null; - if (self::$cache_dir) { + $cache_directory = Config::getInstance()->getCacheDirectory(); + + if ($cache_directory) { $key = md5($contents); - $cache_location = self::$cache_dir . '/' . $key; + $cache_location = $cache_directory . '/' . $key; if (is_readable($cache_location)) { $stmts = unserialize((string) file_get_contents($cache_location)); @@ -346,12 +343,12 @@ class FileChecker implements StatementsSource $stmts = $parser->parse($contents); } - if (self::$cache_dir && $cache_location) { + if ($cache_directory && $cache_location) { if ($from_cache) { touch($cache_location); } else { - if (!file_exists(self::$cache_dir)) { - mkdir(self::$cache_dir); + if (!file_exists($cache_directory)) { + mkdir($cache_directory); } file_put_contents($cache_location, serialize($stmts)); @@ -370,8 +367,10 @@ class FileChecker implements StatementsSource */ public static function loadReferenceCache() { - if (self::$cache_dir) { - $cache_location = self::$cache_dir . '/' . self::REFERENCE_CACHE_NAME; + $cache_directory = Config::getInstance()->getCacheDirectory(); + + if ($cache_directory) { + $cache_location = $cache_directory . '/' . self::REFERENCE_CACHE_NAME; if (is_readable($cache_location)) { self::$file_references = unserialize((string) file_get_contents($cache_location)); @@ -387,8 +386,10 @@ class FileChecker implements StatementsSource */ public static function updateReferenceCache() { - if (self::$cache_dir) { - $cache_location = self::$cache_dir . '/' . self::REFERENCE_CACHE_NAME; + $cache_directory = Config::getInstance()->getCacheDirectory(); + + if ($cache_directory) { + $cache_location = $cache_directory . '/' . self::REFERENCE_CACHE_NAME; foreach (self::$files_checked as $file => $_) { $all_file_references = array_unique( @@ -415,15 +416,6 @@ class FileChecker implements StatementsSource } } - /** - * @param string $cache_dir - * @return void - */ - public static function setCacheDir($cache_dir) - { - self::$cache_dir = $cache_dir; - } - /** * @return null */ @@ -676,7 +668,9 @@ class FileChecker implements StatementsSource */ public static function canDiffFiles() { - return self::$cache_dir && file_exists(self::$cache_dir . '/' . self::GOOD_RUN_NAME); + $cache_directory = Config::getInstance()->getCacheDirectory(); + + return $cache_directory && file_exists($cache_directory . '/' . self::GOOD_RUN_NAME); } /** @@ -686,7 +680,9 @@ class FileChecker implements StatementsSource public static function hasFileChanged($file) { if (self::$last_good_run === null) { - self::$last_good_run = filemtime(self::$cache_dir . '/' . self::GOOD_RUN_NAME) ?: 0; + $cache_directory = Config::getInstance()->getCacheDirectory(); + + self::$last_good_run = filemtime($cache_directory . '/' . self::GOOD_RUN_NAME) ?: 0; } return filemtime($file) > self::$last_good_run; @@ -714,8 +710,10 @@ class FileChecker implements StatementsSource */ public static function goodRun() { - if (self::$cache_dir) { - $run_cache_location = self::$cache_dir . '/' . self::GOOD_RUN_NAME; + $cache_directory = Config::getInstance()->getCacheDirectory(); + + if ($cache_directory) { + $run_cache_location = $cache_directory . '/' . self::GOOD_RUN_NAME; touch($run_cache_location); @@ -727,7 +725,7 @@ class FileChecker implements StatementsSource } file_put_contents( - self::$cache_dir . '/' . self::REFERENCE_CACHE_NAME, + $cache_directory . '/' . self::REFERENCE_CACHE_NAME, serialize(self::$file_references) ); } diff --git a/src/Psalm/Config.php b/src/Psalm/Config.php index c02ac4dc1..ca00bfa97 100644 --- a/src/Psalm/Config.php +++ b/src/Psalm/Config.php @@ -49,7 +49,14 @@ class Config public $throw_exception = false; /** - * Path to the autoloader + * The directory to store PHP Parser (and other) caches + * + * @var string + */ + public $cache_directory = '/var/tmp/psalm'; + + /** + * Path to the autoader * * @var string|null */ @@ -149,6 +156,10 @@ class Config $config->autoloader = (string) $config_xml['autoloader']; } + if (isset($config_xml['cacheDirectory'])) { + $config->cacheDirectory = (string) $config_xml['cacheDirectory']; + } + if (isset($config_xml->inspectFiles)) { $config->inspect_files = FileFilter::loadFromXML($config_xml->inspectFiles, true); } @@ -375,6 +386,14 @@ class Config return $this->mock_classes; } + /** + * @return string|null + */ + public function getCacheDirectory() + { + return $this->cache_directory; + } + /** * @return array */