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

Source for file SpaceAfterNotSniff.php

Documentation is available at SpaceAfterNotSniff.php

  1. <?php
  2. /**
  3.  * Ensures there is a single space after a NOT operator.
  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\Generic\Sniffs\Formatting;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class SpaceAfterNotSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array(
  25.                                    'PHP',
  26.                                    'JS',
  27.                                   );
  28.  
  29.  
  30.     /**
  31.      * Returns an array of tokens this test wants to listen for.
  32.      *
  33.      * @return array 
  34.      */
  35.     public function register()
  36.     {
  37.         return array(T_BOOLEAN_NOT);
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this test, when one of its tokens is encountered.
  44.      *
  45.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  46.      * @param int                         $stackPtr  The position of the current token in
  47.      *                                                the stack passed in $tokens.
  48.      *
  49.      * @return void 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         $tokens $phpcsFile->getTokens();
  54.  
  55.         $spacing = 0;
  56.         if ($tokens[($stackPtr + 1)]['code'=== T_WHITESPACE{
  57.             $spacing $tokens[($stackPtr + 1)]['length'];
  58.         }
  59.  
  60.         if ($spacing === 1{
  61.             return;
  62.         }
  63.  
  64.         $message 'There must be a single space after a NOT operator; %s found';
  65.         $fix     $phpcsFile->addFixableError($message$stackPtr'Incorrect'array($spacing));
  66.  
  67.         if ($fix === true{
  68.             if ($spacing === 0{
  69.                 $phpcsFile->fixer->addContent($stackPtr' ');
  70.             else {
  71.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  72.             }
  73.         }
  74.  
  75.     }//end process()
  76.  
  77.  
  78. }//end class

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