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

Source for file ClassDefinitionNameSpacingSniff.php

Documentation is available at ClassDefinitionNameSpacingSniff.php

  1. <?php
  2. /**
  3.  * Ensure there are no blank lines between the names of classes/IDs.
  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 ClassDefinitionNameSpacingSniff 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.         // Do not check nested style definitions as, for example, in @media style rules.
  53.         $nested $phpcsFile->findNext(T_OPEN_CURLY_BRACKET($stackPtr + 1)$tokens[$stackPtr]['bracket_closer']);
  54.         if ($nested !== false{
  55.             return;
  56.         }
  57.  
  58.         // Find the first blank line before this opening brace, unless we get
  59.         // to another style definition, comment or the start of the file.
  60.         $endTokens  = array(
  61.                        T_OPEN_CURLY_BRACKET  => T_OPEN_CURLY_BRACKET,
  62.                        T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  63.                        T_OPEN_TAG            => T_OPEN_TAG,
  64.                       );
  65.         $endTokens += Tokens::$commentTokens;
  66.  
  67.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  68.  
  69.         $foundContent = false;
  70.         $currentLine  $tokens[$prev]['line'];
  71.         for ($i ($stackPtr - 1)$i >= 0; $i--{
  72.             if (isset($endTokens[$tokens[$i]['code']]=== true{
  73.                 break;
  74.             }
  75.  
  76.             if ($tokens[$i]['line'=== $currentLine{
  77.                 if ($tokens[$i]['code'!== T_WHITESPACE{
  78.                     $foundContent = true;
  79.                 }
  80.  
  81.                 continue;
  82.             }
  83.  
  84.             // We changed lines.
  85.             if ($foundContent === false{
  86.                 // Before we throw an error, make sure we are not looking
  87.                 // at a gap before the style definition.
  88.                 $prev $phpcsFile->findPrevious(T_WHITESPACE$inulltrue);
  89.                 if ($prev !== false
  90.                     && isset($endTokens[$tokens[$prev]['code']]=== false
  91.                 {
  92.                     $error 'Blank lines are not allowed between class names';
  93.                     $phpcsFile->addError($error($i + 1)'BlankLinesFound');
  94.                 }
  95.  
  96.                 break;
  97.             }
  98.  
  99.             $foundContent = false;
  100.             $currentLine  $tokens[$i]['line'];
  101.         }//end for
  102.  
  103.     }//end process()
  104.  
  105.  
  106. }//end class

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