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

Source for file ClassDefinitionClosingBraceSpaceSniff.php

Documentation is available at ClassDefinitionClosingBraceSpaceSniff.php

  1. <?php
  2. /**
  3.  * Ensure there is a single blank line after the closing brace of a class definition.
  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\CSS;
  11.  
  12.  use PHP_CodeSniffer\Sniffs\Sniff;
  13.  use PHP_CodeSniffer\Files\File;
  14.  use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class ClassDefinitionClosingBraceSpaceSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array('CSS');
  25.  
  26.  
  27.     /**
  28.      * Returns the token types that this sniff is interested in.
  29.      *
  30.      * @return int[] 
  31.      */
  32.     public function register()
  33.     {
  34.         return array(T_CLOSE_CURLY_BRACKET);
  35.  
  36.     }//end register()
  37.  
  38.  
  39.     /**
  40.      * Processes the tokens that this sniff is interested in.
  41.      *
  42.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  43.      * @param int                         $stackPtr  The position in the stack where
  44.      *                                                the token was found.
  45.      *
  46.      * @return void 
  47.      */
  48.     public function process(File $phpcsFile$stackPtr)
  49.     {
  50.         $tokens $phpcsFile->getTokens();
  51.  
  52.         $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  53.         if ($next === false{
  54.             return;
  55.         }
  56.  
  57.         if ($tokens[$next]['code'!== T_CLOSE_TAG{
  58.             $found (($tokens[$next]['line'$tokens[$stackPtr]['line']- 1);
  59.             if ($found !== 1{
  60.                 $error 'Expected one blank line after closing brace of class definition; %s found';
  61.                 $data  = array($found);
  62.                 $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingAfterClose'$data);
  63.  
  64.                 if ($fix === true{
  65.                     if ($found === 0{
  66.                         $phpcsFile->fixer->addNewline($stackPtr);
  67.                     else {
  68.                         $nextContent $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  69.                         $phpcsFile->fixer->beginChangeset();
  70.                         for ($i ($stackPtr + 1)$i ($nextContent - 1)$i++{
  71.                             $phpcsFile->fixer->replaceToken($i'');
  72.                         }
  73.  
  74.                         $phpcsFile->fixer->addNewline($i);
  75.                         $phpcsFile->fixer->endChangeset();
  76.                     }
  77.                 }
  78.             }//end if
  79.         }//end if
  80.  
  81.         // Ignore nested style definitions from here on. The spacing before the closing brace
  82.         // (a single blank line) will be enforced by the above check, which ensures there is a
  83.         // blank line after the last nested class.
  84.         $found $phpcsFile->findPrevious(
  85.             T_CLOSE_CURLY_BRACKET,
  86.             ($stackPtr - 1),
  87.             $tokens[$stackPtr]['bracket_opener']
  88.         );
  89.  
  90.         if ($found !== false{
  91.             return;
  92.         }
  93.  
  94.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  95.         if ($prev === false{
  96.             return;
  97.         }
  98.  
  99.         if ($tokens[$prev]['line'=== $tokens[$stackPtr]['line']{
  100.             $error 'Closing brace of class definition must be on new line';
  101.             $fix   $phpcsFile->addFixableError($error$stackPtr'ContentBeforeClose');
  102.             if ($fix === true{
  103.                 $phpcsFile->fixer->addNewlineBefore($stackPtr);
  104.             }
  105.         }
  106.  
  107.     }//end process()
  108.  
  109.  
  110. }//end class

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