Skip to content

Commit

Permalink
Add sniff to check for usage of $_SESSION var
Browse files Browse the repository at this point in the history
  • Loading branch information
shadyvb committed Oct 20, 2013
1 parent 712d291 commit 62829ec
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Sniffs/VIP/SessionVariableUsageSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* WordPress_Sniffs_VIP_SessionVariableUsageSniff
*
* Discourages the use of session functions
*
* @category PHP
* @package PHP_CodeSniffer
* @author Shady Sharaf <shady@x-team.com>
* @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/69

This comment has been minimized.

Copy link
@jtsternberg

jtsternberg Aug 17, 2015

Contributor

This issue documented (#69) has nothing to do with $_SESSIONs. Is there documentation elsewhere for the philosophy behind them being prohibited?

This comment has been minimized.

Copy link
@jtsternberg

jtsternberg Aug 17, 2015

Contributor

Maybe #75?

*/
class WordPress_Sniffs_VIP_SessionVariableUsageSniff extends Generic_Sniffs_PHP_ForbiddenFunctionsSniff
{

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

}//end register()


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

if ( $tokens[$stackPtr]['content'] == '$_SESSION' ) {
$phpcsFile->addError('Usage of $_SESSION variable is prohibited.', $stackPtr);
}


}//end process()



}//end class
4 changes: 4 additions & 0 deletions Tests/VIP/SessionVariableUsageUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

foo( $_SESSION );
foo( $_SESSION['bar'] );
52 changes: 52 additions & 0 deletions Tests/VIP/SessionVariableUsageUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* WordPress_Tests_VIP_SessionVariableUsageUnitTest
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Shady Sharaf <shady@x-team.com>
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

class WordPress_Tests_VIP_SessionVariableUsageUnitTest 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)
*/
public function getErrorList()
{
return array(
3 => 1,
4 => 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)
*/
public function getWarningList()
{
return array();

}//end getWarningList()


}//end class

?>

0 comments on commit 62829ec

Please sign in to comment.