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

Source for file ClassCommentSniff.php

Documentation is available at ClassCommentSniff.php

  1. <?php
  2. /**
  3.  * Parses and verifies the class doc comment.
  4.  *
  5.  * Verifies that :
  6.  * <ul>
  7.  *  <li>A class doc comment exists.</li>
  8.  *  <li>The comment uses the correct docblock style.</li>
  9.  *  <li>There are no blank lines after the class comment.</li>
  10.  *  <li>No tags are used in the docblock.</li>
  11.  * </ul>
  12.  *
  13.  * @author    Greg Sherwood <gsherwood@squiz.net>
  14.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  15.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  16.  */
  17.  
  18. namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
  19.  
  20. use PHP_CodeSniffer\Sniffs\Sniff;
  21. use PHP_CodeSniffer\Files\File;
  22. use PHP_CodeSniffer\Util\Tokens;
  23.  
  24. class ClassCommentSniff implements Sniff
  25. {
  26.  
  27.  
  28.     /**
  29.      * Returns an array of tokens this test wants to listen for.
  30.      *
  31.      * @return array 
  32.      */
  33.     public function register()
  34.     {
  35.         return array(T_CLASS);
  36.  
  37.     }//end register()
  38.  
  39.  
  40.     /**
  41.      * Processes this test, when one of its tokens is encountered.
  42.      *
  43.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  44.      * @param int                         $stackPtr  The position of the current token
  45.      *                                                in the stack passed in $tokens.
  46.      *
  47.      * @return void 
  48.      */
  49.     public function process(File $phpcsFile$stackPtr)
  50.     {
  51.         $tokens $phpcsFile->getTokens();
  52.         $find   = Tokens::$methodPrefixes;
  53.         $find[= T_WHITESPACE;
  54.  
  55.         $commentEnd $phpcsFile->findPrevious($find($stackPtr - 1)nulltrue);
  56.         if ($tokens[$commentEnd]['code'!== T_DOC_COMMENT_CLOSE_TAG
  57.             && $tokens[$commentEnd]['code'!== T_COMMENT
  58.         {
  59.             $phpcsFile->addError('Missing class doc comment'$stackPtr'Missing');
  60.             $phpcsFile->recordMetric($stackPtr'Class has doc comment''no');
  61.             return;
  62.         }
  63.  
  64.         $phpcsFile->recordMetric($stackPtr'Class has doc comment''yes');
  65.  
  66.         if ($tokens[$commentEnd]['code'=== T_COMMENT{
  67.             $phpcsFile->addError('You must use "/**" style comments for a class comment'$stackPtr'WrongStyle');
  68.             return;
  69.         }
  70.  
  71.         if ($tokens[$commentEnd]['line'!== ($tokens[$stackPtr]['line'- 1)) {
  72.             $error 'There must be no blank lines after the class comment';
  73.             $phpcsFile->addError($error$commentEnd'SpacingAfter');
  74.         }
  75.  
  76.         $commentStart $tokens[$commentEnd]['comment_opener'];
  77.         foreach ($tokens[$commentStart]['comment_tags'as $tag{
  78.             $error '%s tag is not allowed in class comment';
  79.             $data  = array($tokens[$tag]['content']);
  80.             $phpcsFile->addWarning($error$tag'TagNotAllowed'$data);
  81.         }
  82.  
  83.     }//end process()
  84.  
  85.  
  86. }//end class

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