Skip to content

Commit 34ebced

Browse files
committed
2 parents 90b719d + 6a09f8c commit 34ebced

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

package.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
249249
<file baseinstalldir="PHP/CodeSniffer" name="ClosingPHPTagStandard.xml" role="php" />
250250
<file baseinstalldir="PHP/CodeSniffer" name="DeprecatedFunctionsStandard.xml" role="php" />
251251
<file baseinstalldir="PHP/CodeSniffer" name="DisallowAlternativePHPTagsStandard.xml" role="php" />
252+
<file baseinstalldir="PHP/CodeSniffer" name="DisallowRequestSuperGlobalStandard.xml" role="php" />
252253
<file baseinstalldir="PHP/CodeSniffer" name="DisallowShortOpenTagStandard.xml" role="php" />
253254
<file baseinstalldir="PHP/CodeSniffer" name="DiscourageGotoStandard.xml" role="php" />
254255
<file baseinstalldir="PHP/CodeSniffer" name="ForbiddenFunctionsStandard.xml" role="php" />
@@ -352,6 +353,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
352353
<file baseinstalldir="PHP/CodeSniffer" name="CharacterBeforePHPOpeningTagSniff.php" role="php" />
353354
<file baseinstalldir="PHP/CodeSniffer" name="ClosingPHPTagSniff.php" role="php" />
354355
<file baseinstalldir="PHP/CodeSniffer" name="DeprecatedFunctionsSniff.php" role="php" />
356+
<file baseinstalldir="PHP/CodeSniffer" name="DisallowRequestSuperGlobalSniff.php" role="php" />
355357
<file baseinstalldir="PHP/CodeSniffer" name="DisallowShortOpenTagSniff.php" role="php" />
356358
<file baseinstalldir="PHP/CodeSniffer" name="DisallowAlternativePHPTagsSniff.php" role="php" />
357359
<file baseinstalldir="PHP/CodeSniffer" name="DiscourageGotoSniff.php" role="php" />
@@ -611,6 +613,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
611613
<file baseinstalldir="PHP/CodeSniffer" name="DisallowAlternativePHPTagsUnitTest.2.inc.fixed" role="test" />
612614
<file baseinstalldir="PHP/CodeSniffer" name="DisallowAlternativePHPTagsUnitTest.3.inc" role="test" />
613615
<file baseinstalldir="PHP/CodeSniffer" name="DisallowAlternativePHPTagsUnitTest.php" role="test" />
616+
<file baseinstalldir="PHP/CodeSniffer" name="DisallowRequestSuperGlobalUnitTest.inc" role="test" />
617+
<file baseinstalldir="PHP/CodeSniffer" name="DisallowRequestSuperGlobalUnitTest.php" role="test" />
614618
<file baseinstalldir="PHP/CodeSniffer" name="DisallowShortOpenTagUnitTest.1.inc" role="test" />
615619
<file baseinstalldir="PHP/CodeSniffer" name="DisallowShortOpenTagUnitTest.1.inc.fixed" role="test" />
616620
<file baseinstalldir="PHP/CodeSniffer" name="DisallowShortOpenTagUnitTest.2.inc" role="test" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<documentation title="$_REQUEST Super Global">
2+
<standard>
3+
<![CDATA[
4+
$_REQUEST should never be used due to the ambiguity created to identify where the data is coming from. Use $_POST, $_GET or $_COOKIE instead
5+
]]>
6+
</standard>
7+
</documentation>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Ensures the $_REQUEST super global is not used
4+
*
5+
* @author Jeantwan Teuma <[email protected]>
6+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
use PHP_CodeSniffer\Sniffs\Sniff;
14+
15+
class DisallowRequestSuperGlobalSniff implements Sniff
16+
{
17+
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return [T_VARIABLE];
27+
28+
}//end register()
29+
30+
31+
/**
32+
* Processes this sniff, when one of its tokens is encountered.
33+
*
34+
* @param File $phpcsFile The file being scanned.
35+
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
36+
*
37+
* @return void
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$tokens = $phpcsFile->getTokens();
42+
43+
$varName = $tokens[$stackPtr]['content'];
44+
if ($varName !== '$_REQUEST') {
45+
return;
46+
}
47+
48+
$error = 'The $_REQUEST super global should not be used. Use $_GET, $_POST or $_COOKIE instead';
49+
$phpcsFile->addError($error, $stackPtr, 'Found');
50+
51+
}//end process()
52+
53+
54+
}//end class
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
echo $_REQUEST['action'];
3+
4+
echo '$_REQUEST';
5+
6+
echo $_POST['action'];
7+
8+
echo $_GET[$action];
9+
10+
echo $_COOKIE['action'];
11+
12+
$sample = Util::getArrayIndex($_REQUEST, 'sample', '');
13+
$syntax = Util::getArrayIndex($_REQUEST, 'syntax', '');
14+
$value = Util::getArrayIndex($_FILES, $key, $default);
15+
16+
?>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Unit test class for the DisallowRequestSuperGlobal sniff.
4+
*
5+
* @author Jeantwan Teuma <[email protected]>
6+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
namespace PHP_CodeSniffer\Standards\Generic\Tests\PHP;
10+
11+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
13+
class DisallowRequestSuperGlobalUnitTest extends AbstractSniffUnitTest
14+
{
15+
16+
17+
/**
18+
* Returns the lines where errors should occur.
19+
*
20+
* The key of the array should represent the line number and the value
21+
* should represent the number of errors that should occur on that line.
22+
*
23+
* @return array<int, int>
24+
*/
25+
protected function getErrorList()
26+
{
27+
return [
28+
2 => 1,
29+
12 => 1,
30+
13 => 1,
31+
];
32+
33+
}//end getErrorList()
34+
35+
36+
/**
37+
* Returns the lines where warnings should occur.
38+
*
39+
* The key of the array should represent the line number and the value
40+
* should represent the number of warnings that should occur on that line.
41+
*
42+
* @return array<int, int>
43+
*/
44+
protected function getWarningList()
45+
{
46+
return [];
47+
48+
}//end getWarningList()
49+
50+
51+
}//end class

0 commit comments

Comments
 (0)