Skip to content

Commit

Permalink
Merge pull request #59 from webimpress/feature/doc-comment-star
Browse files Browse the repository at this point in the history
Feature: Commenting\DocComment sniff checks if there is a star in every line
  • Loading branch information
michalbundyra committed Nov 13, 2019
2 parents 8ad56ad + a315761 commit 1ec33c4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/WebimpressCodingStandard/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function process(File $phpcsFile, $stackPtr)

$this->checkSpacesAfterStar($phpcsFile, $commentStart, $commentEnd);
$this->checkBlankLinesInComment($phpcsFile, $commentStart, $commentEnd);
$this->checkStarInEveryLine($phpcsFile, $commentStart, $commentEnd);

$this->checkBlankLineBeforeTags($phpcsFile, $commentStart);
}
Expand Down Expand Up @@ -584,6 +585,66 @@ private function checkSpacesAfterStar(File $phpcsFile, int $commentStart, int $c
}
}

private function checkStarInEveryLine(File $phpcsFile, int $commentStart, int $commentEnd) : void
{
$tokens = $phpcsFile->getTokens();

$firstLine = $tokens[$commentStart]['line'] + 1;
$lastLine = $tokens[$commentEnd]['line'] - 1;

$indentToken = $tokens[$commentStart - 1];
if ($indentToken['code'] === T_WHITESPACE
&& $indentToken['line'] === $tokens[$commentStart]['line']
) {
$indent = strlen($indentToken['content']) + 1;
} else {
$indent = 1;
}

$currentLine = $firstLine - 1;
for ($i = $commentStart; $i < $commentEnd; ++$i) {
$line = $tokens[$i]['line'];
if ($line > $lastLine) {
break;
}

if ($line === $currentLine + 1) {
$currentLine = $line;
if ($tokens[$i]['code'] === T_DOC_COMMENT_STAR) {
continue;
}

if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE
&& $tokens[$i + 1]['line'] === $line
&& $tokens[$i + 1]['code'] === T_DOC_COMMENT_STAR
) {
continue;
}

// blank line in a comment, skipping here
if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE
&& $tokens[$i + 1]['line'] > $line
) {
continue;
}

$error = 'Missing star at the beginning doc-block comment line';
$fix = $phpcsFile->addFixableError($error, $i, 'MissingStar');

if ($fix) {
if ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) {
$len = $tokens[$i]['length'];
$spaces = max($len - $indent - 1, 1);
$content = str_repeat(' ', $indent) . '*' . str_repeat(' ', $spaces);
$phpcsFile->fixer->replaceToken($i, $content);
} else {
$phpcsFile->fixer->addContentBefore($i, str_repeat(' ', $indent) . '* ');
}
}
}
}
}

/**
* Doc comment cannot have empty line on the beginning of the comment, at the end of the comment,
* and there is allowed only one empty line between two comment sections.
Expand Down
15 changes: 15 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,18 @@ class Foo
{
}
}

/**
hey
hello
*/

/**
hi
hello
*/

/**
@var int $param
*/
$param = 1;
15 changes: 15 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,18 @@ $c = 'xyz';
{
}
}

/**
* hey
* hello
*/

/**
* hi
* hello
*/

/**
* @var int $param
*/
$param = 1;
5 changes: 5 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ protected function getErrorList(string $testFile = '') : array
260 => 1,
320 => 1,
322 => 1,
329 => 1,
330 => 1,
334 => 1,
335 => 1,
339 => 1,
];
}

Expand Down

0 comments on commit 1ec33c4

Please sign in to comment.