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

Source for file DuplicateStyleDefinitionSniff.php

Documentation is available at DuplicateStyleDefinitionSniff.php

  1. <?php
  2. /**
  3.  * Check for duplicate style definitions in the same class.
  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.  
  15. class DuplicateStyleDefinitionSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array('CSS');
  24.  
  25.  
  26.     /**
  27.      * Returns the token types that this sniff is interested in.
  28.      *
  29.      * @return int[] 
  30.      */
  31.     public function register()
  32.     {
  33.         return array(T_OPEN_CURLY_BRACKET);
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes the tokens that this sniff is interested in.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
  42.      * @param int                         $stackPtr  The position in the stack where
  43.      *                                                the token was found.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         // Find the content of each style definition name.
  52.         $styleNames = array();
  53.  
  54.         $next $stackPtr;
  55.         $end  $tokens[$stackPtr]['bracket_closer'];
  56.  
  57.         do {
  58.             $next $phpcsFile->findNext(array(T_STYLET_OPEN_CURLY_BRACKET)($next + 1)$end);
  59.             if ($next === false{
  60.                 // Class definition is empty.
  61.                 break;
  62.             }
  63.  
  64.             if ($tokens[$next]['code'=== T_OPEN_CURLY_BRACKET{
  65.                 $next $tokens[$next]['bracket_closer'];
  66.                 continue;
  67.             }
  68.  
  69.             $name $tokens[$next]['content'];
  70.             if (isset($styleNames[$name]=== true{
  71.                 $first $styleNames[$name];
  72.                 $error 'Duplicate style definition found; first defined on line %s';
  73.                 $data  = array($tokens[$first]['line']);
  74.                 $phpcsFile->addError($error$next'Found'$data);
  75.             else {
  76.                 $styleNames[$name$next;
  77.             }
  78.         while ($next !== false);
  79.  
  80.     }//end process()
  81.  
  82.  
  83. }//end class

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