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

Source for file DocCommentAlignmentSniff.php

Documentation is available at DocCommentAlignmentSniff.php

  1. <?php
  2. /**
  3.  * Tests that the stars in a doc comment align correctly.
  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 DocCommentAlignmentSniff 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(T_DOC_COMMENT_OPEN_TAG);
  38.  
  39.     }//end register()
  40.  
  41.  
  42.     /**
  43.      * Processes this test, when one of its tokens is encountered.
  44.      *
  45.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  46.      * @param int                         $stackPtr  The position of the current token
  47.      *                                                in the stack passed in $tokens.
  48.      *
  49.      * @return void 
  50.      */
  51.     public function process(File $phpcsFile$stackPtr)
  52.     {
  53.         $tokens $phpcsFile->getTokens();
  54.  
  55.         // We are only interested in function/class/interface doc block comments.
  56.         $ignore = Tokens::$emptyTokens;
  57.         if ($phpcsFile->tokenizerType === 'JS'{
  58.             $ignore[= T_EQUAL;
  59.             $ignore[= T_STRING;
  60.             $ignore[= T_OBJECT_OPERATOR;
  61.         }
  62.  
  63.         $nextToken $phpcsFile->findNext($ignore($stackPtr + 1)nulltrue);
  64.         $ignore    = array(
  65.                       T_CLASS     => true,
  66.                       T_INTERFACE => true,
  67.                       T_FUNCTION  => true,
  68.                       T_PUBLIC    => true,
  69.                       T_PRIVATE   => true,
  70.                       T_PROTECTED => true,
  71.                       T_STATIC    => true,
  72.                       T_ABSTRACT  => true,
  73.                       T_PROPERTY  => true,
  74.                       T_OBJECT    => true,
  75.                       T_PROTOTYPE => true,
  76.                       T_VAR       => true,
  77.                      );
  78.  
  79.         if (isset($ignore[$tokens[$nextToken]['code']]=== false{
  80.             // Could be a file comment.
  81.             $prevToken $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  82.             if ($tokens[$prevToken]['code'!== T_OPEN_TAG{
  83.                 return;
  84.             }
  85.         }
  86.  
  87.         // There must be one space after each star (unless it is an empty comment line)
  88.         // and all the stars must be aligned correctly.
  89.         $requiredColumn ($tokens[$stackPtr]['column'+ 1);
  90.         $endComment     $tokens[$stackPtr]['comment_closer'];
  91.         for ($i ($stackPtr + 1)$i <= $endComment$i++{
  92.             if ($tokens[$i]['code'!== T_DOC_COMMENT_STAR
  93.                 && $tokens[$i]['code'!== T_DOC_COMMENT_CLOSE_TAG
  94.             {
  95.                 continue;
  96.             }
  97.  
  98.             if ($tokens[$i]['code'=== T_DOC_COMMENT_CLOSE_TAG{
  99.                 // Can't process the close tag if it is not the first thing on the line.
  100.                 $prev $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE($i - 1)$stackPtrtrue);
  101.                 if ($tokens[$prev]['line'=== $tokens[$i]['line']{
  102.                     continue;
  103.                 }
  104.             }
  105.  
  106.             if ($tokens[$i]['column'!== $requiredColumn{
  107.                 $error 'Expected %s space(s) before asterisk; %s found';
  108.                 $data  = array(
  109.                           ($requiredColumn - 1),
  110.                           ($tokens[$i]['column'- 1),
  111.                          );
  112.                 $fix   $phpcsFile->addFixableError($error$i'SpaceBeforeStar'$data);
  113.                 if ($fix === true{
  114.                     $padding str_repeat(' '($requiredColumn - 1));
  115.                     if ($tokens[$i]['column'=== 1{
  116.                         $phpcsFile->fixer->addContentBefore($i$padding);
  117.                     else {
  118.                         $phpcsFile->fixer->replaceToken(($i - 1)$padding);
  119.                     }
  120.                 }
  121.             }
  122.  
  123.             if ($tokens[$i]['code'!== T_DOC_COMMENT_STAR{
  124.                 continue;
  125.             }
  126.  
  127.             if ($tokens[($i + 2)]['line'!== $tokens[$i]['line']{
  128.                 // Line is empty.
  129.                 continue;
  130.             }
  131.  
  132.             if ($tokens[($i + 1)]['code'!== T_DOC_COMMENT_WHITESPACE{
  133.                 $error 'Expected 1 space after asterisk; 0 found';
  134.                 $fix   $phpcsFile->addFixableError($error$i'NoSpaceAfterStar');
  135.                 if ($fix === true{
  136.                     $phpcsFile->fixer->addContent($i' ');
  137.                 }
  138.             else if ($tokens[($i + 2)]['code'=== T_DOC_COMMENT_TAG
  139.                 && $tokens[($i + 1)]['content'!== ' '
  140.             {
  141.                 $error 'Expected 1 space after asterisk; %s found';
  142.                 $data  = array(strlen($tokens[($i + 1)]['content']));
  143.                 $fix   $phpcsFile->addFixableError($error$i'SpaceAfterStar'$data);
  144.                 if ($fix === true{
  145.                     $phpcsFile->fixer->replaceToken(($i + 1)' ');
  146.                 }
  147.             }
  148.         }//end for
  149.  
  150.     }//end process()
  151.  
  152.  
  153. }//end class

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