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\Files\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(T_COMMENT($stackPtr + 1)nullfalse);
  215.         if ($nextComment !== false
  216.             && $tokens[$nextComment]['line'=== ($tokens[$stackPtr]['line'+ 1)
  217.         {
  218.             $nextNonWhitespace $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)$nextCommenttrue);
  219.             if ($nextNonWhitespace === false{
  220.                 return;
  221.             }
  222.         }
  223.  
  224.         $topComment  $stackPtr;
  225.         $lastComment $stackPtr;
  226.         while (($topComment $phpcsFile->findPrevious(array(T_COMMENT)($lastComment - 1)nullfalse)) !== false{
  227.             if ($tokens[$topComment]['line'!== ($tokens[$lastComment]['line'- 1)) {
  228.                 break;
  229.             }
  230.  
  231.             $nextNonWhitespace $phpcsFile->findNext(T_WHITESPACE($topComment + 1)$lastCommenttrue);
  232.             if ($nextNonWhitespace !== false{
  233.                 break;
  234.             }
  235.  
  236.             $lastComment $topComment;
  237.         }
  238.  
  239.         $topComment  $lastComment;
  240.         $commentText '';
  241.  
  242.         for ($i $topComment$i <= $stackPtr$i++{
  243.             if ($tokens[$i]['code'=== T_COMMENT{
  244.                 $commentText .= trim(substr($tokens[$i]['content']2));
  245.             }
  246.         }
  247.  
  248.         if ($commentText === ''{
  249.             $error 'Blank comments are not allowed';
  250.             $fix   $phpcsFile->addFixableError($error$stackPtr'Empty');
  251.             if ($fix === true{
  252.                 $phpcsFile->fixer->replaceToken($stackPtr'');
  253.             }
  254.  
  255.             return;
  256.         }
  257.  
  258.         if (preg_match('/^\p{Ll}/u'$commentText=== 1{
  259.             $error 'Inline comments must start with a capital letter';
  260.             $phpcsFile->addError($error$topComment'NotCapital');
  261.         }
  262.  
  263.         // Only check the end of comment character if the start of the comment
  264.         // is a letter, indicating that the comment is just standard text.
  265.         if (preg_match('/^\p{L}/u'$commentText=== 1{
  266.             $commentCloser   $commentText[(strlen($commentText- 1)];
  267.             $acceptedClosers = array(
  268.                                 'full-stops'        => '.',
  269.                                 'exclamation marks' => '!',
  270.                                 'or question marks' => '?',
  271.                                );
  272.  
  273.             if (in_array($commentCloser$acceptedClosers=== false{
  274.                 $error 'Inline comments must end in %s';
  275.                 $ender '';
  276.                 foreach ($acceptedClosers as $closerName => $symbol{
  277.                     $ender .= ' '.$closerName.',';
  278.                 }
  279.  
  280.                 $ender trim($ender' ,');
  281.                 $data  = array($ender);
  282.                 $phpcsFile->addError($error$stackPtr'InvalidEndChar'$data);
  283.             }
  284.         }
  285.  
  286.         // Finally, the line below the last comment cannot be empty if this inline
  287.         // comment is on a line by itself.
  288.         if ($tokens[$previousContent]['line'$tokens[$stackPtr]['line']{
  289.             $next $phpcsFile->findNext(T_WHITESPACE($stackPtr + 1)nulltrue);
  290.             if ($next === false{
  291.                 // Ignore if the comment is the last non-whitespace token in a file.
  292.                 return;
  293.             }
  294.  
  295.             $start = false;
  296.             for ($i ($stackPtr + 1)$i $phpcsFile->numTokens; $i++{
  297.                 if ($tokens[$i]['line'=== ($tokens[$stackPtr]['line'+ 1)) {
  298.                     if ($tokens[$i]['code'!== T_WHITESPACE{
  299.                         return;
  300.                     }
  301.                 else if ($tokens[$i]['line'($tokens[$stackPtr]['line'+ 1)) {
  302.                     break;
  303.                 }
  304.             }
  305.  
  306.             $error 'There must be no blank line following an inline comment';
  307.             $fix   $phpcsFile->addFixableError($error$stackPtr'SpacingAfter');
  308.             if ($fix === true{
  309.                 $phpcsFile->fixer->beginChangeset();
  310.                 for ($i ($stackPtr + 1)$i $next$i++{
  311.                     if ($tokens[$i]['line'=== $tokens[$next]['line']{
  312.                         break;
  313.                     }
  314.  
  315.                     $phpcsFile->fixer->replaceToken($i'');
  316.                 }
  317.  
  318.                 $phpcsFile->fixer->endChangeset();
  319.             }
  320.         }//end if
  321.  
  322.     }//end process()
  323.  
  324.  
  325. }//end class

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