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_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.                      );
  77.  
  78.         if (isset($ignore[$tokens[$nextToken]['code']]=== false{
  79.             // Could be a file comment.
  80.             $prevToken $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  81.             if ($tokens[$prevToken]['code'!== T_OPEN_TAG{
  82.                 return;
  83.             }
  84.         }
  85.  
  86.         // There must be one space after each star (unless it is an empty comment line)
  87.         // and all the stars must be aligned correctly.
  88.         $requiredColumn ($tokens[$stackPtr]['column'+ 1);
  89.         $endComment     $tokens[$stackPtr]['comment_closer'];
  90.         for ($i ($stackPtr + 1)$i <= $endComment$i++{
  91.             if ($tokens[$i]['code'!== T_DOC_COMMENT_STAR
  92.                 && $tokens[$i]['code'!== T_DOC_COMMENT_CLOSE_TAG
  93.             {
  94.                 continue;
  95.             }
  96.  
  97.             if ($tokens[$i]['code'=== T_DOC_COMMENT_CLOSE_TAG{
  98.                 // Can't process the close tag if it is not the first thing on the line.
  99.                 $prev $phpcsFile->findPrevious(T_DOC_COMMENT_WHITESPACE($i - 1)$stackPtrtrue);
  100.                 if ($tokens[$prev]['line'=== $tokens[$i]['line']{
  101.                     continue;
  102.                 }
  103.             }
  104.  
  105.             if ($tokens[$i]['column'!== $requiredColumn{
  106.                 $error 'Expected %s space(s) before asterisk; %s found';
  107.                 $data  = array(
  108.                           ($requiredColumn - 1),
  109.                           ($tokens[$i]['column'- 1),
  110.                          );
  111.                 $fix   $phpcsFile->addFixableError($error$i'SpaceBeforeStar'$data);
  112.                 if ($fix === true{
  113.                     $padding str_repeat(' '($requiredColumn - 1));
  114.                     if ($tokens[$i]['column'=== 1{
  115.                         $phpcsFile->fixer->addContentBefore($i$padding);
  116.                     else {
  117.                         $phpcsFile->fixer->replaceToken(($i - 1)$padding);
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             if ($tokens[$i]['code'!== T_DOC_COMMENT_STAR{
  123.                 continue;
  124.             }
  125.  
  126.             if ($tokens[($i + 2)]['line'!== $tokens[$i]['line']{
  127.                 // Line is empty.
  128.                 continue;
  129.             }
  130.  
  131.             if ($tokens[($i + 1)]['code'!== T_DOC_COMMENT_WHITESPACE{
  132.                 $error 'Expected 1 space after asterisk; 0 found';
  133.                 $fix   $phpcsFile->addFixableError($error$i'NoSpaceAfterStar');
  134.                 if ($fix === true{
  135.                     $phpcsFile->fixer->addContent($i' ');
  136.                 }
  137.             else if ($tokens[($i + 2)]['code'=== T_DOC_COMMENT_TAG
  138.                 && $tokens[($i + 1)]['content'!== ' '
  139.             {
  140.                 $error 'Expected 1 space after asterisk; %s found';
  141.                 $data  = array(strlen($tokens[($i + 1)]['content']));
  142.                 $fix   $phpcsFile->addFixableError($error$i'SpaceAfterStar'$data);
  143.                 if ($fix === true{
  144.                     $phpcsFile->fixer->replaceToken(($i + 1)' ');
  145.                 }
  146.             }
  147.         }//end for
  148.  
  149.     }//end process()
  150.  
  151.  
  152. }//end class

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