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

Source for file AjaxNullComparisonSniff.php

Documentation is available at AjaxNullComparisonSniff.php

  1. <?php
  2. /**
  3.  * Ensures that values submitted via JS are not compared to NULL.
  4.  *
  5.  * With jQuery 1.8, the behavior of ajax requests changed so that null values are
  6.  * submitted as null= instead of null=null.
  7.  *
  8.  * @author    Greg Sherwood <gsherwood@squiz.net>
  9.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  10.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  11.  */
  12.  
  13. namespace PHP_CodeSniffer\Standards\MySource\Sniffs\PHP;
  14.  
  15. use PHP_CodeSniffer\Sniffs\Sniff;
  16. use PHP_CodeSniffer\Files\File;
  17.  
  18. class AjaxNullComparisonSniff implements Sniff
  19. {
  20.  
  21.  
  22.     /**
  23.      * Returns an array of tokens this test wants to listen for.
  24.      *
  25.      * @return array 
  26.      */
  27.     public function register()
  28.     {
  29.         return array(T_FUNCTION);
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes this sniff, when one of its tokens is encountered.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  38.      * @param int                         $stackPtr  The position of the current token in
  39.      *                                                the stack passed in $tokens.
  40.      *
  41.      * @return void 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $tokens $phpcsFile->getTokens();
  46.  
  47.         // Make sure it is an API function. We know this by the doc comment.
  48.         $commentEnd   $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG$stackPtr);
  49.         $commentStart $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG($commentEnd - 1));
  50.         $comment      $phpcsFile->getTokensAsString($commentStart($commentEnd $commentStart));
  51.         if (strpos($comment'* @api'=== false{
  52.             return;
  53.         }
  54.  
  55.         // Find all the vars passed in as we are only interested in comparisons
  56.         // to NULL for these specific variables.
  57.         $foundVars = array();
  58.         $open      $tokens[$stackPtr]['parenthesis_opener'];
  59.         $close     $tokens[$stackPtr]['parenthesis_closer'];
  60.         for ($i ($open + 1)$i $close$i++{
  61.             if ($tokens[$i]['code'=== T_VARIABLE{
  62.                 $foundVars[$tokens[$i]['content']] = true;
  63.             }
  64.         }
  65.  
  66.         if (empty($foundVars=== true{
  67.             return;
  68.         }
  69.  
  70.         $start $tokens[$stackPtr]['scope_opener'];
  71.         $end   $tokens[$stackPtr]['scope_closer'];
  72.         for ($i ($start + 1)$i $end$i++{
  73.             if ($tokens[$i]['code'!== T_VARIABLE
  74.                 || isset($foundVars[$tokens[$i]['content']]=== false
  75.             {
  76.                 continue;
  77.             }
  78.  
  79.             $operator $phpcsFile->findNext(T_WHITESPACE($i + 1)nulltrue);
  80.             if ($tokens[$operator]['code'!== T_IS_IDENTICAL
  81.                 && $tokens[$operator]['code'!== T_IS_NOT_IDENTICAL
  82.             {
  83.                 continue;
  84.             }
  85.  
  86.             $nullValue $phpcsFile->findNext(T_WHITESPACE($operator + 1)nulltrue);
  87.             if ($tokens[$nullValue]['code'!== T_NULL{
  88.                 continue;
  89.             }
  90.  
  91.             $error 'Values submitted via Ajax requests should not be compared directly to NULL; use empty() instead';
  92.             $phpcsFile->addWarning($error$nullValue'Found');
  93.         }//end for
  94.  
  95.     }//end process()
  96.  
  97.  
  98. }//end class

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