Complex classes like Cache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Cache |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The filesystem location of the cache file. |
||
| 22 | * |
||
| 23 | * @var void |
||
| 24 | */ |
||
| 25 | private static $path = ''; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The cached data. |
||
| 29 | * |
||
| 30 | * @var array<string, mixed> |
||
| 31 | */ |
||
| 32 | private static $cache = array(); |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Loads existing cache data for the run, if any. |
||
| 37 | * |
||
| 38 | * @param \Symplify\PHP7_CodeSniffer\Ruleset $ruleset The ruleset used for the run. |
||
| 39 | * @param \Symplify\PHP7_CodeSniffer\Config $config The config data for the run. |
||
| 40 | * |
||
| 41 | * @return void |
||
| 42 | */ |
||
| 43 | public static function load(Ruleset $ruleset, Config $config) |
||
| 44 | { |
||
| 45 | // Look at every loaded sniff class so far and use their file contents |
||
| 46 | // to generate a hash for the code used during the run. |
||
| 47 | // At this point, the loaded class list contains the core PHPCS code |
||
| 48 | // and all sniffs that have been loaded as part of the run. |
||
| 49 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 50 | echo PHP_EOL."\tGenerating loaded file list for code hash".PHP_EOL; |
||
| 51 | } |
||
| 52 | |||
| 53 | $codeHash = ''; |
||
| 54 | $classes = array_keys(Autoload::getLoadedClasses()); |
||
| 55 | sort($classes); |
||
| 56 | |||
| 57 | $installDir = dirname(__DIR__); |
||
| 58 | $installDirLen = strlen($installDir); |
||
| 59 | $standardDir = $installDir.DIRECTORY_SEPARATOR.'Standards'; |
||
| 60 | $standardDirLen = strlen($standardDir); |
||
| 61 | foreach ($classes as $file) { |
||
| 62 | if (substr($file, 0, $standardDirLen) !== $standardDir) { |
||
| 63 | if (substr($file, 0, $installDirLen) === $installDir) { |
||
| 64 | // We are only interested in sniffs here. |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 69 | echo "\t\t=> external file: $file".PHP_EOL; |
||
| 70 | } |
||
| 71 | } else if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 72 | echo "\t\t=> internal sniff: $file".PHP_EOL; |
||
| 73 | } |
||
| 74 | |||
| 75 | $codeHash .= md5_file($file); |
||
| 76 | } |
||
| 77 | |||
| 78 | // Add the content of the used rulesets to the hash so that sniff setting |
||
| 79 | // changes in the ruleset invalidate the cache. |
||
| 80 | $rulesets = $ruleset->paths; |
||
| 81 | sort($rulesets); |
||
| 82 | foreach ($rulesets as $file) { |
||
| 83 | if (substr($file, 0, $standardDirLen) !== $standardDir) { |
||
| 84 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 85 | echo "\t\t=> external ruleset: $file".PHP_EOL; |
||
| 86 | } |
||
| 87 | } else if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 88 | echo "\t\t=> internal ruleset: $file".PHP_EOL; |
||
| 89 | } |
||
| 90 | |||
| 91 | $codeHash .= md5_file($file); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Go through the core PHPCS code and add those files to the file |
||
| 95 | // hash. This ensures that core PHPCS changes will also invalidate the cache. |
||
| 96 | // Note that we ignore sniffs here, and any files that don't affect |
||
| 97 | // the outcome of the run. |
||
| 98 | $di = new \RecursiveIteratorIterator( |
||
|
|
|||
| 99 | new \RecursiveDirectoryIterator($installDir), |
||
| 100 | 0, |
||
| 101 | \RecursiveIteratorIterator::CATCH_GET_CHILD |
||
| 102 | ); |
||
| 103 | |||
| 104 | $di = new \RecursiveDirectoryIterator($installDir); |
||
| 105 | $filter = new \RecursiveCallbackFilterIterator( |
||
| 106 | $di, |
||
| 107 | function ($file, $key, $iterator) { |
||
| 108 | // Skip hidden files. |
||
| 109 | $filename = $file->getFilename(); |
||
| 110 | if (substr($filename, 0, 1) === '.') { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | $filePath = Common::realpath($file->getPathname()); |
||
| 115 | if ($filePath === false) { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (is_dir($filePath) === true |
||
| 120 | && ($filename === 'Standards' |
||
| 121 | || $filename === 'Exceptions' |
||
| 122 | || $filename === 'Reports' |
||
| 123 | || $filename === 'Generators') |
||
| 124 | ) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | return true; |
||
| 129 | } |
||
| 130 | ); |
||
| 131 | |||
| 132 | $iterator = new \RecursiveIteratorIterator($filter); |
||
| 133 | foreach ($iterator as $file) { |
||
| 134 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 135 | echo "\t\t=> core file: $file".PHP_EOL; |
||
| 136 | } |
||
| 137 | |||
| 138 | $codeHash .= md5_file($file); |
||
| 139 | } |
||
| 140 | |||
| 141 | $codeHash = md5($codeHash); |
||
| 142 | |||
| 143 | // Along with the code hash, use various settings that can affect |
||
| 144 | // the results of a run to create a new hash. This hash will be used |
||
| 145 | // in the cache file name. |
||
| 146 | $rulesetHash = md5(var_export($ruleset->ignorePatterns, true).var_export($ruleset->includePatterns, true)); |
||
| 147 | $configData = array( |
||
| 148 | 'tabWidth' => $config->tabWidth, |
||
| 149 | 'encoding' => $config->encoding, |
||
| 150 | 'recordErrors' => $config->recordErrors, |
||
| 151 | 'codeHash' => $codeHash, |
||
| 152 | 'rulesetHash' => $rulesetHash, |
||
| 153 | ); |
||
| 154 | |||
| 155 | $configString = implode(',', $configData); |
||
| 156 | $cacheHash = substr(sha1($configString), 0, 12); |
||
| 157 | |||
| 158 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 159 | echo "\tGenerating cache key data".PHP_EOL; |
||
| 160 | echo "\t\t=> tabWidth: ".$configData['tabWidth'].PHP_EOL; |
||
| 161 | echo "\t\t=> encoding: ".$configData['encoding'].PHP_EOL; |
||
| 162 | echo "\t\t=> recordErrors: ".(int) $configData['recordErrors'].PHP_EOL; |
||
| 163 | echo "\t\t=> codeHash: ".$configData['codeHash'].PHP_EOL; |
||
| 164 | echo "\t\t=> rulesetHash: ".$configData['rulesetHash'].PHP_EOL; |
||
| 165 | echo "\t\t=> cacheHash: $cacheHash".PHP_EOL; |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($config->cacheFile !== null) { |
||
| 169 | $cacheFile = $config->cacheFile; |
||
| 170 | } else { |
||
| 171 | // Determine the common paths for all files being checked. |
||
| 172 | // We can use this to locate an existing cache file, or to |
||
| 173 | // determine where to create a new one. |
||
| 174 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 175 | echo "\tChecking possible cache file paths".PHP_EOL; |
||
| 176 | } |
||
| 177 | |||
| 178 | $paths = array(); |
||
| 179 | foreach ($config->files as $file) { |
||
| 180 | $file = Common::realpath($file); |
||
| 181 | while ($file !== DIRECTORY_SEPARATOR) { |
||
| 182 | if (isset($paths[$file]) === false) { |
||
| 183 | $paths[$file] = 1; |
||
| 184 | } else { |
||
| 185 | $paths[$file]++; |
||
| 186 | } |
||
| 187 | |||
| 188 | $lastFile = $file; |
||
| 189 | $file = dirname($file); |
||
| 190 | if ($file === $lastFile) { |
||
| 191 | // Just in case something went wrong, |
||
| 192 | // we don't want to end up in an infinite loop. |
||
| 193 | break; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | ksort($paths); |
||
| 199 | $paths = array_reverse($paths); |
||
| 200 | |||
| 201 | $numFiles = count($config->files); |
||
| 202 | $tmpDir = sys_get_temp_dir(); |
||
| 203 | $cacheFile = null; |
||
| 204 | foreach ($paths as $file => $count) { |
||
| 205 | if ($count !== $numFiles) { |
||
| 206 | unset($paths[$file]); |
||
| 207 | continue; |
||
| 208 | } |
||
| 209 | |||
| 210 | $fileHash = substr(sha1($file), 0, 12); |
||
| 211 | $testFile = $tmpDir.DIRECTORY_SEPARATOR."phpcs.$fileHash.$cacheHash.cache"; |
||
| 212 | if ($cacheFile === null) { |
||
| 213 | // This will be our default location if we can't find |
||
| 214 | // an existing file. |
||
| 215 | $cacheFile = $testFile; |
||
| 216 | } |
||
| 217 | |||
| 218 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 219 | echo "\t\t=> $testFile".PHP_EOL; |
||
| 220 | echo "\t\t\t * based on shared location: $file *".PHP_EOL; |
||
| 221 | } |
||
| 222 | |||
| 223 | if (file_exists($testFile) === true) { |
||
| 224 | $cacheFile = $testFile; |
||
| 225 | break; |
||
| 226 | } |
||
| 227 | }//end foreach |
||
| 228 | |||
| 229 | if ($cacheFile === null) { |
||
| 230 | // Unlikely, but just in case $paths is empty for some reason. |
||
| 231 | $cacheFile = $tmpDir.DIRECTORY_SEPARATOR."phpcs.$cacheHash.cache"; |
||
| 232 | } |
||
| 233 | }//end if |
||
| 234 | |||
| 235 | self::$path = $cacheFile; |
||
| 236 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 237 | echo "\t=> Using cache file: ".self::$path.PHP_EOL; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (file_exists(self::$path) === true) { |
||
| 241 | self::$cache = json_decode(file_get_contents(self::$path), true); |
||
| 242 | |||
| 243 | // Verify the contents of the cache file. |
||
| 244 | if (self::$cache['config'] !== $configData) { |
||
| 245 | self::$cache = array(); |
||
| 246 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 247 | echo "\t* cache was invalid and has been cleared *".PHP_EOL; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } else if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
| 251 | echo "\t* cache file does not exist *".PHP_EOL; |
||
| 252 | } |
||
| 253 | |||
| 254 | self::$cache['config'] = $configData; |
||
| 255 | |||
| 256 | }//end load() |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * Saves the current cache to the filesystem. |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public static function save() |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Retrieves a single entry from the cache. |
||
| 273 | * |
||
| 274 | * @param string $key The key of the data to get. If NULL, |
||
| 275 | * everything in the cache is returned. |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | public static function get($key=null) |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Retrieves a single entry from the cache. |
||
| 296 | * |
||
| 297 | * @param string $key The key of the data to set. If NULL, |
||
| 298 | * sets the entire cache. |
||
| 299 | * @param mixed $value The value to set. |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | public static function set($key, $value) |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Retrieves the number of cache entries. |
||
| 316 | * |
||
| 317 | * @return int |
||
| 318 | */ |
||
| 319 | public static function getSize() |
||
| 324 | |||
| 325 | |||
| 326 | }//end class |
||
| 327 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.