diff --git a/src/Config.php b/src/Config.php index f77eaef473..eefae4730d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -744,6 +744,12 @@ public function processLongArgument($arg, $pos) $this->cache = false; self::$overriddenDefaults['cache'] = true; break; + case 'delete-cache': + ob_start(); + Util\Cache::delete($this->cacheFile); + $output = ob_get_contents(); + ob_end_clean(); + throw new DeepExitException($output, 0); case 'ignore-annotations': if (isset(self::$overriddenDefaults['annotations']) === true) { break; @@ -1344,7 +1350,7 @@ public function printShortUsage($return=false) public function printPHPCSUsage() { echo 'Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--colors] [--no-colors]'.PHP_EOL; - echo ' [--cache[=]] [--no-cache] [--tab-width=]'.PHP_EOL; + echo ' [--cache[=]] [--delete-cache] [--no-cache] [--tab-width=]'.PHP_EOL; echo ' [--report=] [--report-file=] [--report-=]'.PHP_EOL; echo ' [--report-width=] [--basepath=] [--bootstrap=]'.PHP_EOL; echo ' [--severity=] [--error-severity=] [--warning-severity=]'.PHP_EOL; @@ -1376,10 +1382,11 @@ public function printPHPCSUsage() echo ' --colors Use colors in output'.PHP_EOL; echo ' --no-colors Do not use colors in output (this is the default)'.PHP_EOL; echo ' --cache Cache results between runs'.PHP_EOL; + echo ' --delete-cache Delete existing cached data'.PHP_EOL; echo ' --no-cache Do not cache results between runs (this is the default)'.PHP_EOL; echo ' --ignore-annotations Ignore all phpcs: annotations in code comments'.PHP_EOL; echo PHP_EOL; - echo ' Use a specific file for caching (uses a temporary file by default)'.PHP_EOL; + echo ' Use (or delete) a specific file for caching (uses a temporary file by default)'.PHP_EOL; echo ' A path to strip from the front of file paths inside reports'.PHP_EOL; echo ' A comma separated list of files to run before processing begins'.PHP_EOL; echo ' The encoding of the files being checked (default is utf-8)'.PHP_EOL; diff --git a/src/Util/Cache.php b/src/Util/Cache.php index b7819c91eb..4e50ec5833 100644 --- a/src/Util/Cache.php +++ b/src/Util/Cache.php @@ -218,10 +218,7 @@ function ($file, $key, $iterator) { $numFiles = count($config->files); $cacheFile = null; - $cacheDir = getenv('XDG_CACHE_HOME'); - if ($cacheDir === false || is_dir($cacheDir) === false) { - $cacheDir = sys_get_temp_dir(); - } + $cacheDir = self::getCacheDir(); foreach ($paths as $file => $count) { if ($count !== $numFiles) { @@ -345,4 +342,64 @@ public static function getSize() }//end getSize() + /** + * Returns the dir in which cache files should be stored/found + * + * @return string + */ + private static function getCacheDir() + { + $cacheDir = getenv('XDG_CACHE_HOME'); + if ($cacheDir === false || is_dir($cacheDir) === false) { + $cacheDir = sys_get_temp_dir(); + } + + return $cacheDir; + + }//end getCacheDir() + + + /** + * Deletes the cache file + * + * @param string $cacheFile A cache file provided by the user to be deleted + * + * @return void + */ + public static function delete(?string $cacheFile=null) + { + if ($cacheFile !== null) { + if (unlink($cacheFile) === true) { + echo $cacheFile.' deleted successfully'.PHP_EOL; + } else { + $error = error_get_last(); + echo "ERROR : Could not delete ".$cacheFile.': '.$error['message'].PHP_EOL; + } + + return; + } + + $cacheDir = self::getCacheDir(); + $files = glob($cacheDir.DIRECTORY_SEPARATOR.'phpcs.*.cache'); + + if (count($files) === 0) { + echo 'No cache to delete'.PHP_EOL; + return; + } + + $deletedFiles = 0; + foreach ($files as $file) { + if (unlink($file) === true) { + $deletedFiles++; + } else { + $error = error_get_last(); + echo "ERROR : Could not delete ".$file.': '.$error['message'].PHP_EOL; + } + } + + echo $deletedFiles.' cache files deleted successfully'.PHP_EOL; + + }//end delete() + + }//end class