Skip to content

Commit

Permalink
Merge pull request #104 from mansimas/patch-deprecated
Browse files Browse the repository at this point in the history
fixed @deprecated tag check
  • Loading branch information
saimaz committed Apr 21, 2015
2 parents bf76700 + 4e27274 commit d52000b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 9 deletions.
71 changes: 63 additions & 8 deletions Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
$this->processReturn($phpcsFile, $stackPtr, $commentStart);
$this->processThrows($phpcsFile, $stackPtr, $commentStart);
$this->processDeprecated($phpcsFile, $stackPtr, $commentStart);
$this->processParams($phpcsFile, $stackPtr, $commentStart);
$this->processComments($phpcsFile, $stackPtr, $commentStart);

Expand Down Expand Up @@ -299,9 +300,9 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
}//end if

$comment = null;
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
if ($tokens[($return + 2)]['code'] === T_DOC_COMMENT_STRING) {
$matches = array();
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($tag + 2)]['content'], $matches);
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[($return + 2)]['content'], $matches);
if (isset($matches[2]) === true) {
$comment = $matches[2];
}
Expand All @@ -312,26 +313,26 @@ protected function processReturn(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co
$firstChar = $comment{0};
if (preg_match('|\p{Lu}|u', $firstChar) === 0) {
$error = 'Return comment must start with a capital letter';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'ReturnCommentNotCapital');
$fix = $phpcsFile->addFixableError($error, ($return + 2), 'ReturnCommentNotCapital');

if ($fix === true) {
$newComment = ucfirst($comment);
$tokenLength = strlen($tokens[($tag + 2)]['content']);
$tokenLength = strlen($tokens[($return + 2)]['content']);
$commentLength = strlen($comment);
$tokenWithoutComment
= substr($tokens[($tag + 2)]['content'], 0, $tokenLength - $commentLength);
= substr($tokens[($return + 2)]['content'], 0, $tokenLength - $commentLength);

$phpcsFile->fixer->replaceToken(($tag + 2), $tokenWithoutComment . $newComment);
$phpcsFile->fixer->replaceToken(($return + 2), $tokenWithoutComment . $newComment);
}
}

$lastChar = substr($comment, -1);
if ($lastChar !== '.') {
$error = 'Return comment must end with a full stop';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'ReturnCommentFullStop');
$fix = $phpcsFile->addFixableError($error, ($return + 2), 'ReturnCommentFullStop');

if ($fix === true) {
$phpcsFile->fixer->addContent(($tag + 2), '.');
$phpcsFile->fixer->addContent(($return + 2), '.');
}
}
}
Expand Down Expand Up @@ -414,6 +415,60 @@ protected function processThrows(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $co

}//end processThrows()

/**
* Process any deprecated tags that this function comment has.
*
* @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 processDeprecated(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $commentStart)
{
$tokens = $phpcsFile->getTokens();
foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
if ($tokens[$tag]['content'] !== '@deprecated') {
continue;
}

$content = $tokens[($tag + 2)]['content'];
if (empty($content) === true || $tokens[($tag + 2)]['code'] !== T_DOC_COMMENT_STRING) {
$error = 'Deprecated type missing for @deprecated tag in function comment';
$phpcsFile->addError($error, $tag, 'MissingDeprecatedType');
} else {
//ONGR Validate that return comment begins with capital letter and ends with full stop.
if ($content !== null) {
$firstChar = $content{0};
if (preg_match('|\p{Lu}|u', $firstChar) === 0) {
$error = 'Deprecated comment must start with a capital letter';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DeprecatedCommentNotCapital');

if ($fix === true) {
$newComment = ucfirst($content);
$tokenLength = strlen($tokens[($tag + 2)]['content']);
$commentLength = strlen($content);
$tokenWithoutComment
= substr($tokens[($tag + 2)]['content'], 0, $tokenLength - $commentLength);

$phpcsFile->fixer->replaceToken(($tag + 2), $tokenWithoutComment . $newComment);
}
}

$lastChar = substr($content, -1);
if ($lastChar !== '.') {
$error = 'Deprecated comment must end with a full stop';
$fix = $phpcsFile->addFixableError($error, ($tag + 2), 'DeprecatedCommentFullStop');

if ($fix === true) {
$phpcsFile->fixer->addContent(($tag + 2), '.');
}
}
}
}
}
}

/**
* Process the function parameter comments.
Expand Down
4 changes: 3 additions & 1 deletion Tests/Unit/Commenting/FunctionCommentSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ protected function getErrorList()
89 => 1,
99 => 1,
109 => 2,
117 => 1
117 => 1,
129 => 1,
153 => 2
];
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/Unit/Commenting/FunctionCommentSniffTest.phptest
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,40 @@ class FunctionCommentSniffTest
{
return foo($bar);
}

/**
* Mapping is compared with loaded, if needed updates it and returns true.
*
* @param array $types Types to update.
*
* @return bool Foo
*
* @throws \LogicException
*
* @deprecated Will be removed in 1.0. Please now use Bar#foo().
*/
public function foo(array $types = [])
{
return $this->bar($types);
}

/**
* Short description.
*
* @deprecated Will be removed in 1.0. Please now use Bar#foo().
*/
public function updateMapping()
{
return $this->updateTypes();
}

/**
* Short description.
*
* @deprecated will be removed in 1.0. Please now use Connection#updateTypes()
*/
public function updateMapping()
{
return $this->updateTypes();
}
}

0 comments on commit d52000b

Please sign in to comment.