Skip to content
Open
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
11 changes: 9 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[=<cacheFile>]] [--no-cache] [--tab-width=<tabWidth>]'.PHP_EOL;
echo ' [--cache[=<cacheFile>]] [--delete-cache] [--no-cache] [--tab-width=<tabWidth>]'.PHP_EOL;
echo ' [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>]'.PHP_EOL;
echo ' [--report-width=<reportWidth>] [--basepath=<basepath>] [--bootstrap=<bootstrap>]'.PHP_EOL;
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
Expand Down Expand Up @@ -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 ' <cacheFile> Use a specific file for caching (uses a temporary file by default)'.PHP_EOL;
echo ' <cacheFile> Use (or delete) a specific file for caching (uses a temporary file by default)'.PHP_EOL;
echo ' <basepath> A path to strip from the front of file paths inside reports'.PHP_EOL;
echo ' <bootstrap> A comma separated list of files to run before processing begins'.PHP_EOL;
echo ' <encoding> The encoding of the files being checked (default is utf-8)'.PHP_EOL;
Expand Down
65 changes: 61 additions & 4 deletions src/Util/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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