Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions/DynamicCalls: various bug fixes and improvements #592

Merged
merged 12 commits into from
Nov 23, 2020

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Oct 23, 2020

This PR is the result of a review of the WordPressVIPMinimum.Functions.DynamicCalls sniff.

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_VARIABLE tokens, so checking that what was received is a T_VARIABLE is redundant.

Functions/DynamicCalls: remove redundant conditions [2]

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.

Functions/DynamicCalls: improve code readability

  • 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).

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:

  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.

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 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.

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:

  • 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.

Functions/DynamicCalls: rename private property

Rename the private $blacklisted_functions property to $function_names to 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

jrfnl added 12 commits October 23, 2020 20:26
... 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
@@ -120,10 +120,6 @@ private function collect_variables() {
return;
}

if ( $this->tokens[ $t_item_key ]['length'] !== 1 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious - what tokens are == and ===?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • == => T_IS_EQUAL
  • === => T_IS_IDENTICAL

Ref: https://www.php.net/manual/en/tokens.php

Copy link
Contributor

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the performance fix amongst the general fixes!

@jrfnl
Copy link
Collaborator Author

jrfnl commented Oct 25, 2020

Please also see my general review comment about this sniff which I've left in the review ticket: #517 (comment)

'get_defined_vars',
'mb_parse_str',
'parse_str',
private $function_names = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for re-naming this!

@rebeccahum rebeccahum merged commit 387d448 into develop Nov 23, 2020
@rebeccahum rebeccahum deleted the fix/590-prevent-undefined-offset-notice branch November 23, 2020 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants