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

Source for file SpaceAfterCastSniff.php

Documentation is available at SpaceAfterCastSniff.php

  1. <?php
  2. /**
  3.  * Ensures there is a single space after cast tokens.
  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 SpaceAfterCastSniff implements Sniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Returns an array of tokens this test wants to listen for.
  22.      *
  23.      * @return array 
  24.      */
  25.     public function register()
  26.     {
  27.         return Tokens::$castTokens;
  28.  
  29.     }//end register()
  30.  
  31.  
  32.     /**
  33.      * Processes this test, when one of its tokens is encountered.
  34.      *
  35.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  36.      * @param int                         $stackPtr  The position of the current token in
  37.      *                                                the stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         if ($tokens[($stackPtr + 1)]['code'!== T_WHITESPACE{
  46.             $error 'A cast statement must be followed by a single space';
  47.             $fix   $phpcsFile->addFixableError($error$stackPtr'NoSpace');
  48.             if ($fix === true{
  49.                 $phpcsFile->fixer->addContent($stackPtr' ');
  50.             }
  51.  
  52.             $phpcsFile->recordMetric($stackPtr'Spacing after cast statement'0);
  53.             return;
  54.         }
  55.  
  56.         $phpcsFile->recordMetric($stackPtr'Spacing after cast statement'$tokens[($stackPtr + 1)]['length']);
  57.  
  58.         if ($tokens[($stackPtr + 1)]['length'!== 1{
  59.             $error 'A cast statement must be followed by a single space';
  60.             $fix   $phpcsFile->addFixableError($error$stackPtr'TooMuchSpace');
  61.             if ($fix === true{
  62.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)' ');
  63.             }
  64.         }
  65.  
  66.     }//end process()
  67.  
  68.  
  69. }//end class

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