Skip to content

Commit 86eaa88

Browse files
committed
Performance/FetchingRemoteData: bug fix - examine all text string tokens in the parameter
As things were, only the first text string token in the parameter was being examined, while lead to false negatives. Includes test.
1 parent 89845e6 commit 86eaa88

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

WordPressVIPMinimum/Sniffs/Performance/FetchingRemoteDataSniff.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,16 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
6767
$this->add_contents_unknown_warning( $stackPtr, $data );
6868
}
6969

70-
$fileName = $this->tokens[ $has_text_string ]['content'];
70+
$isRemoteFile = false;
71+
while ( $has_text_string !== false ) {
72+
if ( strpos( $this->tokens[ $has_text_string ]['content'], '://' ) !== false ) {
73+
$isRemoteFile = true;
74+
break;
75+
}
76+
77+
$has_text_string = $this->phpcsFile->findNext( Tokens::$stringTokens, ( $has_text_string + 1 ), $search_end );
78+
}
7179

72-
$isRemoteFile = ( strpos( $fileName, '://' ) !== false );
7380
if ( $isRemoteFile === true ) {
7481
$message = '`%s()` is highly discouraged for remote requests, please use `wpcom_vip_file_get_contents()` or `vip_safe_wp_remote_get()` instead.';
7582
$this->phpcsFile->addWarning( $message, $stackPtr, 'FileGetContentsRemoteFile', $data );

WordPressVIPMinimum/Tests/Performance/FetchingRemoteDataUnitTest.inc

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

8181
// Bug: don't throw a warning when __DIR__ is used as this indicates use with a local file.
8282
\file_get_contents( __DIR__ . $filename ); // OK.
83+
84+
// Bug: check all text string tokens in the parameter.
85+
$result = file_get_contents(( is_ssl() ? 'http' : 'https' ) . '://example.com'); // Warning FileGetContentsRemoteFile.

WordPressVIPMinimum/Tests/Performance/FetchingRemoteDataUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function getWarningList() {
4343
62 => 1,
4444
70 => 1,
4545
79 => 1,
46+
85 => 1,
4647
];
4748
}
4849
}

0 commit comments

Comments
 (0)