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

Source for file ClassDefinitionOpeningBraceSpaceSniff.php

Documentation is available at ClassDefinitionOpeningBraceSpaceSniff.php

  1. <?php
  2. /**
  3.  * Ensure a single space before, and a newline after, the class opening brace
  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 ClassDefinitionOpeningBraceSpaceSniff 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_OPEN_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.         if ($tokens[($stackPtr - 1)]['code'!== T_WHITESPACE{
  53.             $error 'Expected 1 space before opening brace of class definition; 0 found';
  54.             $fix   $phpcsFile->addFixableError($error$stackPtr'NoneBefore');
  55.             if ($fix === true{
  56.                 $phpcsFile->fixer->addContentBefore($stackPtr' ');
  57.             }
  58.         else {
  59.             $content $tokens[($stackPtr - 1)]['content'];
  60.             if ($content !== ' '{
  61.                 if ($tokens[($stackPtr - 1)]['line'$tokens[$stackPtr]['line']{
  62.                     $length 'newline';
  63.                 else {
  64.                     $length strlen($content);
  65.                     if ($length === 1{
  66.                         $length 'tab';
  67.                     }
  68.                 }
  69.  
  70.                 $error 'Expected 1 space before opening brace of class definition; %s found';
  71.                 $data  = array($length);
  72.                 $fix   $phpcsFile->addFixableError($error$stackPtr'Before'$data);
  73.                 if ($fix === true{
  74.                     $phpcsFile->fixer->replaceToken(($stackPtr - 1)' ');
  75.                 }
  76.             }
  77.         }//end if
  78.  
  79.         $next $phpcsFile->findNext(Tokens::$emptyTokens($stackPtr + 1)nulltrue);
  80.         if ($next === false{
  81.             return;
  82.         }
  83.  
  84.         // Check for nested class definitions.
  85.         $nested = false;
  86.         $found  $phpcsFile->findNext(
  87.             T_OPEN_CURLY_BRACKET,
  88.             ($stackPtr + 1),
  89.             $tokens[$stackPtr]['bracket_closer']
  90.         );
  91.  
  92.         if ($found !== false{
  93.             $nested = true;
  94.         }
  95.  
  96.         if ($tokens[$next]['line'=== $tokens[$stackPtr]['line']{
  97.             $error 'Opening brace should be the last content on the line';
  98.             $fix   $phpcsFile->addFixableError($error$stackPtr'ContentBefore');
  99.             if ($fix === true{
  100.                 $phpcsFile->fixer->addNewline($stackPtr);
  101.             }
  102.         else {
  103.             $foundLines ($tokens[$next]['line'$tokens[$stackPtr]['line'- 1);
  104.             if ($nested === true{
  105.                 if ($foundLines !== 1{
  106.                     $error 'Expected 1 blank line after opening brace of nesting class definition; %s found';
  107.                     $data  = array($foundLines);
  108.                     $fix   $phpcsFile->addFixableError($error$stackPtr'AfterNesting'$data);
  109.  
  110.                     if ($fix === true{
  111.                         if ($foundLines === 0{
  112.                             $phpcsFile->fixer->addNewline($stackPtr);
  113.                         else {
  114.                             $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  115.                             $phpcsFile->fixer->beginChangeset();
  116.                             for ($i ($stackPtr + 1)$i ($next + 1)$i++{
  117.                                 $phpcsFile->fixer->replaceToken($i'');
  118.                             }
  119.  
  120.                             $phpcsFile->fixer->addNewline($stackPtr);
  121.                             $phpcsFile->fixer->endChangeset();
  122.                         }
  123.                     }
  124.                 }//end if
  125.             }//end if
  126.         }//end if
  127.  
  128.     }//end process()
  129.  
  130.  
  131. }//end class

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