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 doc comments for classes.
  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\PEAR\Sniffs\Commenting;
  11.  
  12. use PHP_CodeSniffer\Files\File;
  13. use PHP_CodeSniffer\Util\Tokens;
  14.  
  15. class ClassCommentSniff extends FileCommentSniff
  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 int                         $stackPtr  The position of the current token
  40.      *                                                in the stack passed in $tokens.
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $this->currentFile $phpcsFile;
  47.  
  48.         $tokens    $phpcsFile->getTokens();
  49.         $type      strtolower($tokens[$stackPtr]['content']);
  50.         $errorData = array($type);
  51.  
  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.         // Check each tag.
  72.         $this->processTags($phpcsFile$stackPtr$tokens[$commentEnd]['comment_opener']);
  73.  
  74.     }//end process()
  75.  
  76.  
  77.     /**
  78.      * Process the version tag.
  79.      *
  80.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  81.      * @param array                       $tags      The tokens for these tags.
  82.      *
  83.      * @return void 
  84.      */
  85.     protected function processVersion($phpcsFilearray $tags)
  86.     {
  87.         $tokens $phpcsFile->getTokens();
  88.         foreach ($tags as $tag{
  89.             if ($tokens[($tag + 2)]['code'!== T_DOC_COMMENT_STRING{
  90.                 // No content.
  91.                 continue;
  92.             }
  93.  
  94.             $content $tokens[($tag + 2)]['content'];
  95.             if ((strstr($content'Release:'=== false)) {
  96.                 $error 'Invalid version "%s" in doc comment; consider "Release: <package_version>" instead';
  97.                 $data  = array($content);
  98.                 $phpcsFile->addWarning($error$tag'InvalidVersion'$data);
  99.             }
  100.         }
  101.  
  102.     }//end processVersion()
  103.  
  104.  
  105. }//end class

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