Skip to content

Conversation

@jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Jun 4, 2020

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 use-case:

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.

Solution

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.

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.
@gsherwood gsherwood added this to the 3.5.6 milestone Jun 11, 2020
gsherwood added a commit that referenced this pull request Jun 22, 2020
@gsherwood gsherwood merged commit 4c7fdee into squizlabs:master Jun 22, 2020
@gsherwood
Copy link
Member

Invalidating the cache whenever the set of extensions loaded changes is the more stable solution.

100% agree. Thanks a lot for fixing this.

@jrfnl jrfnl deleted the feature/caching-invalidate-on-different-loaded-extensions branch June 22, 2020 05:44
@jrfnl
Copy link
Contributor Author

jrfnl commented Jun 22, 2020

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants