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

Source for file VariableCommentSniff.php

Documentation is available at VariableCommentSniff.php

  1. <?php
  2. /**
  3.  * Parses and verifies the variable doc comment.
  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\AbstractVariableSniff;
  13. use PHP_CodeSniffer\Files\File;
  14. use PHP_CodeSniffer\Util\Common;
  15.  
  16. class VariableCommentSniff extends AbstractVariableSniff
  17. {
  18.  
  19.  
  20.     /**
  21.      * Called to process class member vars.
  22.      *
  23.      * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  24.      * @param int                  $stackPtr  The position of the current token
  25.      *                                         in the stack passed in $tokens.
  26.      *
  27.      * @return void 
  28.      */
  29.     public function processMemberVar(File $phpcsFile$stackPtr)
  30.     {
  31.         $tokens       $phpcsFile->getTokens();
  32.         $commentToken = array(
  33.                          T_COMMENT,
  34.                          T_DOC_COMMENT_CLOSE_TAG,
  35.                         );
  36.  
  37.         $commentEnd $phpcsFile->findPrevious($commentToken$stackPtr);
  38.         if ($commentEnd === false{
  39.             $phpcsFile->addError('Missing member variable doc comment'$stackPtr'Missing');
  40.             return;
  41.         }
  42.  
  43.         if ($tokens[$commentEnd]['code'=== T_COMMENT{
  44.             $phpcsFile->addError('You must use "/**" style comments for a member variable comment'$stackPtr'WrongStyle');
  45.             return;
  46.         else if ($tokens[$commentEnd]['code'!== T_DOC_COMMENT_CLOSE_TAG{
  47.             $phpcsFile->addError('Missing member variable doc comment'$stackPtr'Missing');
  48.             return;
  49.         else {
  50.             // Make sure the comment we have found belongs to us.
  51.             $commentFor $phpcsFile->findNext(array(T_VARIABLET_CLASST_INTERFACE)($commentEnd + 1));
  52.             if ($commentFor !== $stackPtr{
  53.                 $phpcsFile->addError('Missing member variable doc comment'$stackPtr'Missing');
  54.                 return;
  55.             }
  56.         }
  57.  
  58.         $commentStart $tokens[$commentEnd]['comment_opener'];
  59.  
  60.         $foundVar = null;
  61.         foreach ($tokens[$commentStart]['comment_tags'as $tag{
  62.             if ($tokens[$tag]['content'=== '@var'{
  63.                 if ($foundVar !== null{
  64.                     $error 'Only one @var tag is allowed in a member variable comment';
  65.                     $phpcsFile->addError($error$tag'DuplicateVar');
  66.                 else {
  67.                     $foundVar $tag;
  68.                 }
  69.             else if ($tokens[$tag]['content'=== '@see'{
  70.                 // Make sure the tag isn't empty.
  71.                 $string $phpcsFile->findNext(T_DOC_COMMENT_STRING$tag$commentEnd);
  72.                 if ($string === false || $tokens[$string]['line'!== $tokens[$tag]['line']{
  73.                     $error 'Content missing for @see tag in member variable comment';
  74.                     $phpcsFile->addError($error$tag'EmptySees');
  75.                 }
  76.             else {
  77.                 $error '%s tag is not allowed in member variable comment';
  78.                 $data  = array($tokens[$tag]['content']);
  79.                 $phpcsFile->addWarning($error$tag'TagNotAllowed'$data);
  80.             }//end if
  81.         }//end foreach
  82.  
  83.         // The @var tag is the only one we require.
  84.         if ($foundVar === null{
  85.             $error 'Missing @var tag in member variable comment';
  86.             $phpcsFile->addError($error$commentEnd'MissingVar');
  87.             return;
  88.         }
  89.  
  90.         $firstTag $tokens[$commentStart]['comment_tags'][0];
  91.         if ($foundVar !== null && $tokens[$firstTag]['content'!== '@var'{
  92.             $error 'The @var tag must be the first tag in a member variable comment';
  93.             $phpcsFile->addError($error$foundVar'VarOrder');
  94.         }
  95.  
  96.         // Make sure the tag isn't empty and has the correct padding.
  97.         $string $phpcsFile->findNext(T_DOC_COMMENT_STRING$foundVar$commentEnd);
  98.         if ($string === false || $tokens[$string]['line'!== $tokens[$foundVar]['line']{
  99.             $error 'Content missing for @var tag in member variable comment';
  100.             $phpcsFile->addError($error$foundVar'EmptyVar');
  101.             return;
  102.         }
  103.  
  104.         $varType       $tokens[($foundVar + 2)]['content'];
  105.         $suggestedType = Common::suggestType($varType);
  106.         if ($varType !== $suggestedType{
  107.             $error 'Expected "%s" but found "%s" for @var tag in member variable comment';
  108.             $data  = array(
  109.                       $suggestedType,
  110.                       $varType,
  111.                      );
  112.             $phpcsFile->addError($error($foundVar + 2)'IncorrectVarType'$data);
  113.         }
  114.  
  115.     }//end processMemberVar()
  116.  
  117.  
  118.     /**
  119.      * Called to process a normal variable.
  120.      *
  121.      * Not required for this sniff.
  122.      *
  123.      * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
  124.      * @param int                  $stackPtr  The position where the double quoted
  125.      *                                         string was found.
  126.      *
  127.      * @return void 
  128.      */
  129.     protected function processVariable(File $phpcsFile$stackPtr)
  130.     {
  131.  
  132.     }//end processVariable()
  133.  
  134.  
  135.     /**
  136.      * Called to process variables found in double quoted strings.
  137.      *
  138.      * Not required for this sniff.
  139.      *
  140.      * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this token was found.
  141.      * @param int                  $stackPtr  The position where the double quoted
  142.      *                                         string was found.
  143.      *
  144.      * @return void 
  145.      */
  146.     protected function processVariableInString(File $phpcsFile$stackPtr)
  147.     {
  148.  
  149.     }//end processVariableInString()
  150.  
  151.  
  152. }//end class

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