-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathFetchingRemoteDataSniff.php
58 lines (48 loc) · 1.77 KB
/
FetchingRemoteDataSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
* @link https://github.com/Automattic/VIP-Coding-Standards
*/
namespace WordPressVIPMinimum\Sniffs\Performance;
use PHP_CodeSniffer\Util\Tokens;
use WordPressVIPMinimum\Sniffs\Sniff;
/**
* Restricts usage of file_get_contents().
*/
class FetchingRemoteDataSniff extends Sniff {
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register() {
return [ T_STRING ];
}
/**
* Process this test when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process_token( $stackPtr ) {
$functionName = $this->tokens[ $stackPtr ]['content'];
if ( $functionName !== 'file_get_contents' ) {
return;
}
$data = [ $this->tokens[ $stackPtr ]['content'] ];
$fileNameStackPtr = $this->phpcsFile->findNext( Tokens::$stringTokens, $stackPtr + 1, null, false, null, true );
if ( $fileNameStackPtr === false ) {
$message = '`%s()` is highly discouraged for remote requests, please use `wpcom_vip_file_get_contents()` or `vip_safe_wp_remote_get()` instead. If it\'s for a local file please use WP_Filesystem instead.';
$this->phpcsFile->addWarning( $message, $stackPtr, 'FileGetContentsUnknown', $data );
}
$fileName = $this->tokens[ $fileNameStackPtr ]['content'];
$isRemoteFile = ( strpos( $fileName, '://' ) !== false );
if ( $isRemoteFile === true ) {
$message = '`%s()` is highly discouraged for remote requests, please use `wpcom_vip_file_get_contents()` or `vip_safe_wp_remote_get()` instead.';
$this->phpcsFile->addWarning( $message, $stackPtr, 'FileGetContentsRemoteFile', $data );
}
}
}