AbstractFunctionRestrictions: fix constants being recognized as functions #1863
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a follow-up on #1658 which special cased the
curl_version()
function for theWP.AlternativeFunctions
sniff.The bug reported to me was that the sniff was throwing an error for the below code:
Turned out, it wasn't throwing an error for the function call to
curl_version()
. Instead it was (incorrectly) throwing an error for the use of theCURL_VERSION_SSL
constant, thinking that was a function call.I've fixed this now and added a number of additional unit tests.
With an eye on modern PHP, the abstract sniff(s) needs more work, but as PHPCSUtils is expected to have a similar abstract sniff soon which will take all that into account and which we will be able to switch to, I think this will have to do for now.
Commit Summary
AlternativeFunctions: use
whitelist
instead of special caseThe
AbstractFunctionRestrictions
sniff allows for awhitelist
of functions which shouldn't be matched when a wildcard is used.We may as well use it as it will bow out earlier than special casing the function in the
switch
.AbstractFunctionRestrictions: improve matching of function calls
The code as it was could inadvertently match a CONSTANT with the same name as a function.
This has been fixed by adding a check for an open parenthesis after the function name.
Adding that check, however, would break the check on
use function
import statements. So some additional code has been added to make sure those will still be matched too.Includes tightening up the regex pattern.
Includes unit tests via the
WP.AlternativeFunctions
sniff.