Skip to content

Commit

Permalink
Added missing cache util class + cache is now using loaded code to ge…
Browse files Browse the repository at this point in the history
…nerate a more accurate hash (ref #530)

If the PHPS core code updates (as with an upgrade) or if the ruleset changes which sniffs are loaded, or if the PHP code inside a sniff file changes (as with a standard update) then a new hash will be generated.
  • Loading branch information
gsherwood committed May 5, 2015
1 parent 8a11931 commit 80d7712
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 5 deletions.
24 changes: 24 additions & 0 deletions autoload.php
Expand Up @@ -137,6 +137,30 @@ public static function getLoadedFileName($class)
}//end getLoadedFileName()


/**
* Gets the mapping of file names to class names.
*
* @return array<string, string>
*/
public static function getLoadedClasses()
{
return self::$loadedClasses;

}//end getLoadedClasses()


/**
* Gets the mapping of class names to file names.
*
* @return array<string, string>
*/
public static function getLoadedFiles()
{
return self::$loadedFiles;

}//end getLoadedFiles()


}//end class


Expand Down
5 changes: 1 addition & 4 deletions src/Files/FileList.php
Expand Up @@ -44,10 +44,7 @@ class FileList implements \Iterator, \Countable
protected $ignorePatterns = array();


public function __construct(
Config $config,
Ruleset $ruleset
) {
public function __construct(Config $config, Ruleset $ruleset) {

$paths = $config->files;
$local = $config->local;
Expand Down
2 changes: 1 addition & 1 deletion src/Runner.php
Expand Up @@ -322,7 +322,7 @@ private function run()
$numFiles = count($todo);

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

if (PHP_CODESNIFFER_VERBOSITY > 0) {
Expand Down
111 changes: 111 additions & 0 deletions src/Util/Cache.php
@@ -0,0 +1,111 @@
<?php

namespace PHP_CodeSniffer\Util;

use PHP_CodeSniffer\Autoload;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;

/**
* A class to process command line phpcs scripts.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

/**
* A class to process command line phpcs scripts.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class Cache
{

private static $path = '';
private static $cache = array();


public static function load(Config $config, Ruleset $ruleset)
{
// Look at every loaded class so far and use their file contents
// to generate a hash for the code used during the run.
// At this point, the loaded class list contains the core PHPCS code
// and all sniffs that have been loaded as part of the run.
$codeHash = '';
$classes = array_keys(Autoload::getLoadedClasses());
sort($classes);
foreach ($classes as $file) {
$codeHash .= md5_file($file);
}

$codeHash = md5($codeHash);

// Along with the code hash, use various settings that can affect
// the results of a run to create a new hash. This hash will be used
// in the cache file name.
$configData = array(
'tabWidth' => $config->tabWidth,
'encoding' => $config->encoding,
'errorSeverity' => $config->errorSeverity,
'warningSeverity' => $config->warningSeverity,
'sniffs' => $config->sniffs,
'standards' => $ruleset->paths,
'codeHash' => $codeHash,
);

sort($configData['sniffs']);
sort($configData['standards']);
$configData['sniffs'] = implode(',', $configData['sniffs']);
$configData['standards'] = implode(',', $configData['standards']);

$configString = implode(',', $configData);
$hash = substr(sha1($configString), 0, 12);

self::$path = getcwd().DIRECTORY_SEPARATOR.".phpcs.$hash.cache";
if (file_exists(self::$path) === true) {
self::$cache = json_decode(file_get_contents(self::$path), true);
}

self::$cache['config'] = $configData;

}//end load()


public static function save()
{
file_put_contents(self::$path, json_encode(self::$cache));

}//end save()


public static function get($key)
{
if (isset(self::$cache[$key]) === true) {
return self::$cache[$key];
}

return false;

}//end get()


public static function set($key, $value)
{
self::$cache[$key] = $value;

}//end set()


}//end class

0 comments on commit 80d7712

Please sign in to comment.