Skip to content

Commit

Permalink
Added experimental caching system between runs for better performance
Browse files Browse the repository at this point in the history
On subsequent runs with the same config settings, a cache file can be used to only check files that have been modified.

Use the --cache command line argument to enable caching, or set the config var "cache" to have it on by default. If set in this way, use --no-cache to disable for a single run.
  • Loading branch information
gsherwood committed May 5, 2015
1 parent 2be6622 commit e5cc0ab
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
3 changes: 3 additions & 0 deletions package.xml
Expand Up @@ -26,6 +26,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
</stability>
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
<notes>
- Caching between runs (--cache, --no-cache and "cache" config var) (request #530)
- Automatic discovery of executable paths (request #571)
-- Thanks to Sergey Morozov for the patch
- Improved the performance of the CSS tokenizer, especially on very large CSS files (thousands of lines)
-- Thanks to Klaus Purer for the patch
- Defined tokens for lower PHP versions are now phpcs-specific string instead of ints
Expand Down
25 changes: 24 additions & 1 deletion src/Config.php
Expand Up @@ -64,6 +64,13 @@ class Config
*/
public $interactive;

/**
* Enable the use of the file cache.
*
* @var bool
*/
public $cache;

/**
* Display colous in output.
*
Expand Down Expand Up @@ -380,6 +387,7 @@ public function restoreDefaults()
$this->standards = array('PEAR');
$this->verbosity = 0;
$this->interactive = false;
$this->cache = false;
$this->colors = false;
$this->explain = false;
$this->local = false;
Expand Down Expand Up @@ -464,6 +472,11 @@ public function restoreDefaults()
$this->colors = (bool) $colors;
}

$cache = self::getConfigData('cache');
if ($cache !== null) {
$this->cache = (bool) $cache;
}

}//end restoreDefaults()


Expand Down Expand Up @@ -565,6 +578,14 @@ public function processLongArgument($arg, $pos)
$this->colors = false;
$this->overriddenDefaults['colors'] = true;
break;
case 'cache':
$this->cache = true;
$this->overriddenDefaults['cache'] = true;
break;
case 'no-cache':
$this->cache = false;
$this->overriddenDefaults['cache'] = true;
break;
case 'config-set':
if (isset($this->cliArgs[($pos + 1)]) === false
|| isset($this->cliArgs[($pos + 2)]) === false
Expand Down Expand Up @@ -883,7 +904,7 @@ public function printUsage()
*/
public function printPHPCSUsage()
{
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--colors] [--no-colors]'.PHP_EOL;
echo 'Usage: phpcs [-nwlsaepvi] [-d key[=value]] [--cache] [--no-cache] [--colors] [--no-colors]'.PHP_EOL;
echo ' [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>] ...'.PHP_EOL;
echo ' [--report-width=<reportWidth>] [--generator=<generator>] [--tab-width=<tabWidth>]'.PHP_EOL;
echo ' [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]'.PHP_EOL;
Expand All @@ -905,6 +926,8 @@ public function printPHPCSUsage()
echo ' --version Print version information'.PHP_EOL;
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 ' --no-cache Do not cache results between runs (this is the default)'.PHP_EOL;
echo ' <file> One or more files and/or directories to check'.PHP_EOL;
echo ' <encoding> The encoding of the files being checked (default is iso-8859-1)'.PHP_EOL;
echo ' <extensions> A comma separated list of file extensions to check'.PHP_EOL;
Expand Down
43 changes: 43 additions & 0 deletions src/Files/LocalFile.php
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Util\Cache;

class LocalFile extends File
{
Expand Down Expand Up @@ -76,4 +77,46 @@ function reloadContent()
}//end reloadContent()


/**
*
*
* @return void
*/
public function process()
{
$hash = $this->path.'.'.md5_file($this->path);
$cache = Cache::get($hash);
if ($cache !== false) {
$this->errors = $cache['errors'];
$this->warnings = $cache['warnings'];
$this->metrics = $cache['metrics'];
$this->errorCount = $cache['errorCount'];
$this->warningCount = $cache['warningCount'];
$this->fixableCount = $cache['fixableCount'];

if (PHP_CODESNIFFER_VERBOSITY > 0
|| (PHP_CODESNIFFER_CBF === true && empty($this->config->files) === false)
) {
echo "[loaded from cache]... ";
}

return;
}

parent::process();

$cache = array(
'errors' => $this->errors,
'warnings' => $this->warnings,
'metrics' => $this->metrics,
'errorCount' => $this->errorCount,
'warningCount' => $this->warningCount,
'fixableCount' => $this->fixableCount,
);

Cache::set($hash, $cache);

}//end process()


}//end class
12 changes: 12 additions & 0 deletions src/Runner.php
Expand Up @@ -15,6 +15,7 @@
use PHP_CodeSniffer\Files\FileList;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Util\Cache;

class Runner
{
Expand Down Expand Up @@ -117,6 +118,7 @@ public function runPHPCBF()
$this->config->generator = null;
$this->config->explain = false;
$this->config->interactive = false;
$this->config->cache = false;
$this->config->showSources = false;
$this->config->reportFile = null;
$this->config->reports = array();
Expand Down Expand Up @@ -319,6 +321,10 @@ private function run()
$todo = new FileList($this->config, $ruleset);
$numFiles = count($todo);

if ($this->config->cache === true) {
Cache::load($this->config);
}

if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo "DONE ($numFiles files in queue)".PHP_EOL;
}
Expand Down Expand Up @@ -492,6 +498,12 @@ private function run()
echo PHP_EOL.PHP_EOL;
}

if ($this->config->cache === true
&& $this->config->stdin === false
) {
Cache::save();
}

$ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit');
$ignoreErrors = Config::getConfigData('ignore_errors_on_exit');

Expand Down
Expand Up @@ -827,7 +827,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}

// Skip to the end of multi-line strings.
if (isset(PHP_CodeSniffer_Tokens::$stringTokens[$tokens[$i]['code']]) === true) {
if (isset(Tokens::$stringTokens[$tokens[$i]['code']]) === true) {
$i = $phpcsFile->findNext($tokens[$i]['code'], ($i + 1), null, true);
$i--;
$valueLine = $tokens[$i]['line'];
Expand Down

0 comments on commit e5cc0ab

Please sign in to comment.