Skip to content

Commit 0f1c282

Browse files
committed
Added DocCommentHelper::getDocComment()
1 parent 2f337b0 commit 0f1c282

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

SlevomatCodingStandard/Helpers/DocCommentHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ public static function hasDocComment(\PHP_CodeSniffer\Files\File $codeSnifferFil
1010
return self::findDocCommentOpenToken($codeSnifferFile, $pointer) !== null;
1111
}
1212

13+
/**
14+
* @param \PHP_CodeSniffer\Files\File $codeSnifferFile
15+
* @param int $pointer
16+
* @return string|null
17+
*/
18+
public static function getDocComment(\PHP_CodeSniffer\Files\File $codeSnifferFile, int $pointer)
19+
{
20+
$docCommentOpenToken = self::findDocCommentOpenToken($codeSnifferFile, $pointer);
21+
if ($docCommentOpenToken === null) {
22+
return null;
23+
}
24+
25+
return trim(TokenHelper::getContent($codeSnifferFile, $docCommentOpenToken + 1, $codeSnifferFile->getTokens()[$docCommentOpenToken]['comment_closer'] - 1));
26+
}
27+
1328
public static function hasDocCommentDescription(\PHP_CodeSniffer\Files\File $codeSnifferFile, int $pointer): bool
1429
{
1530
$docCommentOpenToken = self::findDocCommentOpenToken($codeSnifferFile, $pointer);

SlevomatCodingStandard/Sniffs/Commenting/InlineDocCommentDeclarationSniff.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use SlevomatCodingStandard\Helpers\DocCommentHelper;
66
use SlevomatCodingStandard\Helpers\PropertyHelper;
7-
use SlevomatCodingStandard\Helpers\TokenHelper;
87

98
class InlineDocCommentDeclarationSniff implements \PHP_CodeSniffer\Sniffs\Sniff
109
{
@@ -39,7 +38,7 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $variablePointer
3938

4039
$tokens = $phpcsFile->getTokens();
4140

42-
$docCommentContent = trim(TokenHelper::getContent($phpcsFile, $docCommentOpenPointer + 1, $tokens[$docCommentOpenPointer]['comment_closer'] - 1));
41+
$docCommentContent = DocCommentHelper::getDocComment($phpcsFile, $variablePointer);
4342

4443
if (strpos($docCommentContent, '@var') !== 0) {
4544
return;

tests/Helpers/DocCommentHelperTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public function testClassHasDocComment()
1414
$this->assertTrue(DocCommentHelper::hasDocComment($this->getTestedCodeSnifferFile(), $this->findClassPointerByName($this->getTestedCodeSnifferFile(), 'WithDocComment')));
1515
}
1616

17+
public function testClassGetDocComment()
18+
{
19+
$this->assertSame("* Class WithDocComment\n *\n * @see https://www.slevomat.cz", DocCommentHelper::getDocComment($this->getTestedCodeSnifferFile(), $this->findClassPointerByName($this->getTestedCodeSnifferFile(), 'WithDocCommentAndDescription')));
20+
$this->assertNull(DocCommentHelper::getDocComment($this->getTestedCodeSnifferFile(), $this->findClassPointerByName($this->getTestedCodeSnifferFile(), 'WithoutDocComment')));
21+
}
22+
1723
public function testClassHasNoDocComment()
1824
{
1925
$this->assertFalse(DocCommentHelper::hasDocComment($this->getTestedCodeSnifferFile(), $this->findClassPointerByName($this->getTestedCodeSnifferFile(), 'WithoutDocComment')));

0 commit comments

Comments
 (0)