Skip to content

Commit 3b7ec02

Browse files
committed
Invalidate the cache when a different set of PHP extensions are loaded
Basic justification: PHPCS uses the `iconv` extension, if available, to determine the length of tokens. The results of sniffs can be different if the length has changed. Additional usecase: While not in use in PHPCS itself, external standards _may_ use additional PHP extension which may or may not be loaded and can influence the results of a run. Think: a sniff which checks the naming conventions of function/class/variable names and takes the file encoding into account. This sniff may be using the Mbstring PHP extension and the sniff could either be skipped if Mbstring is not available or fall back to a less reliable way of determining compliance with the naming conventions. With the above in mind, I believe the cache should keep a hash of the loaded PHP extensions and invalidate the cache if the set of loaded extensions has changed. As in reality, it will probably be a limited set of extensions which will ever be used in sniffs, a choice could be made to keep track of just those extensions instead of the complete list of loaded extensions. I've decided against doing that though, as 1) this makes maintenance more fiddly as a list of "tracked extensions" would need to be maintained and 2) this may limit the imagination of sniff writers. Invalidating the cache whenever the set of extensions loaded changes is the more stable solution.
1 parent 75ff420 commit 3b7ec02

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Util/Cache.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,18 @@ function ($file, $key, $iterator) {
143143
// Along with the code hash, use various settings that can affect
144144
// the results of a run to create a new hash. This hash will be used
145145
// in the cache file name.
146-
$rulesetHash = md5(var_export($ruleset->ignorePatterns, true).var_export($ruleset->includePatterns, true));
147-
$configData = [
148-
'phpVersion' => PHP_VERSION_ID,
149-
'tabWidth' => $config->tabWidth,
150-
'encoding' => $config->encoding,
151-
'recordErrors' => $config->recordErrors,
152-
'annotations' => $config->annotations,
153-
'configData' => Config::getAllConfigData(),
154-
'codeHash' => $codeHash,
155-
'rulesetHash' => $rulesetHash,
146+
$rulesetHash = md5(var_export($ruleset->ignorePatterns, true).var_export($ruleset->includePatterns, true));
147+
$phpExtensionsHash = md5(var_export(get_loaded_extensions(), true));
148+
$configData = [
149+
'phpVersion' => PHP_VERSION_ID,
150+
'phpExtensions' => $phpExtensionsHash,
151+
'tabWidth' => $config->tabWidth,
152+
'encoding' => $config->encoding,
153+
'recordErrors' => $config->recordErrors,
154+
'annotations' => $config->annotations,
155+
'configData' => Config::getAllConfigData(),
156+
'codeHash' => $codeHash,
157+
'rulesetHash' => $rulesetHash,
156158
];
157159

158160
$configString = var_export($configData, true);

0 commit comments

Comments
 (0)