Functions/DynamicCalls: various bug fixes and improvements#592
Merged
rebeccahum merged 12 commits intodevelopfrom Nov 23, 2020
Merged
Functions/DynamicCalls: various bug fixes and improvements#592rebeccahum merged 12 commits intodevelopfrom
rebeccahum merged 12 commits intodevelopfrom
Conversation
... to show which ones should fail and which should pass.
This sniff only listens to `T_VARIABLE` tokens, so checking that what was received is a `T_VARIABLE` is redundant.
The condition above checks if a token is `T_EQUAL` and bows out if it is not. The only code matching on `T_EQUAL` is the `=` operator, which will always have a `length` of `1`.
* Cutting code lines off at 50 chars is maybe taking it a little too far, especially as it makes assignments hard to read. * Use proper tags in docblocks. * Improve (fix) documentation (and move it to the right place).
Join two conditions which both return anyway.
Skip over both whitespace, as well as comments. This reduces false negatives. Includes unit test.
A variable value may be build up of multiple tokens. As it was, the sniff would look for the first text string token after the equal sign within the variable assignment statement, but this disregards that: 1. The text string token found may not be the only token in the statement. 2. A statement can end on a PHP close tag (possibly a bug in PHPCS itself, but that's another matter), which would lead the sniff to look at the next statement for text strings. Fixed now. Includes unit tests, the first four of which resulted in false positives previously.
Text strings can use both single quotes as well as double quotes. When the text string contains an interpolated variable, it will be tokenized as `T_DOUBLE_QUOTED_STRING`, but when it is a plain text string, a double quoted text string will be tokenized as `T_CONSTANT_ENCAPSED_STRING`, same as single quoted text string. The sniff did not take this into account, leading to false negatives. The sniff also would strip quotes from within a text - `'my\'text'` - . This did not cause a problem for this sniff as function names cannot have back slashes in them, but it was still wrong. Fixed now by using the WPCS `strip_quotes()` method. Includes unit test which would fail previously.
The sniff maintains a cache of all the variables it has seen and their assigned value. When a variable is encountered, it would: * Check if it was an (plain text) assignment and if so, register the variable name + value to the cache. * Next, call the `find_dynamic_calls()` method, which first checks if any variables have been registered to the cache before doing anything. * And then checks for dynamic function calls and if one is found, checks if the variable used is one registered in the cache with a value we are looking for. This is highly inefficient as text string variable assignments are common and, as it was, _every single one_ would be added to the cache. With a large code base, that means that the cache could grow pretty large. It also means that the logic to determine if something is a dynamic function call would be executed even when there would be no text strings registered in the cache which could match any of the ones we're looking for. By changing the order of the logic, the memory leak and performance inefficiency is removed. With the updated logic, the sniff will: * Check if it was an (plain text) assignment **and if the text string matches one we're looking for** and if so, register the variable name + value to the cache. * Next, call the `find_dynamic_calls()` method, which first checks if any variables have been registered to the cache before doing anything. * And then checks for dynamic function calls. This means that if none of the previous assignments encountered matches any of the target text strings (~ 99% of the time), this sniff will bow out at step 2 before executing the logic to check if a variable assignment is a dynamic function call.
Rename the `private` `$blacklisted_functions` property to `$function_names` to get rid of the use of a non-inclusive term.
Skip over both whitespace, as well as comments and take live coding into account. This reduces false negatives, as well as fixing issue 590. Includes unit tests. Fixes 590
GaryJones
reviewed
Oct 25, 2020
| return; | ||
| } | ||
|
|
||
| if ( $this->tokens[ $t_item_key ]['length'] !== 1 ) { |
Contributor
There was a problem hiding this comment.
Just curious - what tokens are == and ===?
Collaborator
Author
There was a problem hiding this comment.
===>T_IS_EQUAL====>T_IS_IDENTICAL
GaryJones
approved these changes
Oct 25, 2020
Contributor
GaryJones
left a comment
There was a problem hiding this comment.
Love the performance fix amongst the general fixes!
Collaborator
Author
|
Please also see my general review comment about this sniff which I've left in the review ticket: #517 (comment) |
rebeccahum
reviewed
Nov 23, 2020
| 'get_defined_vars', | ||
| 'mb_parse_str', | ||
| 'parse_str', | ||
| private $function_names = [ |
Contributor
There was a problem hiding this comment.
Thank you for re-naming this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 PR is the result of a review of the
WordPressVIPMinimum.Functions.DynamicCallssniff.It will be easiest to understand the different changes and their impact by reviewing this PR on the individual commits.
Functions/DynamicCalls: annotate the unit tests
... to show which ones should fail and which should pass.
Functions/DynamicCalls: remove redundant conditions [1]
This sniff only listens to
T_VARIABLEtokens, so checking that what was received is aT_VARIABLEis redundant.Functions/DynamicCalls: remove redundant conditions [2]
The condition above checks if a token is
T_EQUALand bows out if it is not.The only code matching on
T_EQUALis the=operator, which will always have alengthof1.Functions/DynamicCalls: improve code readability
Functions/DynamicCalls: minor simplification
Join two conditions which both return anyway.
Functions/DynamicCalls: bug fix - ignore comments [1]
Skip over both whitespace, as well as comments. This reduces false negatives.
Includes unit test.
Functions/DynamicCalls: bug fix - don't blindly use the next text string
A variable value may be build up of multiple tokens.
As it was, the sniff would look for the first text string token after the equal sign within the variable assignment statement, but this disregards that:
Fixed now.
Includes unit tests, the first four of which resulted in false positives previously.
Functions/DynamicCalls: bug fix - allow for double quotes
Text strings can use both single quotes as well as double quotes.
When the text string contains an interpolated variable, it will be tokenized as
T_DOUBLE_QUOTED_STRING, but when it is a plain text string, a double quoted text string will be tokenized asT_CONSTANT_ENCAPSED_STRING, same as single quoted text string.The sniff did not take this into account, leading to false negatives.
The sniff also would strip quotes from within a text -
'my\'text'- . This did not cause a problem for this sniff as function names cannot have back slashes in them, but it was still wrong.Fixed now by using the WPCS
strip_quotes()method.Includes unit test which would fail previously.
Functions/DynamicCalls: bug fix - fix memory + performance issue
The sniff maintains a cache of all the variables it has seen and their assigned value.
When a variable is encountered, it would:
find_dynamic_calls()method, which first checks if any variables have been registered to the cache before doing anything.This is highly inefficient as text string variable assignments are common and, as it was, every single one would be added to the cache.
With a large code base, that means that the cache could grow pretty large.
It also means that the logic to determine if something is a dynamic function call would be executed even when there would be no text strings registered in the cache which could match any of the ones we're looking for.
By changing the order of the logic, the memory leak and performance inefficiency is removed.
With the updated logic, the sniff will:
find_dynamic_calls()method, which first checks if any variables have been registered to the cache before doing anything.This means that if none of the previous assignments encountered matches any of the target text strings (~ 99% of the time), this sniff will bow out at step 2 before executing the logic to check if a variable assignment is a dynamic function call.
Functions/DynamicCalls: rename private property
Rename the
private$blacklisted_functionsproperty to$function_namesto get rid of the use of a non-inclusive term.Loosely related to #492
Functions/DynamicCalls: bug fix - ignore comments [2]
Skip over both whitespace, as well as comments and take live coding into account. This reduces false negatives, as well as fixing issue #590.
Includes unit tests.
Fixes #590
Functions/DynamicCalls: error message tweak