← All changes
|
src/extra/php-codesniffer-php-token/Util/Cache.php
+62
-71
3.1.0
→
6.3.0
View file →
| @@ -8,93 +8,84 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace PHP_CodeSniffer\Util; |
| 11 | 11 | |
| 12 | -use PHP_CodeSniffer\Autoload; | |
| 13 | -use PHP_CodeSniffer\Config; | |
| 14 | -use PHP_CodeSniffer\Ruleset; | |
| 12 | +class Cache { | |
| 15 | 13 | |
| 16 | -class Cache | |
| 17 | -{ | |
| 14 | + /** | |
| 15 | + * The filesystem location of the cache file. | |
| 16 | + * | |
| 17 | + * @var void | |
| 18 | + */ | |
| 19 | + private static $path = ''; | |
| 18 | 20 | |
| 19 | - /** | |
| 20 | - * The filesystem location of the cache file. | |
| 21 | - * | |
| 22 | - * @var void | |
| 23 | - */ | |
| 24 | - private static $path = ''; | |
| 21 | + /** | |
| 22 | + * The cached data. | |
| 23 | + * | |
| 24 | + * @var array<string, mixed> | |
| 25 | + */ | |
| 26 | + private static $cache = []; | |
| 25 | 27 | |
| 26 | - /** | |
| 27 | - * The cached data. | |
| 28 | - * | |
| 29 | - * @var array<string, mixed> | |
| 30 | - */ | |
| 31 | - private static $cache = []; | |
| 28 | + /** | |
| 29 | + * Saves the current cache to the filesystem. | |
| 30 | + * | |
| 31 | + * @return void | |
| 32 | + */ | |
| 33 | + public static function save() { | |
| 34 | + file_put_contents( self::$path, json_encode( self::$cache ) ); | |
| 32 | 35 | |
| 33 | - /** | |
| 34 | - * Saves the current cache to the filesystem. | |
| 35 | - * | |
| 36 | - * @return void | |
| 37 | - */ | |
| 38 | - public static function save() | |
| 39 | - { | |
| 40 | - file_put_contents(self::$path, json_encode(self::$cache)); | |
| 36 | + }//end save() | |
| 41 | 37 | |
| 42 | - }//end save() | |
| 43 | 38 | |
| 39 | + /** | |
| 40 | + * Retrieves a single entry from the cache. | |
| 41 | + * | |
| 42 | + * @param string $key The key of the data to get. If NULL, | |
| 43 | + * everything in the cache is returned. | |
| 44 | + * | |
| 45 | + * @return mixed | |
| 46 | + */ | |
| 47 | + public static function get( $key = null ) { | |
| 48 | + if ( $key === null ) { | |
| 49 | + return self::$cache; | |
| 50 | + } | |
| 44 | 51 | |
| 45 | - /** | |
| 46 | - * Retrieves a single entry from the cache. | |
| 47 | - * | |
| 48 | - * @param string $key The key of the data to get. If NULL, | |
| 49 | - * everything in the cache is returned. | |
| 50 | - * | |
| 51 | - * @return mixed | |
| 52 | - */ | |
| 53 | - public static function get($key=null) | |
| 54 | - { | |
| 55 | - if ($key === null) { | |
| 56 | - return self::$cache; | |
| 57 | - } | |
| 52 | + if ( isset( self::$cache[ $key ] ) === true ) { | |
| 53 | + return self::$cache[ $key ]; | |
| 54 | + } | |
| 58 | 55 | |
| 59 | - if (isset(self::$cache[$key]) === true) { | |
| 60 | - return self::$cache[$key]; | |
| 61 | - } | |
| 56 | + return false; | |
| 62 | 57 | |
| 63 | - return false; | |
| 58 | + }//end get() | |
| 64 | 59 | |
| 65 | - }//end get() | |
| 66 | 60 | |
| 61 | + /** | |
| 62 | + * Retrieves a single entry from the cache. | |
| 63 | + * | |
| 64 | + * @param string $key The key of the data to set. If NULL, | |
| 65 | + * sets the entire cache. | |
| 66 | + * @param mixed $value The value to set. | |
| 67 | + * | |
| 68 | + * @return void | |
| 69 | + */ | |
| 70 | + public static function set( $key, $value ) { | |
| 71 | + if ( $key === null ) { | |
| 72 | + self::$cache = $value; | |
| 73 | + } else { | |
| 74 | + self::$cache[ $key ] = $value; | |
| 75 | + } | |
| 67 | 76 | |
| 68 | - /** | |
| 69 | - * Retrieves a single entry from the cache. | |
| 70 | - * | |
| 71 | - * @param string $key The key of the data to set. If NULL, | |
| 72 | - * sets the entire cache. | |
| 73 | - * @param mixed $value The value to set. | |
| 74 | - * | |
| 75 | - * @return void | |
| 76 | - */ | |
| 77 | - public static function set($key, $value) | |
| 78 | - { | |
| 79 | - if ($key === null) { | |
| 80 | - self::$cache = $value; | |
| 81 | - } else { | |
| 82 | - self::$cache[$key] = $value; | |
| 83 | - } | |
| 77 | + }//end set() | |
| 84 | 78 | |
| 85 | - }//end set() | |
| 86 | 79 | |
| 80 | + /** | |
| 81 | + * Retrieves the number of cache entries. | |
| 82 | + * | |
| 83 | + * @return int | |
| 84 | + */ | |
| 85 | + public static function getSize() { | |
| 86 | + return ( count( self::$cache ) - 1 ); | |
| 87 | 87 | |
| 88 | - /** | |
| 89 | - * Retrieves the number of cache entries. | |
| 90 | - * | |
| 91 | - * @return int | |
| 92 | - */ | |
| 93 | - public static function getSize() | |
| 94 | - { | |
| 95 | - return (count(self::$cache) - 1); | |
| 96 | - | |
| 97 | - }//end getSize() | |
| 88 | + }//end getSize() | |
| 98 | 89 | |
| 99 | 90 | |
| 100 | 91 | }//end class |