Skip to content

Commit 3118358

Browse files
committed
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
1 parent 6b4a43d commit 3118358

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

WordPressVIPMinimum/Sniffs/Functions/DynamicCallsSniff.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,15 @@ private function find_dynamic_calls() {
174174
}
175175

176176
/*
177-
* Check if we have an '(' next, or separated by whitespaces from our current position.
177+
* Check if we have an '(' next.
178178
*/
179-
180-
$i = 0;
181-
182-
do {
183-
$i++;
184-
} while ( $this->tokens[ $this->stackPtr + $i ]['type'] === 'T_WHITESPACE' );
185-
186-
if ( $this->tokens[ $this->stackPtr + $i ]['type'] !== 'T_OPEN_PARENTHESIS' ) {
179+
$next = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $this->stackPtr + 1 ), null, true );
180+
if ( $next === false || $this->tokens[ $next ]['code'] !== T_OPEN_PARENTHESIS ) {
187181
return;
188182
}
189183

190-
$t_item_key = $this->stackPtr + $i;
191-
192184
$message = 'Dynamic calling is not recommended in the case of %s.';
193185
$data = [ $this->variables_arr[ $this->tokens[ $this->stackPtr ]['content'] ] ];
194-
$this->phpcsFile->addError( $message, $t_item_key, 'DynamicCalls', $data );
186+
$this->phpcsFile->addError( $message, $this->stackPtr, 'DynamicCalls', $data );
195187
}
196188
}

WordPressVIPMinimum/Tests/Functions/DynamicCallsUnitTest.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ $my_okay_func = 'my_test';
1212
$my_okay_func(); // OK.
1313

1414
$test_with_comment /*comment*/ = 'func_get_args';
15-
$test_with_comment(); // Bad.
15+
$test_with_comment /*comment*/ (); // Bad.
1616

1717
$test_getting_the_actual_value_1 = function_call( 'extract' );
1818
$test_getting_the_actual_value_1(); // OK. Unclear what the actual variable value will be.
@@ -33,3 +33,6 @@ $ensure_no_notices_are_thrown_on_parse_error = /*comment*/ ;
3333

3434
$test_double_quoted_string = "assert";
3535
$test_double_quoted_string(); // Bad.
36+
37+
// Intentional parse error. This has to be the last test in the file.
38+
$my_notokay_func

0 commit comments

Comments
 (0)