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

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