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

Source for file DuplicateClassNameSniff.php

Documentation is available at DuplicateClassNameSniff.php

  1. <?php
  2. /**
  3.  * Reports errors if the same class or interface name is used in multiple files.
  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\Generic\Sniffs\Classes;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class DuplicateClassNameSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * List of classes that have been found during checking.
  20.      *
  21.      * @var array 
  22.      */
  23.     protected $foundClasses = array();
  24.  
  25.  
  26.     /**
  27.      * Registers the tokens that this sniff wants to listen for.
  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 this test, when one of its tokens is encountered.
  40.      *
  41.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  42.      * @param int                         $stackPtr  The position of the current token
  43.      *                                                in the stack passed in $tokens.
  44.      *
  45.      * @return void 
  46.      */
  47.     public function process(File $phpcsFile$stackPtr)
  48.     {
  49.         $tokens $phpcsFile->getTokens();
  50.  
  51.         $namespace  '';
  52.         $findTokens = array(
  53.                        T_CLASS,
  54.                        T_INTERFACE,
  55.                        T_NAMESPACE,
  56.                        T_CLOSE_TAG,
  57.                       );
  58.  
  59.         $stackPtr $phpcsFile->findNext($findTokens($stackPtr + 1));
  60.         while ($stackPtr !== false{
  61.             if ($tokens[$stackPtr]['code'=== T_CLOSE_TAG{
  62.                 // We can stop here. The sniff will continue from the next open
  63.                 // tag when PHPCS reaches that token, if there is one.
  64.                 return;
  65.             }
  66.  
  67.             // Keep track of what namespace we are in.
  68.             if ($tokens[$stackPtr]['code'=== T_NAMESPACE{
  69.                 $nsEnd $phpcsFile->findNext(
  70.                     array(
  71.                      T_NS_SEPARATOR,
  72.                      T_STRING,
  73.                      T_WHITESPACE,
  74.                     ),
  75.                     ($stackPtr + 1),
  76.                     null,
  77.                     true
  78.                 );
  79.  
  80.                 $namespace trim($phpcsFile->getTokensAsString(($stackPtr + 1)($nsEnd $stackPtr - 1)));
  81.                 $stackPtr  $nsEnd;
  82.             else {
  83.                 $nameToken $phpcsFile->findNext(T_STRING$stackPtr);
  84.                 $name      $tokens[$nameToken]['content'];
  85.                 if ($namespace !== ''{
  86.                     $name $namespace.'\\'.$name;
  87.                 }
  88.  
  89.                 $compareName strtolower($name);
  90.                 if (isset($this->foundClasses[$compareName]=== true{
  91.                     $type  strtolower($tokens[$stackPtr]['content']);
  92.                     $file  $this->foundClasses[$compareName]['file'];
  93.                     $line  $this->foundClasses[$compareName]['line'];
  94.                     $error 'Duplicate %s name "%s" found; first defined in %s on line %s';
  95.                     $data  = array(
  96.                               $type,
  97.                               $name,
  98.                               $file,
  99.                               $line,
  100.                              );
  101.                     $phpcsFile->addWarning($error$stackPtr'Found'$data);
  102.                 else {
  103.                     $this->foundClasses[$compareName= array(
  104.                                                          'file' => $phpcsFile->getFilename(),
  105.                                                          'line' => $tokens[$stackPtr]['line'],
  106.                                                         );
  107.                 }
  108.             }//end if
  109.  
  110.             $stackPtr $phpcsFile->findNext($findTokens($stackPtr + 1));
  111.         }//end while
  112.  
  113.     }//end process()
  114.  
  115.  
  116. }//end class

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