Skip to content

Commit a6c2bac

Browse files
committed
UselessInheritDocCommentSniff: Fixed false positives
1 parent 0064786 commit a6c2bac

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

SlevomatCodingStandard/Sniffs/Commenting/UselessInheritDocCommentSniff.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use SlevomatCodingStandard\Helpers\FunctionHelper;
78
use SlevomatCodingStandard\Helpers\TokenHelper;
9+
use SlevomatCodingStandard\Helpers\TypeHintHelper;
810
use const T_DOC_COMMENT_OPEN_TAG;
911
use const T_DOC_COMMENT_STAR;
1012
use const T_DOC_COMMENT_WHITESPACE;
@@ -49,6 +51,29 @@ public function process(File $phpcsFile, $docCommentOpenPointer): void
4951
return;
5052
}
5153

54+
$docCommentOwnerPointer = TokenHelper::findNext($phpcsFile, array_merge(TokenHelper::$functionTokenCodes, TokenHelper::$typeHintTokenCodes), $tokens[$docCommentOpenPointer]['comment_closer'] + 1);
55+
if (in_array($tokens[$docCommentOwnerPointer]['code'], TokenHelper::$functionTokenCodes, true)) {
56+
$returnTypeHint = FunctionHelper::findReturnTypeHint($phpcsFile, $docCommentOwnerPointer);
57+
if ($returnTypeHint === null) {
58+
return;
59+
}
60+
61+
if (TypeHintHelper::isSimpleIterableTypeHint($returnTypeHint->getTypeHint())) {
62+
return;
63+
}
64+
65+
$parametersTypeHints = FunctionHelper::getParametersTypeHints($phpcsFile, $docCommentOwnerPointer);
66+
foreach ($parametersTypeHints as $parameterTypeHint) {
67+
if ($parameterTypeHint === null) {
68+
return;
69+
}
70+
71+
if (TypeHintHelper::isSimpleIterableTypeHint($parameterTypeHint->getTypeHint())) {
72+
return;
73+
}
74+
}
75+
}
76+
5277
$fix = $phpcsFile->addFixableError('Useless documentation comment with @inheritDoc.', $docCommentOpenPointer, self::CODE_USELESS_INHERIT_DOC_COMMENT);
5378

5479
if (!$fix) {

tests/Sniffs/Commenting/UselessInheritDocCommentSniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/uselessInheritDocCommentSniffErrors.php');
1919

20-
self::assertSame(4, $report->getErrorCount());
20+
self::assertSame(6, $report->getErrorCount());
2121

2222
self::assertSniffError($report, 3, UselessInheritDocCommentSniff::CODE_USELESS_INHERIT_DOC_COMMENT);
2323
self::assertSniffError($report, 11, UselessInheritDocCommentSniff::CODE_USELESS_INHERIT_DOC_COMMENT);
2424
self::assertSniffError($report, 19, UselessInheritDocCommentSniff::CODE_USELESS_INHERIT_DOC_COMMENT);
2525
self::assertSniffError($report, 27, UselessInheritDocCommentSniff::CODE_USELESS_INHERIT_DOC_COMMENT);
26+
self::assertSniffError($report, 41, UselessInheritDocCommentSniff::CODE_USELESS_INHERIT_DOC_COMMENT);
27+
self::assertSniffError($report, 49, UselessInheritDocCommentSniff::CODE_USELESS_INHERIT_DOC_COMMENT);
2628

2729
self::assertAllFixedInFile($report);
2830
}

tests/Sniffs/Commenting/data/uselessInheritDocCommentSniffErrors.fixed.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ class ALotOfWhitespace
1919
{
2020

2121
}
22+
23+
class Errors
24+
{
25+
26+
public function parameterWithNotIterableTypeHint(bool $a): bool
27+
{
28+
return true;
29+
}
30+
31+
public function withNotIterableReturnType(): bool
32+
{
33+
return false;
34+
}
35+
36+
}

tests/Sniffs/Commenting/data/uselessInheritDocCommentSniffErrors.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@ class ALotOfWhitespace
3434
{
3535

3636
}
37+
38+
class Errors
39+
{
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function parameterWithNotIterableTypeHint(bool $a): bool
45+
{
46+
return true;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function withNotIterableReturnType(): bool
53+
{
54+
return false;
55+
}
56+
57+
}

tests/Sniffs/Commenting/data/uselessInheritDocCommentSniffNoErrors.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,38 @@ trait ContainsEvenSomethingElse
2525
{
2626

2727
}
28+
29+
class NoErrors
30+
{
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function parameterWithoutTypeHint($a): bool
36+
{
37+
return true;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function iterableParameter(array $a, iterable $b): bool
44+
{
45+
return false;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function withoutReturnType()
52+
{
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function iterableReturnType(): array
59+
{
60+
}
61+
62+
}

0 commit comments

Comments
 (0)