Commit 6e88ebe8 authored by Mircea Fernea's avatar Mircea Fernea Committed by Klaus Purer
Browse files

fix(RemoteAddress): Improve error message for Drupal 8 and prevent errors on assignment ( #2902547)

parent a33d3388
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -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
@@ -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');
        }

+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'];
}
+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