PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file GetRequestDataSniff.php

Documentation is available at GetRequestDataSniff.php

  1. <?php
  2. /**
  3.  * Ensures that getRequestData() is used to access super globals.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 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\MySource\Sniffs\PHP;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class GetRequestDataSniff 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 array(T_VARIABLE);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this sniff, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token in
  36.      *                                                the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens $phpcsFile->getTokens();
  43.  
  44.         $varName $tokens[$stackPtr]['content'];
  45.         if ($varName !== '$_REQUEST'
  46.             && $varName !== '$_GET'
  47.             && $varName !== '$_POST'
  48.             && $varName !== '$_FILES'
  49.         {
  50.             return;
  51.         }
  52.  
  53.         // The only place these super globals can be accessed directly is
  54.         // in the getRequestData() method of the Security class.
  55.         $inClass = false;
  56.         foreach ($tokens[$stackPtr]['conditions'as $i => $type{
  57.             if ($tokens[$i]['code'=== T_CLASS{
  58.                 $className $phpcsFile->findNext(T_STRING$i);
  59.                 $className $tokens[$className]['content'];
  60.                 if (strtolower($className=== 'security'{
  61.                     $inClass = true;
  62.                 else {
  63.                     // We don't have nested classes.
  64.                     break;
  65.                 }
  66.             else if ($inClass === true && $tokens[$i]['code'=== T_FUNCTION{
  67.                 $funcName $phpcsFile->findNext(T_STRING$i);
  68.                 $funcName $tokens[$funcName]['content'];
  69.                 if (strtolower($funcName=== 'getrequestdata'{
  70.                     // This is valid.
  71.                     return;
  72.                 else {
  73.                     // We don't have nested functions.
  74.                     break;
  75.                 }
  76.             }//end if
  77.         }//end foreach
  78.  
  79.         // If we get to here, the super global was used incorrectly.
  80.         // First find out how it is being used.
  81.         $globalName strtolower(substr($varName2));
  82.         $usedVar    '';
  83.  
  84.         $openBracket $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  85.         if ($tokens[$openBracket]['code'=== T_OPEN_SQUARE_BRACKET{
  86.             $closeBracket $tokens[$openBracket]['bracket_closer'];
  87.             $usedVar      $phpcsFile->getTokensAsString(($openBracket + 1)($closeBracket $openBracket - 1));
  88.         }
  89.  
  90.         $type  'SuperglobalAccessed';
  91.         $error 'The %s super global must not be accessed directly; use Security::getRequestData(';
  92.         $data  = array($varName);
  93.         if ($usedVar !== ''{
  94.             $type  .= 'WithVar';
  95.             $error .= '%s, \'%s\'';
  96.             $data[$usedVar;
  97.             $data[$globalName;
  98.         }
  99.  
  100.         $error .= ') instead';
  101.         $phpcsFile->addError($error$stackPtr$type$data);
  102.  
  103.     }//end process()
  104.  
  105.  
  106. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.