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

Source for file PropertyLabelSpacingSniff.php

Documentation is available at PropertyLabelSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensures that a property or label colon has a single space after it and no space before it.
  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.  
  15. class PropertyLabelSpacingSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array('JS');
  24.  
  25.  
  26.     /**
  27.      * Returns an array of tokens this test wants to listen for.
  28.      *
  29.      * @return array 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(
  34.                 T_PROPERTY,
  35.                 T_LABEL,
  36.                );
  37.  
  38.     }//end register()
  39.  
  40.  
  41.     /**
  42.      * Processes this test, when one of its tokens is encountered.
  43.      *
  44.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  45.      * @param int                         $stackPtr  The position of the current token
  46.      *                                                in the stack passed in $tokens.
  47.      *
  48.      * @return void 
  49.      */
  50.     public function process(File $phpcsFile$stackPtr)
  51.     {
  52.         $tokens $phpcsFile->getTokens();
  53.  
  54.         $colon $phpcsFile->findNext(T_COLON($stackPtr + 1));
  55.  
  56.         if ($colon !== ($stackPtr + 1)) {
  57.             $error 'There must be no space before the colon in a property/label declaration';
  58.             $fix   $phpcsFile->addFixableError($error$stackPtr'Before');
  59.             if ($fix === true{
  60.                 $phpcsFile->fixer->replaceToken(($stackPtr + 1)'');
  61.             }
  62.         }
  63.  
  64.         if ($tokens[($colon + 1)]['code'!== T_WHITESPACE || $tokens[($colon + 1)]['content'!== ' '{
  65.             $error 'There must be a single space after the colon in a property/label declaration';
  66.             $fix   $phpcsFile->addFixableError($error$stackPtr'After');
  67.             if ($fix === true{
  68.                 if ($tokens[($colon + 1)]['code'=== T_WHITESPACE{
  69.                     $phpcsFile->fixer->replaceToken(($colon + 1)' ');
  70.                 else {
  71.                     $phpcsFile->fixer->addContent($colon' ');
  72.                 }
  73.             }
  74.         }
  75.  
  76.     }//end process()
  77.  
  78.  
  79. }//end class

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