Skip to content

Commit

Permalink
Merge branch 'feature/50' into develop
Browse files Browse the repository at this point in the history
Close #50
  • Loading branch information
michalbundyra committed Nov 10, 2019
2 parents e7f7408 + 5f42a7c commit 2494384
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ All notable changes to this project will be documented in this file, in reverse

- [#49](https://github.com/webimpress/coding-standard/pull/49) adds `Commenting\DisallowEmptyComment` sniff to detect empty comments and multiple empty lines in comments

- [#50](https://github.com/webimpress/coding-standard/pull/50) adds check for open and close of doc block comment in `Commenting\DocComment`.
Only short version is allowed: `/**` and `*/`. Additional asterisk are disallowed.

### Changed

- [#42](https://github.com/webimpress/coding-standard/pull/42) changes `NamingConventions\ValidVariableName` to require variable names be in strict camelCase. It means two capital letters next to each other are not allowed.
Expand Down
29 changes: 29 additions & 0 deletions src/WebimpressCodingStandard/Sniffs/Commenting/DocCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

$this->checkTags($phpcsFile, $commentStart, $commentEnd);
$this->checkBeforeOpen($phpcsFile, $commentStart);
$this->checkAfterClose($phpcsFile, $commentStart, $commentEnd);
$this->checkCommentIndents($phpcsFile, $commentStart, $commentEnd);
Expand Down Expand Up @@ -160,6 +161,34 @@ private function checkIfEmpty(File $phpcsFile, int $commentStart, int $commentEn
return true;
}

/**
* Check if there is no additional * with comment open and close tags
*/
private function checkTags(File $phpcsFile, int $commentStart, int $commentEnd) : void
{
$tokens = $phpcsFile->getTokens();

if ($tokens[$commentStart]['content'] !== '/**') {
$error = 'Invalid PHPDoc open tag; expected /** but found %s';
$data = [$tokens[$commentStart]['content']];

$fix = $phpcsFile->addFixableError($error, $commentStart, 'InvalidOpen', $data);
if ($fix) {
$phpcsFile->fixer->replaceToken($commentStart, '/**');
}
}

if ($tokens[$commentEnd]['content'] !== '*/') {
$error = 'Invalid PHPDoc close tag; expected */ but found %s';
$data = [$tokens[$commentEnd]['content']];

$fix = $phpcsFile->addFixableError($error, $commentEnd, 'InvalidClose', $data);
if ($fix) {
$phpcsFile->fixer->replaceToken($commentEnd, '*/');
}
}
}

/**
* Checks if there is no any other content before doc comment opening tag,
* and if there is blank line before doc comment (for multiline doc comment).
Expand Down
7 changes: 7 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,11 @@ class Foo
public function allowSpaceBetweenTwoPhpDocBlock()
{
}

/***
* @return void
**/
public function wrongAdditionalStarsWithOpenAndCloseTags()
{
}
}
7 changes: 7 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,11 @@ $c = 'xyz';
public function allowSpaceBetweenTwoPhpDocBlock()
{
}

/**
* @return void
*/
public function wrongAdditionalStarsWithOpenAndCloseTags()
{
}
}
2 changes: 2 additions & 0 deletions test/Sniffs/Commenting/DocCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ protected function getErrorList(string $testFile = '') : array
258 => 1,
259 => 1,
260 => 1,
320 => 1,
322 => 1,
];
}

Expand Down

0 comments on commit 2494384

Please sign in to comment.