| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. |
| 4 |
* |
| 5 |
* @package PHPCSUtils |
| 6 |
* @copyright 2019-2020 PHPCSUtils Contributors |
| 7 |
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 |
* @link https://github.com/PHPCSStandards/PHPCSUtils |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace PHPCSUtils\Internal; |
| 12 |
|
| 13 |
use PHP_CodeSniffer\Files\File; |
| 14 |
|
| 15 |
/** |
| 16 |
* Results cache. |
| 17 |
* |
| 18 |
* Allows to cache the return value of utility functions which do a lot of token walking. |
| 19 |
* Those type of utilities can significantly slow down file scanning, especially |
| 20 |
* with large files and when multiple sniffs use the same utility function. |
| 21 |
* |
| 22 |
* Caching the results can significantly speed things up, though it can also eat memory, |
| 23 |
* so use with care. |
| 24 |
* |
| 25 |
* Typical usage: |
| 26 |
* ```php |
| 27 |
* function doSomething() |
| 28 |
* { |
| 29 |
* if (Cache::isCached($phpcsFile, __METHOD__, $stackPtr) === true) { |
| 30 |
* return Cache::get($phpcsFile, __METHOD__, $stackPtr); |
| 31 |
* } |
| 32 |
* |
| 33 |
* // Do something. |
| 34 |
* |
| 35 |
* Cache::set($phpcsFile, __METHOD__, $stackPtr, $returnValue); |
| 36 |
* return $returnValue; |
| 37 |
* } |
| 38 |
* ``` |
| 39 |
* |
| 40 |
* --------------------------------------------------------------------------------------------- |
| 41 |
* This class is only intended for internal use by PHPCSUtils and is not part of the public API. |
| 42 |
* This also means that it has no promise of backward compatibility. |
| 43 |
* --------------------------------------------------------------------------------------------- |
| 44 |
* |
| 45 |
* @internal |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
final class Cache |
| 50 |
{ |
| 51 |
|
| 52 |
/** |
| 53 |
* Whether caching is enabled or not. |
| 54 |
* |
| 55 |
* Note: this switch is ONLY intended for use within test suites and should never |
| 56 |
* be touched in any other circumstances! |
| 57 |
* |
| 58 |
* Don't forget to always turn the cache back on in a `tear_down()` method! |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @var bool |
| 63 |
*/ |
| 64 |
public static $enabled = true; |
| 65 |
|
| 66 |
/** |
| 67 |
* Results cache. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
* @var array<int, array<string, array<string, array<string|int, mixed>>>> |
| 72 |
* Format: $cache[$loop][$fileName][$key][$id] = mixed $value; |
| 73 |
*/ |
| 74 |
private static $cache = []; |
| 75 |
|
| 76 |
/** |
| 77 |
* Check whether a result has been cached for a certain utility function. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 82 |
* @param string $key The key to identify a particular set of results. |
| 83 |
* It is recommended to pass __METHOD__ to this parameter. |
| 84 |
* @param int|string $id Unique identifier for these results. |
| 85 |
* Generally speaking this will be the $stackPtr passed |
| 86 |
* to the utility function, but it can also something else, |
| 87 |
* like a serialization of args passed to a function or an |
| 88 |
* md5 hash of an input. |
| 89 |
* |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
public static function isCached(File $phpcsFile, $key, $id) |
| 93 |
{ |
| 94 |
if (self::$enabled === false) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$fileName = $phpcsFile->getFilename(); |
| 99 |
$loop = $phpcsFile->fixer->enabled === true ? $phpcsFile->fixer->loops : 0; |
| 100 |
|
| 101 |
return isset(self::$cache[$loop][$fileName][$key]) |
| 102 |
&& \array_key_exists($id, self::$cache[$loop][$fileName][$key]); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Retrieve a previously cached result for a certain utility function. |
| 107 |
* |
| 108 |
* @since 1.0.0 |
| 109 |
* |
| 110 |
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 111 |
* @param string $key The key to identify a particular set of results. |
| 112 |
* It is recommended to pass __METHOD__ to this parameter. |
| 113 |
* @param int|string $id Unique identifier for these results. |
| 114 |
* Generally speaking this will be the $stackPtr passed |
| 115 |
* to the utility function, but it can also something else, |
| 116 |
* like a serialization of args passed to a function or an |
| 117 |
* md5 hash of an input. |
| 118 |
* |
| 119 |
* @return mixed |
| 120 |
*/ |
| 121 |
public static function get(File $phpcsFile, $key, $id) |
| 122 |
{ |
| 123 |
if (self::$enabled === false) { |
| 124 |
return null; |
| 125 |
} |
| 126 |
|
| 127 |
$fileName = $phpcsFile->getFilename(); |
| 128 |
$loop = $phpcsFile->fixer->enabled === true ? $phpcsFile->fixer->loops : 0; |
| 129 |
|
| 130 |
if (isset(self::$cache[$loop][$fileName][$key]) |
| 131 |
&& \array_key_exists($id, self::$cache[$loop][$fileName][$key]) |
| 132 |
) { |
| 133 |
return self::$cache[$loop][$fileName][$key][$id]; |
| 134 |
} |
| 135 |
|
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Retrieve all previously cached results for a certain utility function and a certain file. |
| 141 |
* |
| 142 |
* @since 1.0.0 |
| 143 |
* |
| 144 |
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 145 |
* @param string $key The key to identify a particular set of results. |
| 146 |
* It is recommended to pass __METHOD__ to this parameter. |
| 147 |
* |
| 148 |
* @return array<string|int, mixed> |
| 149 |
*/ |
| 150 |
public static function getForFile(File $phpcsFile, $key) |
| 151 |
{ |
| 152 |
if (self::$enabled === false) { |
| 153 |
return []; |
| 154 |
} |
| 155 |
|
| 156 |
$fileName = $phpcsFile->getFilename(); |
| 157 |
$loop = $phpcsFile->fixer->enabled === true ? $phpcsFile->fixer->loops : 0; |
| 158 |
|
| 159 |
if (isset(self::$cache[$loop][$fileName]) |
| 160 |
&& \array_key_exists($key, self::$cache[$loop][$fileName]) |
| 161 |
) { |
| 162 |
return self::$cache[$loop][$fileName][$key]; |
| 163 |
} |
| 164 |
|
| 165 |
return []; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Cache the result for a certain utility function. |
| 170 |
* |
| 171 |
* @since 1.0.0 |
| 172 |
* |
| 173 |
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 174 |
* @param string $key The key to identify a particular set of results. |
| 175 |
* It is recommended to pass __METHOD__ to this parameter. |
| 176 |
* @param int|string $id Unique identifier for these results. |
| 177 |
* Generally speaking this will be the $stackPtr passed |
| 178 |
* to the utility function, but it can also something else, |
| 179 |
* like a serialization of args passed to a function or an |
| 180 |
* md5 hash of an input. |
| 181 |
* @param mixed $value An arbitrary value to write to the cache. |
| 182 |
* |
| 183 |
* @return mixed |
| 184 |
*/ |
| 185 |
public static function set(File $phpcsFile, $key, $id, $value) |
| 186 |
{ |
| 187 |
if (self::$enabled === false) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$fileName = $phpcsFile->getFilename(); |
| 192 |
$loop = $phpcsFile->fixer->enabled === true ? $phpcsFile->fixer->loops : 0; |
| 193 |
|
| 194 |
/* |
| 195 |
* If this is a phpcbf run and we've reached the next loop, clear the cache |
| 196 |
* of all previous loops to free up memory. |
| 197 |
*/ |
| 198 |
if (isset(self::$cache[$loop]) === false |
| 199 |
&& empty(self::$cache) === false |
| 200 |
) { |
| 201 |
self::clear(); |
| 202 |
} |
| 203 |
|
| 204 |
self::$cache[$loop][$fileName][$key][$id] = $value; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Clear the cache. |
| 209 |
* |
| 210 |
* @since 1.0.0 |
| 211 |
* |
| 212 |
* @return void |
| 213 |
*/ |
| 214 |
public static function clear() |
| 215 |
{ |
| 216 |
self::$cache = []; |
| 217 |
} |
| 218 |
} |
| 219 |
|