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

Source for file ClosingDeclarationCommentSniff.php

Documentation is available at ClosingDeclarationCommentSniff.php

  1. <?php
  2. /**
  3.  * Checks the //end ... comments on classes, interfaces and functions.
  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\Squiz\Sniffs\Commenting;
  11.  
  12. use PHP_CodeSniffer\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class ClosingDeclarationCommentSniff 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_FUNCTION,
  28.                 T_CLASS,
  29.                 T_INTERFACE,
  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 in the
  40.      *                                                stack passed in $tokens..
  41.      *
  42.      * @return void 
  43.      */
  44.     public function process(File $phpcsFile$stackPtr)
  45.     {
  46.         $tokens $phpcsFile->getTokens();
  47.  
  48.         if ($tokens[$stackPtr]['code'=== T_FUNCTION{
  49.             $methodProps $phpcsFile->getMethodProperties($stackPtr);
  50.  
  51.             // Abstract methods do not require a closing comment.
  52.             if ($methodProps['is_abstract'=== true{
  53.                 return;
  54.             }
  55.  
  56.             // If this function is in an interface then we don't require
  57.             // a closing comment.
  58.             if ($phpcsFile->hasCondition($stackPtrT_INTERFACE=== true{
  59.                 return;
  60.             }
  61.  
  62.             if (isset($tokens[$stackPtr]['scope_closer']=== false{
  63.                 $error 'Possible parse error: non-abstract method defined as abstract';
  64.                 $phpcsFile->addWarning($error$stackPtr'Abstract');
  65.                 return;
  66.             }
  67.  
  68.             $decName $phpcsFile->getDeclarationName($stackPtr);
  69.             $comment '//end '.$decName.'()';
  70.         else if ($tokens[$stackPtr]['code'=== T_CLASS{
  71.             $comment '//end class';
  72.         else {
  73.             $comment '//end interface';
  74.         }//end if
  75.  
  76.         if (isset($tokens[$stackPtr]['scope_closer']=== false{
  77.             $error 'Possible parse error: %s missing opening or closing brace';
  78.             $data  = array($tokens[$stackPtr]['content']);
  79.             $phpcsFile->addWarning($error$stackPtr'MissingBrace'$data);
  80.             return;
  81.         }
  82.  
  83.         $closingBracket $tokens[$stackPtr]['scope_closer'];
  84.  
  85.         if ($closingBracket === null{
  86.             // Possible inline structure. Other tests will handle it.
  87.             return;
  88.         }
  89.  
  90.         $data = array($comment);
  91.         if (isset($tokens[($closingBracket + 1)]=== false || $tokens[($closingBracket + 1)]['code'!== T_COMMENT{
  92.             $next $phpcsFile->findNext(T_WHITESPACE($closingBracket + 1)nulltrue);
  93.             if (rtrim($tokens[$next]['content']=== $comment{
  94.                 // The comment isn't really missing; it is just in the wrong place.
  95.                 $fix $phpcsFile->addFixableError('Expected %s directly after closing brace'$closingBracket'Misplaced'$data);
  96.                 if ($fix === true{
  97.                     $phpcsFile->fixer->beginChangeset();
  98.                     for ($i ($closingBracket + 1)$i $next$i++{
  99.                         $phpcsFile->fixer->replaceToken($i'');
  100.                     }
  101.  
  102.                     // Just in case, because indentation fixes can add indents onto
  103.                     // these comments and cause us to be unable to fix them.
  104.                     $phpcsFile->fixer->replaceToken($next$comment.$phpcsFile->eolChar);
  105.                     $phpcsFile->fixer->endChangeset();
  106.                 }
  107.             else {
  108.                 $fix $phpcsFile->addFixableError('Expected %s'$closingBracket'Missing'$data);
  109.                 if ($fix === true{
  110.                     $phpcsFile->fixer->replaceToken($closingBracket'}'.$comment.$phpcsFile->eolChar);
  111.                 }
  112.             }
  113.  
  114.             return;
  115.         }//end if
  116.  
  117.         if (rtrim($tokens[($closingBracket + 1)]['content']!== $comment{
  118.             $fix $phpcsFile->addFixableError('Expected %s'$closingBracket'Incorrect'$data);
  119.             if ($fix === true{
  120.                 $phpcsFile->fixer->replaceToken(($closingBracket + 1)$comment.$phpcsFile->eolChar);
  121.             }
  122.  
  123.             return;
  124.         }
  125.  
  126.     }//end process()
  127.  
  128.  
  129. }//end class

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