diff --git a/package.xml b/package.xml index 57d98a105b..00a16b5f89 100644 --- a/package.xml +++ b/package.xml @@ -249,6 +249,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -352,6 +353,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -611,6 +613,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> + + diff --git a/src/Standards/Generic/Docs/PHP/DisallowRequestSuperGlobalStandard.xml b/src/Standards/Generic/Docs/PHP/DisallowRequestSuperGlobalStandard.xml new file mode 100644 index 0000000000..5dfea56fd3 --- /dev/null +++ b/src/Standards/Generic/Docs/PHP/DisallowRequestSuperGlobalStandard.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/src/Standards/Generic/Sniffs/PHP/DisallowRequestSuperGlobalSniff.php b/src/Standards/Generic/Sniffs/PHP/DisallowRequestSuperGlobalSniff.php new file mode 100644 index 0000000000..6ef89f7213 --- /dev/null +++ b/src/Standards/Generic/Sniffs/PHP/DisallowRequestSuperGlobalSniff.php @@ -0,0 +1,54 @@ + + * @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; + } + + $error = 'The $_REQUEST super global should not be used. Use $_GET, $_POST or $_COOKIE instead'; + $phpcsFile->addError($error, $stackPtr, 'Found'); + + }//end process() + + +}//end class diff --git a/src/Standards/Generic/Tests/PHP/DisallowRequestSuperGlobalUnitTest.inc b/src/Standards/Generic/Tests/PHP/DisallowRequestSuperGlobalUnitTest.inc new file mode 100644 index 0000000000..974e45c053 --- /dev/null +++ b/src/Standards/Generic/Tests/PHP/DisallowRequestSuperGlobalUnitTest.inc @@ -0,0 +1,16 @@ + diff --git a/src/Standards/Generic/Tests/PHP/DisallowRequestSuperGlobalUnitTest.php b/src/Standards/Generic/Tests/PHP/DisallowRequestSuperGlobalUnitTest.php new file mode 100644 index 0000000000..63b49a8e44 --- /dev/null +++ b/src/Standards/Generic/Tests/PHP/DisallowRequestSuperGlobalUnitTest.php @@ -0,0 +1,51 @@ + + * @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 + */ + 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 + */ + protected function getWarningList() + { + return []; + + }//end getWarningList() + + +}//end class