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

Source for file CastSpacingSniff.php

Documentation is available at CastSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensure cast statements don't contain whitespace.
  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\Squiz\Sniffs\WhiteSpace;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class CastSpacingSniff 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 the
  37.      *                                                stack passed in $tokens.
  38.      *
  39.      * @return void 
  40.      */
  41.     public function process(File $phpcsFile$stackPtr)
  42.     {
  43.         $tokens $phpcsFile->getTokens();
  44.  
  45.         $content  $tokens[$stackPtr]['content'];
  46.         $expected = str_replace(' '''$content);
  47.         $expected str_replace("\t"''$expected);
  48.  
  49.         if ($content !== $expected{
  50.             $error 'Cast statements must not contain whitespace; expected "%s" but found "%s"';
  51.             $data  = array(
  52.                       $expected,
  53.                       $content,
  54.                      );
  55.  
  56.             $fix $phpcsFile->addFixableError($error$stackPtr'ContainsWhiteSpace'$data);
  57.             if ($fix === true{
  58.                 $phpcsFile->fixer->replaceToken($stackPtr$expected);
  59.             }
  60.         }
  61.  
  62.     }//end process()
  63.  
  64.  
  65. }//end class

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