Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'PMAStandard/Sniffs/Commenting/FunctionCommentSniff.php')
-rw-r--r--PMAStandard/Sniffs/Commenting/FunctionCommentSniff.php608
1 files changed, 251 insertions, 357 deletions
diff --git a/PMAStandard/Sniffs/Commenting/FunctionCommentSniff.php b/PMAStandard/Sniffs/Commenting/FunctionCommentSniff.php
index 8852c7700b..0f215d3307 100644
--- a/PMAStandard/Sniffs/Commenting/FunctionCommentSniff.php
+++ b/PMAStandard/Sniffs/Commenting/FunctionCommentSniff.php
@@ -8,80 +8,26 @@
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
- * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
+ * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
-if (class_exists('PHP_CodeSniffer_CommentParser_FunctionCommentParser', true) === false) {
- throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_CommentParser_FunctionCommentParser not found');
-}
-
/**
* Parses and verifies the doc comments for functions.
*
- * Verifies that :
- * <ul>
- * <li>A comment exists</li>
- * <li>There is a blank newline after the short description.</li>
- * <li>There is a blank newline between the long and short description.</li>
- * <li>There is a blank newline between the long description and tags.</li>
- * <li>Parameter names represent those in the method.</li>
- * <li>Parameter comments are in the correct order</li>
- * <li>Parameter comments are complete</li>
- * <li>A space is present before the first and after the last parameter</li>
- * <li>A return type exists</li>
- * <li>There must be one blank line between body and headline comments.</li>
- * <li>Any throw tag must have an exception class.</li>
- * </ul>
- *
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
- * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
- * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
- * @version Release: 1.3.3
+ * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
+ * @version Release: 2.2.0
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class PMAStandard_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSniffer_Sniff
{
- /**
- * The name of the method that we are currently processing.
- *
- * @var string
- */
- private $_methodName = '';
-
- /**
- * The position in the stack where the fucntion token was found.
- *
- * @var int
- */
- private $_functionToken = null;
-
- /**
- * The position in the stack where the class token was found.
- *
- * @var int
- */
- private $_classToken = null;
-
- /**
- * The function comment parser for the current method.
- *
- * @var PHP_CodeSniffer_Comment_Parser_FunctionCommentParser
- */
- protected $commentParser = null;
-
- /**
- * The current PHP_CodeSniffer_File object we are processing.
- *
- * @var PHP_CodeSniffer_File
- */
- protected $currentFile = null;
-
/**
* Returns an array of tokens this test wants to listen for.
@@ -106,385 +52,333 @@ class PMAStandard_Sniffs_Commenting_FunctionCommentSniff implements PHP_CodeSnif
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
- $find = array(
- T_COMMENT,
- T_DOC_COMMENT,
- T_CLASS,
- T_FUNCTION,
- T_OPEN_TAG,
- );
-
- $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1));
-
- if ($commentEnd === false) {
- return;
- }
-
- $this->currentFile = $phpcsFile;
- $tokens = $phpcsFile->getTokens();
-
- // If the token that we found was a class or a function, then this
- // function has no doc comment.
- $code = $tokens[$commentEnd]['code'];
-
- if ($code === T_COMMENT) {
- $error = 'You must use "/**" style comments for a function comment';
- $phpcsFile->addError($error, $stackPtr, 'WrongStyle');
- return;
- } else if ($code !== T_DOC_COMMENT) {
- $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
- return;
- }
-
- // If there is any code between the function keyword and the doc block
- // then the doc block is not for us.
- $ignore = PHP_CodeSniffer_Tokens::$scopeModifiers;
- $ignore[] = T_STATIC;
- $ignore[] = T_WHITESPACE;
- $ignore[] = T_ABSTRACT;
- $ignore[] = T_FINAL;
- $prevToken = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
- if ($prevToken !== $commentEnd) {
- $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
- return;
- }
-
- $this->_functionToken = $stackPtr;
-
- $this->_classToken = null;
- foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condition) {
- if ($condition === T_CLASS || $condition === T_INTERFACE) {
- $this->_classToken = $condPtr;
- break;
- }
- }
-
- // If the first T_OPEN_TAG is right before the comment, it is probably
- // a file comment.
- $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
- $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true);
- if ($tokens[$prevToken]['code'] === T_OPEN_TAG) {
- // Is this the first open tag?
- if ($stackPtr === 0 || $phpcsFile->findPrevious(T_OPEN_TAG, ($prevToken - 1)) === false) {
- $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
- return;
+ $tokens = $phpcsFile->getTokens();
+ $find = PHP_CodeSniffer_Tokens::$methodPrefixes;
+ $find[] = T_WHITESPACE;
+
+ $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
+ if ($tokens[$commentEnd]['code'] === T_COMMENT) {
+ // Inline comments might just be closing comments for
+ // control structures or functions instead of function comments
+ // using the wrong comment type. If there is other code on the line,
+ // assume they relate to that code.
+ $prev = $phpcsFile->findPrevious($find, ($commentEnd - 1), null, true);
+ if ($prev !== false && $tokens[$prev]['line'] === $tokens[$commentEnd]['line']) {
+ $commentEnd = $prev;
}
}
- $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
- $this->_methodName = $phpcsFile->getDeclarationName($stackPtr);
-
- try {
- $this->commentParser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile);
- $this->commentParser->parse();
- } catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
- $line = ($e->getLineWithinComment() + $commentStart);
- $phpcsFile->addError($e->getMessage(), $line, 'FailedParse');
+ if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
+ && $tokens[$commentEnd]['code'] !== T_COMMENT
+ ) {
+ $phpcsFile->addError('Missing function doc comment', $stackPtr, 'Missing');
+ $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'no');
return;
+ } else {
+ $phpcsFile->recordMetric($stackPtr, 'Function has doc comment', 'yes');
}
- $comment = $this->commentParser->getComment();
- if (is_null($comment) === true) {
- $error = 'Function doc comment is empty';
- $phpcsFile->addError($error, $commentStart, 'Empty');
+ if ($tokens[$commentEnd]['code'] === T_COMMENT) {
+ $phpcsFile->addError('You must use "/**" style comments for a function comment', $stackPtr, 'WrongStyle');
return;
}
- $this->processParams($commentStart);
- $this->processReturn($commentStart, $commentEnd);
- $this->processThrows($commentStart);
-
- // No extra newline before short description.
- $short = $comment->getShortComment();
- $newlineCount = 0;
- $newlineSpan = strspn($short, $phpcsFile->eolChar);
- if ($short !== '' && $newlineSpan > 0) {
- $error = 'Extra newline(s) found before function comment short description';
- $phpcsFile->addError($error, ($commentStart + 1), 'SpacingBeforeShort');
+ if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
+ $error = 'There must be no blank lines after the function comment';
+ $phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
}
- $newlineCount = (substr_count($short, $phpcsFile->eolChar) + 1);
-
- // Exactly one blank line between short and long description.
- $long = $comment->getLongComment();
- if (empty($long) === false) {
- $between = $comment->getWhiteSpaceBetween();
- $newlineBetween = substr_count($between, $phpcsFile->eolChar);
- if ($newlineBetween !== 2) {
- $error = 'There must be exactly one blank line between descriptions in function comment';
- $phpcsFile->addError($error, ($commentStart + $newlineCount + 1), 'SpacingAfterShort');
- }
-
- $newlineCount += $newlineBetween;
- }
-
- // Exactly one blank line before tags.
- $params = $this->commentParser->getTagOrders();
- if (count($params) > 1) {
- $newlineSpan = $comment->getNewlineAfter();
- if ($newlineSpan !== 2) {
- $error = 'There must be exactly one blank line before the tags in function comment';
- if ($long !== '') {
- $newlineCount += (substr_count($long, $phpcsFile->eolChar) - $newlineSpan + 1);
+ $commentStart = $tokens[$commentEnd]['comment_opener'];
+ foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
+ if ($tokens[$tag]['content'] === '@see') {
+ // Make sure the tag isn't empty.
+ $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
+ if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
+ $error = 'Content missing for @see tag in function comment';
+ $phpcsFile->addError($error, $tag, 'EmptySees');
}
-
- $phpcsFile->addError($error, ($commentStart + $newlineCount), 'SpacingBeforeTags');
- $short = rtrim($short, $phpcsFile->eolChar.' ');
}
}
+ $this->processReturn($phpcsFile, $stackPtr, $commentStart);
+ $this->processThrows($phpcsFile, $stackPtr, $commentStart);
+ $this->processParams($phpcsFile, $stackPtr, $commentStart);
+
}//end process()
/**
- * Process any throw tags that this function comment has.
+ * Process the return comment of this function comment.
*
- * @param int $commentStart The position in the stack where the
- * comment started.
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token
+ * in the stack passed in $tokens.
+ * @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
- protected function processThrows($commentStart)
+ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
- if (count($this->commentParser->getThrows()) === 0) {
- return;
- }
-
- foreach ($this->commentParser->getThrows() as $throw) {
+ $tokens = $phpcsFile->getTokens();
- $exception = $throw->getValue();
- $errorPos = ($commentStart + $throw->getLine());
+ // Skip constructor and destructor.
+ $methodName = $phpcsFile->getDeclarationName($stackPtr);
+ $isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct');
+
+ $return = null;
+ foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
+ if ($tokens[$tag]['content'] === '@return') {
+ if ($return !== null) {
+ $error = 'Only 1 @return tag is allowed in a function comment';
+ $phpcsFile->addError($error, $tag, 'DuplicateReturn');
+ return;
+ }
- if ($exception === '') {
- $error = '@throws tag must contain the exception class name';
- $this->currentFile->addError($error, $errorPos, 'EmptyThrows');
+ $return = $tag;
}
}
- }//end processThrows()
-
-
- /**
- * Process the return comment of this function comment.
- *
- * @param int $commentStart The position in the stack where the comment started.
- * @param int $commentEnd The position in the stack where the comment ended.
- *
- * @return void
- */
- protected function processReturn($commentStart, $commentEnd)
- {
- // Skip constructor and destructor.
- $className = '';
- if ($this->_classToken !== null) {
- $className = $this->currentFile->getDeclarationName($this->_classToken);
- $className = strtolower(ltrim($className, '_'));
+ if ($isSpecialMethod === true) {
+ return;
}
- $methodName = strtolower(ltrim($this->_methodName, '_'));
- $isSpecialMethod = ($this->_methodName === '__construct' || $this->_methodName === '__destruct');
-
- if ($isSpecialMethod === false && $methodName !== $className) {
- // Report missing return tag.
- if ($this->commentParser->getReturn() === null) {
- $error = 'Missing @return tag in function comment';
- $this->currentFile->addError($error, $commentEnd, 'MissingReturn');
- } else if (trim($this->commentParser->getReturn()->getRawContent()) === '') {
- $error = '@return tag is empty in function comment';
- $errorPos = ($commentStart + $this->commentParser->getReturn()->getLine());
- $this->currentFile->addError($error, $errorPos, 'EmptyReturn');
+ if ($return !== null) {
+ $content = $tokens[($return + 2)]['content'];
+ if (empty($content) === true || $tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) {
+ $error = 'Return type missing for @return tag in function comment';
+ $phpcsFile->addError($error, $return, 'MissingReturnType');
}
- }
+ } else {
+ $error = 'Missing @return tag in function comment';
+ $phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
+ }//end if
}//end processReturn()
/**
- * Process the function parameter comments.
+ * Process any throw tags that this function comment has.
*
- * @param int $commentStart The position in the stack where
- * the comment started.
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token
+ * in the stack passed in $tokens.
+ * @param int $commentStart The position in the stack where the comment started.
*
* @return void
*/
- protected function processParams($commentStart)
+ protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
- $realParams = $this->currentFile->getMethodParameters($this->_functionToken);
+ $tokens = $phpcsFile->getTokens();
- $params = $this->commentParser->getParams();
- $foundParams = array();
-
- if (empty($params) === false) {
-
- $lastParm = (count($params) - 1);
- if (substr_count($params[$lastParm]->getWhitespaceAfter(), $this->currentFile->eolChar) !== 2) {
- $error = 'Last parameter comment requires a blank newline after it';
- $errorPos = ($params[$lastParm]->getLine() + $commentStart);
- $this->currentFile->addError($error, $errorPos, 'SpacingAfterParams');
+ $throws = array();
+ foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
+ if ($tokens[$tag]['content'] !== '@throws') {
+ continue;
}
- // Parameters must appear immediately after the comment.
- if ($params[0]->getOrder() !== 2) {
- $error = 'Parameters must appear immediately after the comment';
- $errorPos = ($params[0]->getLine() + $commentStart);
- $this->currentFile->addError($error, $errorPos, 'SpacingBeforeParams');
+ $exception = null;
+ $comment = null;
+ if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
+ $matches = array();
+ preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches);
+ $exception = $matches[1];
+ if (isset($matches[2]) === true) {
+ $comment = $matches[2];
+ }
}
- $previousParam = null;
- $spaceBeforeVar = 10000;
- $spaceBeforeComment = 10000;
- $longestType = 0;
- $longestVar = 0;
-
- foreach ($params as $param) {
-
- $paramComment = trim($param->getComment());
- $errorPos = ($param->getLine() + $commentStart);
+ if ($exception === null) {
+ $error = 'Exception type missing for @throws tag in function comment';
+ $phpcsFile->addError($error, $tag, 'InvalidThrows');
+ }
+ }//end foreach
- // Make sure that there is only one space before the var type.
- if ($param->getWhitespaceBeforeType() !== ' ') {
- $error = 'Expected 1 space before variable type';
- $this->currentFile->addError($error, $errorPos, 'SpacingBeforeParamType');
- }
+ }//end processThrows()
- $spaceCount = substr_count($param->getWhitespaceBeforeVarName(), ' ');
- if ($spaceCount < $spaceBeforeVar) {
- $spaceBeforeVar = $spaceCount;
- $longestType = $errorPos;
- }
- $spaceCount = substr_count($param->getWhitespaceBeforeComment(), ' ');
+ /**
+ * Process the function parameter comments.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token
+ * in the stack passed in $tokens.
+ * @param int $commentStart The position in the stack where the comment started.
+ *
+ * @return void
+ */
+ protected function processParams(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
+ {
+ $tokens = $phpcsFile->getTokens();
+
+ $params = array();
+ $maxType = 0;
+ $maxVar = 0;
+ foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
+ if ($tokens[$tag]['content'] !== '@param') {
+ continue;
+ }
- if ($spaceCount < $spaceBeforeComment && $paramComment !== '') {
- $spaceBeforeComment = $spaceCount;
- $longestVar = $errorPos;
+ $type = '';
+ $typeSpace = 0;
+ $var = '';
+ $varSpace = 0;
+ $comment = '';
+ if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
+ $matches = array();
+ preg_match('/([^$&]+)(?:((?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
+ $typeLen = strlen($matches[1]);
+ $type = trim($matches[1]);
+ $typeSpace = ($typeLen - strlen($type));
+ $typeLen = strlen($type);
+ if ($typeLen > $maxType) {
+ $maxType = $typeLen;
}
- // Make sure they are in the correct order,
- // and have the correct name.
- $pos = $param->getPosition();
-
- $paramName = ($param->getVarName() !== '') ? $param->getVarName() : '[ UNKNOWN ]';
-
- if ($previousParam !== null) {
- $previousName = ($previousParam->getVarName() !== '') ? $previousParam->getVarName() : 'UNKNOWN';
-
- // Check to see if the parameters align properly.
- if ($param->alignsVariableWith($previousParam) === false) {
- $error = 'The variable names for parameters %s (%s) and %s (%s) do not align';
- $data = array(
- $previousName,
- ($pos - 1),
- $paramName,
- $pos,
- );
- $this->currentFile->addError($error, $errorPos, 'ParameterNamesNotAligned', $data);
+ if (isset($matches[2]) === true) {
+ $var = $matches[2];
+ $varLen = strlen($var);
+ if ($varLen > $maxVar) {
+ $maxVar = $varLen;
}
- if ($param->alignsCommentWith($previousParam) === false) {
- $error = 'The comments for parameters %s (%s) and %s (%s) do not align';
- $data = array(
- $previousName,
- ($pos - 1),
- $paramName,
- $pos,
- );
- $this->currentFile->addError($error, $errorPos, 'ParameterCommentsNotAligned', $data);
- }
- }//end if
+ if (isset($matches[4]) === true) {
+ $varSpace = strlen($matches[3]);
+ $comment = $matches[4];
- // Make sure the names of the parameter comment matches the
- // actual parameter.
- if (isset($realParams[($pos - 1)]) === true) {
- $realName = $realParams[($pos - 1)]['name'];
- $foundParams[] = $realName;
-
- // Append ampersand to name if passing by reference.
- if ($realParams[($pos - 1)]['pass_by_reference'] === true) {
- $realName = '&'.$realName;
- }
-
- if ($realName !== $paramName) {
- $code = 'ParamNameNoMatch';
- $data = array(
- $paramName,
- $realName,
- $pos,
- );
-
- $error = 'Doc comment for var %s does not match ';
- if (strtolower($paramName) === strtolower($realName)) {
- $error .= 'case of ';
- $code = 'ParamNameNoCaseMatch';
+ // Any strings until the next tag belong to this comment.
+ if (isset($tokens[$commentStart]['comment_tags'][($pos + 1)]) === true) {
+ $end = $tokens[$commentStart]['comment_tags'][($pos + 1)];
+ } else {
+ $end = $tokens[$commentStart]['comment_closer'];
}
- $error .= 'actual variable name %s at position %s';
-
- $this->currentFile->addError($error, $errorPos, $code, $data);
+ for ($i = ($tag + 3); $i < $end; $i++) {
+ if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
+ $comment .= ' '.$tokens[$i]['content'];
+ }
+ }
+ } else {
+ $error = 'Missing parameter comment';
+ $phpcsFile->addError($error, $tag, 'MissingParamComment');
}
} else {
- // We must have an extra parameter comment.
- $error = 'Superfluous doc comment at position '.$pos;
- $this->currentFile->addError($error, $errorPos, 'ExtraParamComment');
- }
-
- if ($param->getVarName() === '') {
- $error = 'Missing parameter name at position '.$pos;
- $this->currentFile->addError($error, $errorPos, 'MissingParamName');
- }
+ $error = 'Missing parameter name';
+ $phpcsFile->addError($error, $tag, 'MissingParamName');
+ }//end if
+ } else {
+ $error = 'Missing parameter type';
+ $phpcsFile->addError($error, $tag, 'MissingParamType');
+ }//end if
+
+ $params[] = array(
+ 'tag' => $tag,
+ 'type' => $type,
+ 'var' => $var,
+ 'comment' => $comment,
+ 'type_space' => $typeSpace,
+ 'var_space' => $varSpace,
+ );
+ }//end foreach
+
+ $realParams = $phpcsFile->getMethodParameters($stackPtr);
+ $foundParams = array();
+ foreach ($params as $pos => $param) {
+ if ($param['var'] === '') {
+ continue;
+ }
- if ($param->getType() === '') {
- $error = 'Missing type at position '.$pos;
- $this->currentFile->addError($error, $errorPos, 'MissingParamType');
+ $foundParams[] = $param['var'];
+
+ // Check number of spaces after the type.
+ $spaces = ($maxType - strlen($param['type']) + 1);
+ if ($param['type_space'] !== $spaces) {
+ $error = 'Expected %s spaces after parameter type; %s found';
+ $data = array(
+ $spaces,
+ $param['type_space'],
+ );
+
+ $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamType', $data);
+ if ($fix === true) {
+ $content = $param['type'];
+ $content .= str_repeat(' ', $spaces);
+ $content .= $param['var'];
+ $content .= str_repeat(' ', $param['var_space']);
+ $content .= $param['comment'];
+ $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
}
+ }
- if ($paramComment === '') {
- $error = 'Missing comment for param "%s" at position %s';
- $data = array(
- $paramName,
- $pos,
- );
- $this->currentFile->addError($error, $errorPos, 'MissingParamComment', $data);
+ // Make sure the param name is correct.
+ if (isset($realParams[$pos]) === true) {
+ // PMAStandard tweak for pass by reference variable:
+ // Variable should have '&' in doc comment
+ if ($realParams[$pos]['pass_by_reference'] === true) {
+ $realParams[$pos]['name'] = '&' . $realParams[$pos]['name'];
}
+ $realName = $realParams[$pos]['name'];
+ if ($realName !== $param['var']) {
+ $code = 'ParamNameNoMatch';
+ $data = array(
+ $param['var'],
+ $realName,
+ );
+
+ $error = 'Doc comment for parameter %s does not match ';
+ if (strtolower($param['var']) === strtolower($realName)) {
+ $error .= 'case of ';
+ $code = 'ParamNameNoCaseMatch';
+ }
- $previousParam = $param;
-
- }//end foreach
+ $error .= 'actual variable name %s';
- if ($spaceBeforeVar !== 1 && $spaceBeforeVar !== 10000 && $spaceBeforeComment !== 10000) {
- $error = 'Expected 1 space after the longest type';
- $this->currentFile->addError($error, $longestType, 'SpacingAfterLongType');
+ $phpcsFile->addError($error, $param['tag'], $code, $data);
+ }
+ } else if (substr($param['var'], -4) !== ',...') {
+ // We must have an extra parameter comment.
+ $error = 'Superfluous parameter comment';
+ $phpcsFile->addError($error, $param['tag'], 'ExtraParamComment');
+ }//end if
+
+ if ($param['comment'] === '') {
+ continue;
}
- if ($spaceBeforeComment !== 1 && $spaceBeforeComment !== 10000) {
- $error = 'Expected 1 space after the longest variable name';
- $this->currentFile->addError($error, $longestVar, 'SpacingAfterLongName');
+ // Check number of spaces after the var name.
+ $spaces = ($maxVar - strlen($param['var']) + 1);
+ if ($param['var_space'] !== $spaces) {
+ $error = 'Expected %s spaces after parameter name; %s found';
+ $data = array(
+ $spaces,
+ $param['var_space'],
+ );
+
+ $fix = $phpcsFile->addFixableError($error, $param['tag'], 'SpacingAfterParamName', $data);
+ if ($fix === true) {
+ $content = $param['type'];
+ $content .= str_repeat(' ', $param['type_space']);
+ $content .= $param['var'];
+ $content .= str_repeat(' ', $spaces);
+ $content .= $param['comment'];
+ $phpcsFile->fixer->replaceToken(($param['tag'] + 2), $content);
+ }
}
-
- }//end if
+ }//end foreach
$realNames = array();
foreach ($realParams as $realParam) {
$realNames[] = $realParam['name'];
}
- // Report and missing comments.
+ // Report missing comments.
$diff = array_diff($realNames, $foundParams);
foreach ($diff as $neededParam) {
- if (count($params) !== 0) {
- $errorPos = ($params[(count($params) - 1)]->getLine() + $commentStart);
- } else {
- $errorPos = $commentStart;
- }
-
- $error = 'Doc comment for "%s" missing';
+ $error = 'Doc comment for parameter "%s" missing';
$data = array($neededParam);
- $this->currentFile->addError($error, $errorPos, 'MissingParamTag', $data);
+ $phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data);
}
}//end processParams()
}//end class
-
-?>