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

Source for file ClassFileNameSniff.php

Documentation is available at ClassFileNameSniff.php

  1. <?php
  2. /**
  3.  * Tests that the file name and the name of the class contained within the file match.
  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\Classes;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ClassFileNameSniff implements Sniff
  16. {
  17.  
  18.  
  19.     /**
  20.      * Returns an array of tokens this test wants to listen for.
  21.      *
  22.      * @return array 
  23.      */
  24.     public function register()
  25.     {
  26.         return array(
  27.                 T_CLASS,
  28.                 T_INTERFACE,
  29.                );
  30.  
  31.     }//end register()
  32.  
  33.  
  34.     /**
  35.      * Processes this test, when one of its tokens is encountered.
  36.      *
  37.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  38.      * @param int                         $stackPtr  The position of the current token in
  39.      *                                                the stack passed in $tokens.
  40.      *
  41.      * @return void 
  42.      */
  43.     public function process(File $phpcsFile$stackPtr)
  44.     {
  45.         $fullPath basename($phpcsFile->getFilename());
  46.         $fileName substr($fullPath0strrpos($fullPath'.'));
  47.         if ($fileName === ''{
  48.             // No filename probably means STDIN, so we can't do this check.
  49.             return;
  50.         }
  51.  
  52.         $tokens  $phpcsFile->getTokens();
  53.         $decName $phpcsFile->findNext(T_STRING$stackPtr);
  54.  
  55.         if ($tokens[$decName]['content'!== $fileName{
  56.             $error '%s name doesn\'t match filename; expected "%s %s"';
  57.             $data  = array(
  58.                       ucfirst($tokens[$stackPtr]['content']),
  59.                       $tokens[$stackPtr]['content'],
  60.                       $fileName,
  61.                      );
  62.             $phpcsFile->addError($error$stackPtr'NoMatch'$data);
  63.         }
  64.  
  65.     }//end process()
  66.  
  67.  
  68. }//end class

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