Skip to content

Commit e607fb0

Browse files
committed
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.
1 parent 8e63627 commit e607fb0

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ private function collect_variables() {
141141
*
142142
* Register its name and value in the internal array for later usage.
143143
*/
144-
$current_var_value = $this->tokens[ $value_ptr ]['content'];
144+
$current_var_value = $this->strip_quotes( $this->tokens[ $value_ptr ]['content'] );
145145

146-
$this->variables_arr[ $current_var_name ] = str_replace( "'", '', $current_var_value );
146+
$this->variables_arr[ $current_var_name ] = $current_var_value;
147147
}
148148

149149
/**

WordPressVIPMinimum/Tests/Functions/DynamicCallsUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ $test_getting_the_actual_value_4 = 'get_defined_vars' . $source;
3030
$test_getting_the_actual_value_4(); // OK. Unclear what the actual variable value will be.
3131

3232
$ensure_no_notices_are_thrown_on_parse_error = /*comment*/ ;
33+
34+
$test_double_quoted_string = "assert";
35+
$test_double_quoted_string(); // Bad.

WordPressVIPMinimum/Tests/Functions/DynamicCallsUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getErrorList() {
2727
return [
2828
9 => 1,
2929
15 => 1,
30+
35 => 1,
3031
];
3132
}
3233

0 commit comments

Comments
 (0)