Skip to content

Commit 7d23be5

Browse files
committed
Performance/FetchingRemoteData: bug fix - false negative for compound parameters
As things were, if the parameter did contain a text string token and that text string token did *not* contain the text "://", the sniff would stay silent, even when the parameter contained additional non-text string tokens, which means we cannot reliably determine whether it is a local file or not. This commit changes the order of the checks to _first_ check if any of the text string tokens in the parameter contain the "://" text. If so, it will throw the (more informative) `FileGetContentsRemoteFile` warning. If not, it will check if the parameter contained anything but text string related or empty tokens. If so, it will throw the (more generic) `FileGetContentsUnknown` warning. Includes test.
1 parent 11092fd commit 7d23be5

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

WordPressVIPMinimum/Sniffs/Performance/FetchingRemoteDataSniff.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,35 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
6262
return;
6363
}
6464

65-
$has_text_string = $this->phpcsFile->findNext( Tokens::$stringTokens, $param_start, $search_end );
66-
if ( $has_text_string === false ) {
67-
$this->add_contents_unknown_warning( $stackPtr, $data );
68-
}
69-
7065
$isRemoteFile = false;
71-
while ( $has_text_string !== false ) {
66+
$search_start = $param_start;
67+
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- Valid usage.
68+
while ( ( $has_text_string = $this->phpcsFile->findNext( Tokens::$stringTokens, $search_start, $search_end ) ) !== false ) {
7269
if ( strpos( $this->tokens[ $has_text_string ]['content'], '://' ) !== false ) {
7370
$isRemoteFile = true;
7471
break;
7572
}
7673

77-
$has_text_string = $this->phpcsFile->findNext( Tokens::$stringTokens, ( $has_text_string + 1 ), $search_end );
74+
$search_start = ( $has_text_string + 1 );
7875
}
7976

8077
if ( $isRemoteFile === true ) {
8178
$message = '`%s()` is highly discouraged for remote requests, please use `wpcom_vip_file_get_contents()` or `vip_safe_wp_remote_get()` instead.';
8279
$this->phpcsFile->addWarning( $message, $stackPtr, 'FileGetContentsRemoteFile', $data );
80+
return;
81+
}
82+
83+
/*
84+
* Okay, so we haven't been able to determine for certain this is a remote file.
85+
* Check for tokens which would make the parameter contents dynamic.
86+
*/
87+
$ignore = Tokens::$emptyTokens;
88+
$ignore += Tokens::$stringTokens;
89+
$ignore += [ T_STRING_CONCAT => T_STRING_CONCAT ];
90+
91+
$has_non_text_string = $this->phpcsFile->findNext( $ignore, $param_start, $search_end, true );
92+
if ( $has_non_text_string !== false ) {
93+
$this->add_contents_unknown_warning( $stackPtr, $data );
8394
}
8495
}
8596

WordPressVIPMinimum/Tests/Performance/FetchingRemoteDataUnitTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ array_walk($files, file_get_contents(...)); // PHP 8.1 first class callable. War
8383

8484
// Bug: check all text string tokens in the parameter.
8585
$result = file_get_contents(( is_ssl() ? 'http' : 'https' ) . '://example.com'); // Warning FileGetContentsRemoteFile.
86+
87+
// Bug: don't presume if the parameter contains a text string token, that will be the only token.
88+
\file_get_contents( $url . '/filename.css' ); // Warning FileGetContentsUnknown.

WordPressVIPMinimum/Tests/Performance/FetchingRemoteDataUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function getWarningList() {
4444
70 => 1,
4545
79 => 1,
4646
85 => 1,
47+
88 => 1,
4748
];
4849
}
4950
}

0 commit comments

Comments
 (0)