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

Source for file PostStatementCommentSniff.php

Documentation is available at PostStatementCommentSniff.php

  1. <?php
  2. /**
  3.  * Checks to ensure that there are no comments after statements.
  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 PostStatementCommentSniff implements Sniff
  16. {
  17.  
  18.     /**
  19.      * A list of tokenizers this sniff supports.
  20.      *
  21.      * @var array 
  22.      */
  23.     public $supportedTokenizers = array(
  24.                                    'PHP',
  25.                                    'JS',
  26.                                   );
  27.  
  28.  
  29.     /**
  30.      * Returns an array of tokens this test wants to listen for.
  31.      *
  32.      * @return array 
  33.      */
  34.     public function register()
  35.     {
  36.         return array(T_COMMENT);
  37.  
  38.     }//end register()
  39.  
  40.  
  41.     /**
  42.      * Processes this sniff, when one of its tokens is encountered.
  43.      *
  44.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  45.      * @param int                         $stackPtr  The position of the current token in the
  46.      *                                                stack passed in $tokens.
  47.      *
  48.      * @return void 
  49.      */
  50.     public function process(File $phpcsFile$stackPtr)
  51.     {
  52.         $tokens $phpcsFile->getTokens();
  53.  
  54.         if (substr($tokens[$stackPtr]['content']02!== '//'{
  55.             return;
  56.         }
  57.  
  58.         $commentLine $tokens[$stackPtr]['line'];
  59.         $lastContent $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  60.  
  61.         if ($tokens[$lastContent]['line'!== $commentLine{
  62.             return;
  63.         }
  64.  
  65.         if ($tokens[$lastContent]['code'=== T_CLOSE_CURLY_BRACKET{
  66.             return;
  67.         }
  68.  
  69.         // Special case for JS files.
  70.         if ($tokens[$lastContent]['code'=== T_COMMA
  71.             || $tokens[$lastContent]['code'=== T_SEMICOLON
  72.         {
  73.             $lastContent $phpcsFile->findPrevious(T_WHITESPACE($lastContent - 1)nulltrue);
  74.             if ($tokens[$lastContent]['code'=== T_CLOSE_CURLY_BRACKET{
  75.                 return;
  76.             }
  77.         }
  78.  
  79.         $error 'Comments may not appear after statements';
  80.         $fix   $phpcsFile->addFixableError($error$stackPtr'Found');
  81.         if ($fix === true{
  82.             $phpcsFile->fixer->addNewlineBefore($stackPtr);
  83.         }
  84.  
  85.     }//end process()
  86.  
  87.  
  88. }//end class

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