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 there is adequate spacing between comments.
  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. use PHP_CodeSniffer\Util\Tokens;
  15.  
  16. class InlineCommentSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A list of tokenizers this sniff supports.
  21.      *
  22.      * @var array 
  23.      */
  24.     public $supportedTokenizers = array(
  25.                                    'PHP',
  26.                                    'JS',
  27.                                   );
  28.  
  29.  
  30.     /**
  31.      * Returns an array of tokens this test wants to listen for.
  32.      *
  33.      * @return array 
  34.      */
  35.     public function register()
  36.     {
  37.         return array(
  38.                 T_COMMENT,
  39.                 T_DOC_COMMENT_OPEN_TAG,
  40.                );
  41.  
  42.     }//end register()
  43.  
  44.  
  45.     /**
  46.      * Processes this test, when one of its tokens is encountered.
  47.      *
  48.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  49.      * @param int                  $stackPtr  The position of the current token in the
  50.      *                                         stack passed in $tokens.
  51.      *
  52.      * @return void 
  53.      */
  54.     public function process(File $phpcsFile$stackPtr)
  55.     {
  56.         $tokens $phpcsFile->getTokens();
  57.  
  58.         // If this is a function/class/interface doc block comment, skip it.
  59.         // We are only interested in inline doc block comments, which are
  60.         // not allowed.
  61.         if ($tokens[$stackPtr]['code'=== T_DOC_COMMENT_OPEN_TAG{
  62.             $nextToken $phpcsFile->findNext(
  63.                 Tokens::$emptyTokens,
  64.                 ($stackPtr + 1),
  65.                 null,
  66.                 true
  67.             );
  68.  
  69.             $ignore = array(
  70.                        T_CLASS,
  71.                        T_INTERFACE,
  72.                        T_TRAIT,
  73.                        T_FUNCTION,
  74.                        T_CLOSURE,
  75.                        T_PUBLIC,
  76.                        T_PRIVATE,
  77.                        T_PROTECTED,
  78.                        T_FINAL,
  79.                        T_STATIC,
  80.                        T_ABSTRACT,
  81.                        T_CONST,
  82.                        T_PROPERTY,
  83.                        T_INCLUDE,
  84.                        T_INCLUDE_ONCE,
  85.                        T_REQUIRE,
  86.                        T_REQUIRE_ONCE,
  87.                       );
  88.  
  89.             if (in_array($tokens[$nextToken]['code']$ignore=== true{
  90.                 return;
  91.             }
  92.  
  93.             if ($phpcsFile->tokenizerType === 'JS'{
  94.                 // We allow block comments if a function or object
  95.                 // is being assigned to a variable.
  96.                 $ignore    = Tokens::$emptyTokens;
  97.                 $ignore[]  = T_EQUAL;
  98.                 $ignore[]  = T_STRING;
  99.                 $ignore[]  = T_OBJECT_OPERATOR;
  100.                 $nextToken $phpcsFile->findNext($ignore($nextToken + 1)nulltrue);
  101.                 if ($tokens[$nextToken]['code'=== T_FUNCTION
  102.                     || $tokens[$nextToken]['code'=== T_CLOSURE
  103.                     || $tokens[$nextToken]['code'=== T_OBJECT
  104.                     || $tokens[$nextToken]['code'=== T_PROTOTYPE
  105.                 {
  106.                     return;
  107.                 }
  108.             }
  109.  
  110.             $prevToken $phpcsFile->findPrevious(
  111.                 Tokens::$emptyTokens,
  112.                 ($stackPtr - 1),
  113.                 null,
  114.                 true
  115.             );
  116.  
  117.             if ($tokens[$prevToken]['code'=== T_OPEN_TAG{
  118.                 return;
  119.             }
  120.  
  121.             if ($tokens[$stackPtr]['content'=== '/**'{
  122.                 $error 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead';
  123.                 $phpcsFile->addError($error$stackPtr'DocBlock');
  124.             }
  125.         }//end if
  126.  
  127.         if ($tokens[$stackPtr]['content']{0=== '#'{
  128.             $error 'Perl-style comments are not allowed; use "// Comment" instead';
  129.             $fix   $phpcsFile->addFixableError($error$stackPtr'WrongStyle');
  130.             if ($fix === true{
  131.                 $comment ltrim($tokens[$stackPtr]['content']"# \t");
  132.                 $phpcsFile->fixer->replaceToken($stackPtr"// $comment");
  133.             }
  134.         }
  135.  
  136.         // We don't want end of block comments. If the last comment is a closing
  137.         // curly brace.
  138.         $previousContent $phpcsFile->findPrevious(T_WHITESPACE($stackPtr - 1)nulltrue);
  139.         if ($tokens[$previousContent]['line'=== $tokens[$stackPtr]['line']{
  140.             if ($tokens[$previousContent]['code'=== T_CLOSE_CURLY_BRACKET{
  141.                 return;
  142.             }
  143.  
  144.             // Special case for JS files.
  145.             if ($tokens[$previousContent]['code'=== T_COMMA
  146.                 || $tokens[$previousContent]['code'=== T_SEMICOLON
  147.             {
  148.                 $lastContent $phpcsFile->findPrevious(T_WHITESPACE($previousContent - 1)nulltrue);
  149.                 if ($tokens[$lastContent]['code'=== T_CLOSE_CURLY_BRACKET{
  150.                     return;
  151.                 }
  152.             }
  153.         }
  154.  
  155.         $comment rtrim($tokens[$stackPtr]['content']);
  156.  
  157.         // Only want inline comments.
  158.         if (substr($comment02!== '//'{
  159.             return;
  160.         }
  161.  
  162.         if (trim(substr($comment2)) !== ''{
  163.             $spaceCount = 0;
  164.             $tabFound   = false;
  165.  
  166.             $commentLength strlen($comment);
  167.             for ($i = 2; $i $commentLength$i++{
  168.                 if ($comment[$i=== "\t"{
  169.                     $tabFound = true;
  170.                     break;
  171.                 }
  172.  
  173.                 if ($comment[$i!== ' '{
  174.                     break;
  175.                 }
  176.  
  177.                 $spaceCount++;
  178.             }
  179.  
  180.             $fix = false;
  181.             if ($tabFound === true{
  182.                 $error 'Tab found before comment text; expected "// %s" but found "%s"';
  183.                 $data  = array(
  184.                           ltrim(substr($comment2)),
  185.                           $comment,
  186.                          );
  187.                 $fix   $phpcsFile->addFixableError($error$stackPtr'TabBefore'$data);
  188.             else if ($spaceCount === 0{
  189.                 $error 'No space found before comment text; expected "// %s" but found "%s"';
  190.                 $data  = array(
  191.                           substr($comment2),
  192.                           $comment,
  193.                          );
  194.                 $fix   $phpcsFile->addFixableError($error$stackPtr'NoSpaceBefore'$data);
  195.             else if ($spaceCount > 1{
  196.                 $error 'Expected 1 space before comment text but found %s; use block comment if you need indentation';
  197.                 $data  = array(
  198.                           $spaceCount,
  199.                           substr($comment(2 + $spaceCount)),
  200.                           $comment,
  201.                          );
  202.                 $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingBefore'$data);
  203.             }//end if
  204.  
  205.             if ($fix === true{
  206.                 $newComment '// '.ltrim($tokens[$stackPtr]['content']"/\t ");
  207.                 $phpcsFile->fixer->replaceToken($stackPtr$newComment);
  208.             }
  209.         }//end if
  210.  
  211.         // The below section determines if a comment block is correctly capitalised,
  212.         // and ends in a full-stop. It will find the last comment in a block, and
  213.         // work its way up.
  214.         $nextComment $phpcsFile->findNext(array(T_COMMENT)($stackPtr + 1)nullfalse);
  215.         if (($nextComment !== false)
  216.             && (($tokens[$nextComment]['line']=== ($tokens[$stackPtr]['line'+ 1))
  217.         {
  218.             return;
  219.         }
  220.  
  221.         $topComment  $stackPtr;
  222.         $lastComment $stackPtr;
  223.         while (($topComment $phpcsFile->findPrevious(array(T_COMMENT)($lastComment - 1)nullfalse)) !== false{
  224.             if ($tokens[$topComment]['line'!== ($tokens[$lastComment]['line'- 1)) {
  225.                 break;
  226.             }
  227.  
  228.             $lastComment $topComment;
  229.         }
  230.  
  231.         $topComment  $lastComment;
  232.         $commentText '';
  233.  
  234.         for ($i $topComment$i <= $stackPtr$i++{
  235.             if ($tokens[$i]['code'=== T_COMMENT{
  236.                 $commentText .= trim(substr($tokens[$i]['content']2));
  237.             }
  238.         }
  239.  
  240.         if ($commentText === ''{
  241.             $error 'Blank comments are not allowed';
  242.             $fix   $phpcsFile->addFixableError($error$stackPtr'Empty');
  243.             if ($fix === true{
  244.                 $phpcsFile->fixer->replaceToken($stackPtr'');
  245.             }
  246.  
  247.             return;
  248.         }
  249.  
  250.         if (preg_match('/^\p{Ll}/u'$commentText=== 1{
  251.             $error 'Inline comments must start with a capital letter';
  252.             $phpcsFile->addError($error$topComment'NotCapital');
  253.         }
  254.  
  255.         // Only check the end of comment character if the start of the comment
  256.         // is a letter, indicating that the comment is just standard text.
  257.         if (preg_match('/^\p{L}/u'$commentText=== 1{
  258.             $commentCloser   $commentText[(strlen($commentText- 1)];
  259.             $acceptedClosers = array(
  260.                                 'full-stops'        => '.',
  261.                                 'exclamation marks' => '!',
  262.                                 'or question marks' => '?',
  263.                                );
  264.  
  265.             if (in_array($commentCloser$acceptedClosers=== false{
  266.                 $error 'Inline comments must end in %s';
  267.                 $ender '';
  268.                 foreach ($acceptedClosers as $closerName => $symbol{
  269.                     $ender .= ' '.$closerName.',';
  270.                 }
  271.  
  272.                 $ender trim($ender' ,');
  273.                 $data  = array($ender);
  274.                 $phpcsFile->addError($error$stackPtr'InvalidEndChar'$data);
  275.             }
  276.         }
  277.  
  278.         // Finally, the line below the last comment cannot be empty if this inline
  279.         // comment is on a line by itself.
  280.         if ($tokens[$previousContent]['line'$tokens[$stackPtr]['line']{
  281.             $start = false;
  282.             for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  283.                 if ($tokens[$i]['line'=== ($tokens[$stackPtr]['line'+ 1)) {
  284.                     if ($tokens[$i]['code'!== T_WHITESPACE{
  285.                         return;
  286.                     }
  287.                 else if ($tokens[$i]['line'($tokens[$stackPtr]['line'+ 1)) {
  288.                     break;
  289.                 }
  290.             }
  291.  
  292.             $error 'There must be no blank line following an inline comment';
  293.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingAfter');
  294.             if ($fix === true{
  295.                 $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  296.                 $phpcsFile->fixer->beginChangeset();
  297.                 for ($i ($stackPtr + 1)$i $next$i++{
  298.                     if ($tokens[$i]['line'=== $tokens[$next]['line']{
  299.                         break;
  300.                     }
  301.  
  302.                     $phpcsFile->fixer->replaceToken($i'');
  303.                 }
  304.  
  305.                 $phpcsFile->fixer->endChangeset();
  306.             }
  307.         }//end if
  308.  
  309.     }//end process()
  310.  
  311.  
  312. }//end class

Documentation generated on Mon, 11 Mar 2019 14:49:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.