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

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