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

Source for file FunctionCommentSniff.php

Documentation is available at FunctionCommentSniff.php

  1. <?php
  2. /**
  3.  * Parses and verifies the doc comments for functions.
  4.  *
  5.  * Same as the Squiz standard, but adds support for API tags.
  6.  *
  7.  * @author    Greg Sherwood <gsherwood@squiz.net>
  8.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  9.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  10.  */
  11.  
  12. namespace PHP_CodeSniffer\Standards\MySource\Sniffs\Commenting;
  13.  
  14. use PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff as SquizFunctionCommentSniff;
  15. use PHP_CodeSniffer\Util\Tokens;
  16. use PHP_CodeSniffer\Files\File;
  17.  
  18. class FunctionCommentSniff extends SquizFunctionCommentSniff
  19. {
  20.  
  21.  
  22.     /**
  23.      * Processes this test, when one of its tokens is encountered.
  24.      *
  25.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  26.      * @param int                         $stackPtr  The position of the current token
  27.      *                                                in the stack passed in $tokens.
  28.      *
  29.      * @return void 
  30.      */
  31.     public function process(File $phpcsFile$stackPtr)
  32.     {
  33.         parent::process($phpcsFile$stackPtr);
  34.  
  35.         $tokens $phpcsFile->getTokens();
  36.         $find   = Tokens::$methodPrefixes;
  37.         $find[= T_WHITESPACE;
  38.  
  39.         $commentEnd $phpcsFile->findPrevious($find($stackPtr - 1)nulltrue);
  40.         if ($tokens[$commentEnd]['code'!== T_DOC_COMMENT_CLOSE_TAG{
  41.             return;
  42.         }
  43.  
  44.         $commentStart $tokens[$commentEnd]['comment_opener'];
  45.         $hasApiTag    = false;
  46.         foreach ($tokens[$commentStart]['comment_tags'as $tag{
  47.             if ($tokens[$tag]['content'=== '@api'{
  48.                 if ($hasApiTag === true{
  49.                     // We've come across an API tag already, which means
  50.                     // we were not the first tag in the API list.
  51.                     $error 'The @api tag must come first in the @api tag list in a function comment';
  52.                     $phpcsFile->addError($error$tag'ApiNotFirst');
  53.                 }
  54.  
  55.                 $hasApiTag = true;
  56.  
  57.                 // There needs to be a blank line before the @api tag.
  58.                 $prev $phpcsFile->findPrevious(array(T_DOC_COMMENT_STRINGT_DOC_COMMENT_TAG)($tag - 1));
  59.                 if ($tokens[$prev]['line'!== ($tokens[$tag]['line'- 2)) {
  60.                     $error 'There must be one blank line before the @api tag in a function comment';
  61.                     $phpcsFile->addError($error$tag'ApiSpacing');
  62.                 }
  63.             else if (substr($tokens[$tag]['content']05=== '@api-'{
  64.                 $hasApiTag = true;
  65.  
  66.                 $prev $phpcsFile->findPrevious(array(T_DOC_COMMENT_STRINGT_DOC_COMMENT_TAG)($tag - 1));
  67.                 if ($tokens[$prev]['line'!== ($tokens[$tag]['line'- 1)) {
  68.                     $error 'There must be no blank line before the @%s tag in a function comment';
  69.                     $data  = array($tokens[$tag]['content']);
  70.                     $phpcsFile->addError($error$tag'ApiTagSpacing'$data);
  71.                 }
  72.             }//end if
  73.         }//end foreach
  74.  
  75.         if ($hasApiTag === true && substr($tokens[$tag]['content']04!== '@api'{
  76.             // API tags must be the last tags in a function comment.
  77.             $error 'The @api tags must be the last tags in a function comment';
  78.             $phpcsFile->addError($error$commentEnd'ApiNotLast');
  79.         }
  80.  
  81.     }//end process()
  82.  
  83.  
  84. }//end class

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