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

Source for file ClassDeclarationSniff.php

Documentation is available at ClassDeclarationSniff.php

  1. <?php
  2. /**
  3.  * Checks the declaration of the class is correct.
  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\PSR1\Sniffs\Classes;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ClassDeclarationSniff 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.                 T_TRAIT,
  30.                );
  31.  
  32.     }//end register()
  33.  
  34.  
  35.     /**
  36.      * Processes this test, when one of its tokens is encountered.
  37.      *
  38.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  39.      * @param integer                     $stackPtr  The position of the current token in
  40.      *                                                the token stack.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens $phpcsFile->getTokens();
  47.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  48.             return;
  49.         }
  50.  
  51.         $errorData = array(strtolower($tokens[$stackPtr]['content']));
  52.  
  53.         $nextClass $phpcsFile->findNext(array(T_CLASST_INTERFACET_TRAIT)($tokens[$stackPtr]['scope_closer'+ 1));
  54.         if ($nextClass !== false{
  55.             $error 'Each %s must be in a file by itself';
  56.             $phpcsFile->addError($error$nextClass'MultipleClasses'$errorData);
  57.             $phpcsFile->recordMetric($stackPtr'One class per file''no');
  58.         else {
  59.             $phpcsFile->recordMetric($stackPtr'One class per file''yes');
  60.         }
  61.  
  62.         $namespace $phpcsFile->findNext(array(T_NAMESPACET_CLASST_INTERFACET_TRAIT)0);
  63.         if ($tokens[$namespace]['code'!== T_NAMESPACE{
  64.             $error 'Each %s must be in a namespace of at least one level (a top-level vendor name)';
  65.             $phpcsFile->addError($error$stackPtr'MissingNamespace'$errorData);
  66.             $phpcsFile->recordMetric($stackPtr'Class defined in namespace''no');
  67.         else {
  68.             $phpcsFile->recordMetric($stackPtr'Class defined in namespace''yes');
  69.         }
  70.  
  71.     }//end process()
  72.  
  73.  
  74. }//end class

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