You are here

public function RemoteAddressSniff::process in Coder 8.3

Same name and namespace in other branches
  1. 8.2 coder_sniffer/Drupal/Sniffs/Semantics/RemoteAddressSniff.php \Drupal\Sniffs\Semantics\RemoteAddressSniff::process()
  2. 8.3.x coder_sniffer/Drupal/Sniffs/Semantics/RemoteAddressSniff.php \Drupal\Sniffs\Semantics\RemoteAddressSniff::process()

Processes this test, when one of its tokens is encountered.

Parameters

\PHP_CodeSniffer\Files\File $phpcsFile The current file being processed.:

int $stackPtr The position of the current token: in the stack passed in $tokens.

Return value

void

File

coder_sniffer/Drupal/Sniffs/Semantics/RemoteAddressSniff.php, line 48

Class

RemoteAddressSniff
Make sure that ip_address() or Drupal::request()->getClientIp() is used instead of $_SERVER['REMOTE_ADDR'].

Namespace

Drupal\Sniffs\Semantics

Code

public function process(File $phpcsFile, $stackPtr) {
  $string = $phpcsFile
    ->getTokensAsString($stackPtr, 4);
  $startOfStatement = $phpcsFile
    ->findStartOfStatement($stackPtr);
  if (($string === '$_SERVER["REMOTE_ADDR"]' || $string === '$_SERVER[\'REMOTE_ADDR\']') && $stackPtr !== $startOfStatement) {
    $error = 'Use ip_address() or Drupal::request()->getClientIp() instead of $_SERVER[\'REMOTE_ADDR\']';
    $phpcsFile
      ->addError($error, $stackPtr, 'RemoteAddress');
  }
}