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.  
  15. class DuplicateClassDefinitionSniff 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_TAG);
  34.  
  35.     }//end register()
  36.  
  37.  
  38.     /**
  39.      * Processes the tokens that this sniff is interested in.
  40.      *
  41.      * @param PHP_CodeSniffer_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 class definition name.
  52.         $classNames = array();
  53.         $next       $phpcsFile->findNext(T_OPEN_CURLY_BRACKET($stackPtr + 1));
  54.         if ($next === false{
  55.             // No class definitions in the file.
  56.             return;
  57.         }
  58.  
  59.         $find = array(
  60.                  T_CLOSE_CURLY_BRACKET,
  61.                  T_COMMENT,
  62.                  T_OPEN_TAG,
  63.                 );
  64.  
  65.         while ($next !== false{
  66.             $prev $phpcsFile->findPrevious($find($next - 1));
  67.  
  68.             // Create a sorted name for the class so we can compare classes
  69.             // even when the individual names are all over the place.
  70.             $name '';
  71.             for ($i ($prev + 1)$i $next$i++{
  72.                 $name .= $tokens[$i]['content'];
  73.             }
  74.  
  75.             $name trim($name);
  76.             $name str_replace("\n"' '$name);
  77.             $name preg_replace('|[\s]+|'' '$name);
  78.             $name str_replace(', '','$name);
  79.  
  80.             $names explode(','$name);
  81.             sort($names);
  82.             $name implode(','$names);
  83.  
  84.             if (isset($classNames[$name]=== true{
  85.                 $first $classNames[$name];
  86.                 $error 'Duplicate class definition found; first defined on line %s';
  87.                 $data  = array($tokens[$first]['line']);
  88.                 $phpcsFile->addError($error$next'Found'$data);
  89.             else {
  90.                 $classNames[$name$next;
  91.             }
  92.  
  93.             $next $phpcsFile->findNext(T_OPEN_CURLY_BRACKET($next + 1));
  94.         }//end while
  95.  
  96.     }//end process()
  97.  
  98.  
  99. }//end class

Documentation generated on Mon, 11 Mar 2019 14:53:21 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.