Skip to content

Commit

Permalink
Closes #2373 - Create new Request Super Global Sniffer and add unit t…
Browse files Browse the repository at this point in the history
…est files
  • Loading branch information
Morerice committed Dec 4, 2019
1 parent b3c7925 commit 8be584e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Ensures the $_REQUEST super global is not used
*
* @author Jeantwan Teuma <[email protected]>
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class DisallowRequestSuperGlobalSniff implements Sniff
{


/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return [T_VARIABLE];

}//end register()


/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$varName = $tokens[$stackPtr]['content'];
if ($varName !== '$_REQUEST') {
return;
}

$type = 'RequestSuperGlobalAccessed';
$error = 'The $_REQUEST super global should not be used. Use $_GET, $_POST or $_COOKIE instead';
$phpcsFile->addError($error, $stackPtr, $type, []);

}//end process()


}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
echo $_REQUEST['action'];

echo '$_REQUEST';

echo $_POST['action'];

echo $_GET[$action];

echo $_COOKIE['action'];

$sample = Util::getArrayIndex($_REQUEST, 'sample', '');
$syntax = Util::getArrayIndex($_REQUEST, 'syntax', '');
$value = Util::getArrayIndex($_FILES, $key, $default);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Unit test class for the DisallowRequestSuperGlobal sniff.
*
* @author Jeantwan Teuma <[email protected]>
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Standards\Generic\Tests\PHP;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class DisallowRequestSuperGlobalUnitTest extends AbstractSniffUnitTest
{


/**
* 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.
*
* @return array<int, int>
*/
protected function getErrorList()
{
return [
2 => 1,
12 => 1,
13 => 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.
*
* @return array<int, int>
*/
protected function getWarningList()
{
return [];

}//end getWarningList()


}//end class

0 comments on commit 8be584e

Please sign in to comment.