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

Source for file InlineCommentSniff.php

Documentation is available at InlineCommentSniff.php

  1. <?php
  2. /**
  3.  * Checks that no Perl-style comments are used.
  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\Sniffs\Sniff;
  13. use PHP_CodeSniffer\Files\File;
  14.  
  15. class InlineCommentSniff 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(T_COMMENT);
  27.  
  28.     }//end register()
  29.  
  30.  
  31.     /**
  32.      * Processes this test, when one of its tokens is encountered.
  33.      *
  34.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  35.      * @param int                         $stackPtr  The position of the current token
  36.      *                                                in the stack passed in $tokens.
  37.      *
  38.      * @return void 
  39.      */
  40.     public function process(File $phpcsFile$stackPtr)
  41.     {
  42.         $tokens $phpcsFile->getTokens();
  43.  
  44.         if ($tokens[$stackPtr]['content']{0=== '#'{
  45.             $phpcsFile->recordMetric($stackPtr'Inline comment style''# ...');
  46.  
  47.             $error  'Perl-style comments are not allowed. Use "// Comment."';
  48.             $error .= ' or "/* comment */" instead.';
  49.             $fix    $phpcsFile->addFixableError($error$stackPtr'WrongStyle');
  50.             if ($fix === true{
  51.                 $newComment ltrim($tokens[$stackPtr]['content']'# ');
  52.                 $newComment '// '.$newComment;
  53.                 $phpcsFile->fixer->replaceToken($stackPtr$newComment);
  54.             }
  55.         else if ($tokens[$stackPtr]['content']{0=== '/'
  56.             && $tokens[$stackPtr]['content']{1=== '/'
  57.         {
  58.             $phpcsFile->recordMetric($stackPtr'Inline comment style''// ...');
  59.         else if ($tokens[$stackPtr]['content']{0=== '/'
  60.             && $tokens[$stackPtr]['content']{1=== '*'
  61.         {
  62.             $phpcsFile->recordMetric($stackPtr'Inline comment style''/* ... */');
  63.         }
  64.  
  65.     }//end process()
  66.  
  67.  
  68. }//end class

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