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

Source for file DuplicateClassDefinitionSniff.php

Documentation is available at DuplicateClassDefinitionSniff.php

  1. <?php
  2. /**
  3.  * Check for duplicate class definitions that can be merged into one.
  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 DuplicateClassDefinitionSniff 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_TAG);
  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.         // Find the content of each class definition name.
  53.         $classNames = array();
  54.         $next       $phpcsFile->findNext(T_OPEN_CURLY_BRACKET($stackPtr + 1));
  55.         if ($next === false{
  56.             // No class definitions in the file.
  57.             return;
  58.         }
  59.  
  60.         // Save the class names in a "scope",
  61.         // to prevent false positives with @media blocks.
  62.         $scope 'main';
  63.  
  64.         $find = array(
  65.                  T_CLOSE_CURLY_BRACKET,
  66.                  T_OPEN_CURLY_BRACKET,
  67.                  T_OPEN_TAG,
  68.                 );
  69.  
  70.         while ($next !== false{
  71.             $prev $phpcsFile->findPrevious($find($next - 1));
  72.  
  73.             // Check if an inner block was closed.
  74.             $beforePrev $phpcsFile->findPrevious(Tokens::$emptyTokens($prev - 1)nulltrue);
  75.             if ($beforePrev !== false
  76.                 && $tokens[$beforePrev]['code'=== T_CLOSE_CURLY_BRACKET
  77.             {
  78.                 $scope 'main';
  79.             }
  80.  
  81.             // Create a sorted name for the class so we can compare classes
  82.             // even when the individual names are all over the place.
  83.             $name '';
  84.             for ($i ($prev + 1)$i $next$i++{
  85.                 $name .= $tokens[$i]['content'];
  86.             }
  87.  
  88.             $name trim($name);
  89.             $name str_replace("\n"' '$name);
  90.             $name preg_replace('|[\s]+|'' '$name);
  91.             $name str_replace(', '','$name);
  92.  
  93.             $names explode(','$name);
  94.             sort($names);
  95.             $name implode(','$names);
  96.  
  97.             if ($name{0=== '@'{
  98.                 // Media block has its own "scope".
  99.                 $scope $name;
  100.             else if (isset($classNames[$scope][$name]=== true{
  101.                 $first $classNames[$scope][$name];
  102.                 $error 'Duplicate class definition found; first defined on line %s';
  103.                 $data  = array($tokens[$first]['line']);
  104.                 $phpcsFile->addError($error$next'Found'$data);
  105.             else {
  106.                 $classNames[$scope][$name$next;
  107.             }
  108.  
  109.             $next $phpcsFile->findNext(T_OPEN_CURLY_BRACKET($next + 1));
  110.         }//end while
  111.  
  112.     }//end process()
  113.  
  114.  
  115. }//end class

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