Loading coder_sniffer/Drupal/Sniffs/Semantics/RemoteAddressSniff.php +5 −4 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; /** * Make sure that the function ip_address() is used instead of * Make sure that ip_address() or Drupal::request()->getClientIp() is used instead of * $_SERVER['REMOTE_ADDR']. * * @category PHP Loading Loading @@ -48,8 +48,9 @@ class RemoteAddressSniff implements Sniff public function process(File $phpcsFile, $stackPtr) { $string = $phpcsFile->getTokensAsString($stackPtr, 4); if ($string === '$_SERVER["REMOTE_ADDR"]' || $string === '$_SERVER[\'REMOTE_ADDR\']') { $error = 'Use the function ip_address() instead of $_SERVER[\'REMOTE_ADDR\']'; $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'); } Loading coder_sniffer/Drupal/Test/Semantics/RemoteAddressUnitTest.inc 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php /** * @file * Drupal.Semantics.RemoteAddress sniff tests. */ // Pass - Drupal.Semantics.RemoteAddress.RemoteAddress. $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Error - Drupal.Semantics.RemoteAddress.RemoteAddress. $remote_address = $_SERVER['REMOTE_ADDR']; function dummy() { // Pass - Drupal.Semantics.RemoteAddress.RemoteAddress. $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Error - Drupal.Semantics.RemoteAddress.RemoteAddress. $remote_address = $_SERVER['REMOTE_ADDR']; } coder_sniffer/Drupal/Test/Semantics/RemoteAddressUnitTest.php 0 → 100644 +47 −0 Original line number Diff line number Diff line <?php namespace Drupal\Sniffs\Semantics; use Drupal\Test\CoderSniffUnitTest; class RemoteAddressUnitTest extends CoderSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @param string $testFile The filename of the test file. * * @return array(int => int) */ public function getErrorList($testFile = NULL) { return array( 12 => 1, 19 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @param string $testFile The filename of the test file. * * @return array(int => int) */ public function getWarningList($testFile = NULL) { return array(); }//end getWarningList() }//end class Loading
coder_sniffer/Drupal/Sniffs/Semantics/RemoteAddressSniff.php +5 −4 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Sniffs\Sniff; /** * Make sure that the function ip_address() is used instead of * Make sure that ip_address() or Drupal::request()->getClientIp() is used instead of * $_SERVER['REMOTE_ADDR']. * * @category PHP Loading Loading @@ -48,8 +48,9 @@ class RemoteAddressSniff implements Sniff public function process(File $phpcsFile, $stackPtr) { $string = $phpcsFile->getTokensAsString($stackPtr, 4); if ($string === '$_SERVER["REMOTE_ADDR"]' || $string === '$_SERVER[\'REMOTE_ADDR\']') { $error = 'Use the function ip_address() instead of $_SERVER[\'REMOTE_ADDR\']'; $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'); } Loading
coder_sniffer/Drupal/Test/Semantics/RemoteAddressUnitTest.inc 0 → 100644 +20 −0 Original line number Diff line number Diff line <?php /** * @file * Drupal.Semantics.RemoteAddress sniff tests. */ // Pass - Drupal.Semantics.RemoteAddress.RemoteAddress. $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Error - Drupal.Semantics.RemoteAddress.RemoteAddress. $remote_address = $_SERVER['REMOTE_ADDR']; function dummy() { // Pass - Drupal.Semantics.RemoteAddress.RemoteAddress. $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // Error - Drupal.Semantics.RemoteAddress.RemoteAddress. $remote_address = $_SERVER['REMOTE_ADDR']; }
coder_sniffer/Drupal/Test/Semantics/RemoteAddressUnitTest.php 0 → 100644 +47 −0 Original line number Diff line number Diff line <?php namespace Drupal\Sniffs\Semantics; use Drupal\Test\CoderSniffUnitTest; class RemoteAddressUnitTest extends CoderSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @param string $testFile The filename of the test file. * * @return array(int => int) */ public function getErrorList($testFile = NULL) { return array( 12 => 1, 19 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @param string $testFile The filename of the test file. * * @return array(int => int) */ public function getWarningList($testFile = NULL) { return array(); }//end getWarningList() }//end class