Files
archived-phpruntests/cs/documentation/Sniffs/VariableCommentSniff.php
2009-04-15 16:30:27 +00:00

158 lines
6.2 KiB
PHP

<?php
class documentation_Sniffs_VariableCommentSniff extends Squiz_Sniffs_Commenting_VariableCommentSniff
{
public function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$this->currentFile = $phpcsFile;
$tokens = $phpcsFile->getTokens();
$commentToken = array(
T_COMMENT,
T_DOC_COMMENT,
);
// Extract the var comment docblock.
$commentEnd = $phpcsFile->findPrevious($commentToken, ($stackPtr - 3));
if ($commentEnd !== false && $tokens[$commentEnd]['code'] === T_COMMENT) {
$phpcsFile->addError('You must use "/**" style comments for a variable comment', $stackPtr);
return;
} else if ($commentEnd === false || $tokens[$commentEnd]['code'] !== T_DOC_COMMENT) {
$phpcsFile->addError('Missing variable doc comment', $stackPtr);
return;
} else {
// Make sure the comment we have found belongs to us.
$commentFor = $phpcsFile->findNext(array(T_VARIABLE, T_CLASS, T_INTERFACE), ($commentEnd + 1));
if ($commentFor !== $stackPtr) {
$phpcsFile->addError('Missing variable doc comment', $stackPtr);
return;
}
}
$commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1);
$comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1));
// Parse the header comment docblock.
try {
$this->commentParser = new PHP_CodeSniffer_CommentParser_MemberCommentParser($comment, $phpcsFile);
$this->commentParser->parse();
} catch (PHP_CodeSniffer_CommentParser_ParserException $e) {
$line = ($e->getLineWithinComment() + $commentStart);
$phpcsFile->addError($e->getMessage(), $line);
return;
}
$comment = $this->commentParser->getComment();
if (is_null($comment) === true) {
$error = 'Variable doc comment is empty';
$phpcsFile->addError($error, $commentStart);
return;
}
// Check for a comment description.
$short = $comment->getShortComment();
$long = '';
if (trim($short) === '') {
$newlineCount = 1;
} else {
// No extra newline before short description.
$newlineCount = 0;
$newlineSpan = strspn($short, $phpcsFile->eolChar);
$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);
$newlineCount += $newlineBetween;
$testLong = trim($long);
if (preg_match('|[A-Z]|', $testLong[0]) === 0) {
$error = 'Variable comment long description must start with a capital letter';
$phpcsFile->addError($error, ($commentStart + $newlineCount));
}
}//end if
// Short description must be single line and end with a full stop.
$testShort = trim($short);
$lastChar = $testShort[(strlen($testShort) - 1)];
if (substr_count($testShort, $phpcsFile->eolChar) !== 0) {
}
if (preg_match('|[A-Z]|', $testShort[0]) === 0) {
}
if ($lastChar !== '.') {
}
}//end if
// Exactly one blank line before tags.
$tags = $this->commentParser->getTagOrders();
if (count($tags) > 1) {
$newlineSpan = $comment->getNewlineAfter();
if ($newlineSpan !== 2) {
$short = rtrim($short, $phpcsFile->eolChar.' ');
}
}
// Check for unknown/deprecated tags.
$unknownTags = $this->commentParser->getUnknown();
foreach ($unknownTags as $errorTag) {
// Unknown tags are not parsed, do not process further.
$error = "@$errorTag[tag] tag is not allowed in variable comment";
$phpcsFile->addWarning($error, ($commentStart + $errorTag['line']));
}
}//end processMemberVar()
protected function processVar($commentStart, $commentEnd)
{
$var = $this->commentParser->getVar();
if ($var !== null) {
$errorPos = ($commentStart + $var->getLine());
$index = array_keys($this->commentParser->getTagOrders(), 'var');
if (count($index) > 1) {
$error = 'Only 1 @var tag is allowed in variable comment';
$this->currentFile->addError($error, $errorPos);
return;
}
if ($index[0] !== 1) {
$error = 'The @var tag must be the first tag in a variable comment';
$this->currentFile->addError($error, $errorPos);
}
$content = $var->getContent();
if (empty($content) === true) {
$error = 'Var type missing for @var tag in variable comment';
$this->currentFile->addError($error, $errorPos);
return;
} else {
$suggestedType = PHP_CodeSniffer::suggestType($content);
if ($content !== $suggestedType) {
$error = "Expected \"$suggestedType\"; found \"$content\" for @var tag in variable comment";
$this->currentFile->addError($error, $errorPos);
}
}
$spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
if ($spacing !== 3) {
$error = '@var tag indented incorrectly. ';
$error .= "Expected 3 spaces but found $spacing.";
$this->currentFile->addError($error, $errorPos);
}
} else {
$error = 'Missing @var tag in variable comment';
$this->currentFile->addError($error, $commentEnd);
}//end if
}//end processVar()
}//end class
?>