Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ parameters:
curlSetOptArrayTypes: true
checkDateIntervalConstructor: true
reportMethodPurityOverride: true
reportInvalidInheritDocTag: true
5 changes: 5 additions & 0 deletions conf/config.level2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ conditionalTags:
phpstan.restrictedPropertyUsageExtension: %featureToggles.internalTag%
PHPStan\Rules\InternalTag\RestrictedInternalMethodUsageExtension:
phpstan.restrictedMethodUsageExtension: %featureToggles.internalTag%
PHPStan\Rules\PhpDoc\InvalidInheritDocTagRule:
phpstan.rules.rule: %featureToggles.reportInvalidInheritDocTag%

services:
-
class: PHPStan\Rules\InternalTag\RestrictedInternalPropertyUsageExtension

-
class: PHPStan\Rules\InternalTag\RestrictedInternalMethodUsageExtension

-
class: PHPStan\Rules\PhpDoc\InvalidInheritDocTagRule
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ parameters:
curlSetOptArrayTypes: false
checkDateIntervalConstructor: false
reportMethodPurityOverride: false
reportInvalidInheritDocTag: false
fileExtensions:
- php
checkAdvancedIsset: false
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ parametersSchema:
curlSetOptArrayTypes: bool()
checkDateIntervalConstructor: bool()
reportMethodPurityOverride: bool()
reportInvalidInheritDocTag: bool()
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
Expand Down
111 changes: 111 additions & 0 deletions src/Rules/PhpDoc/InvalidInheritDocTagRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PhpDoc;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassMethodNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\Rules\Methods\ParentMethodHelper;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function preg_match;
use function sprintf;
use function strtolower;

/**
* @implements Rule<InClassMethodNode>
*/
final class InvalidInheritDocTagRule implements Rule
{

private const INLINE_INHERIT_DOC_REGEX = '~`[^`]*`(*SKIP)(*FAIL)|(?<![a-zA-Z0-9])\{@inheritDoc\b[^}]*\}~i';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this regex be simplified because it uses i modifier (case-less), so the pattern itself does not need to handle both upper and lower case things?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? To lowercase $child->text and drop i, or to match only on exact case match?


public function __construct(
private Lexer $phpDocLexer,
private PhpDocParser $phpDocParser,
private ParentMethodHelper $parentMethodHelper,
)
{
}

public function getNodeType(): string
{
return InClassMethodNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
$docComment = $node->getOriginalNode()->getDocComment();
if ($docComment === null) {
return [];
}

$tokens = new TokenIterator($this->phpDocLexer->tokenize($docComment->getText()));
$phpDocNode = $this->phpDocParser->parse($tokens);

$inheritDocTagName = null;
foreach ($phpDocNode->getTags() as $tag) {
if (strtolower($tag->name) !== '@inheritdoc') {
continue;
}

$inheritDocTagName = $tag->name;
break;
}

if ($inheritDocTagName === null) {
Copy link
Copy Markdown
Contributor

@staabm staabm May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this additional regex matching on-top of regular phpDoc parsing.
all relevant cases should be detected by the phpdoc parser, if it is doing a good job.

if not we should improve the phpdoc parser IMO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, this is not perfect. The limitation is {@inheritDoc} ends up as part of a PhpDocTextNode, not as a tag.

foreach ($phpDocNode->children as $child) {
if (!$child instanceof PhpDocTextNode) {
continue;
}

if (preg_match(self::INLINE_INHERIT_DOC_REGEX, $child->text, $matches) !== 1) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we do better ?

I know it'll be an edge case but you won't catch description like

/**
 * Please do not add `{@inheritDoc}` to this method
 */

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very good in regex but looking at the test called InheritDocInsideBackticks i'm not sure we understood each other.

My point was that, we might not want to consider that a method with

/**
 * Foo @inheritDoc
 */ 

has an inheritDoc, cause it might be just a comment.
It wasn't related to backticks

(On the opposite,

/**
 * @inheritDoc Bar
 */ 

might be considered as an inherit doc with an extra comment... ; I dunno)

I never use inheritDoc so I can't stay what we can find in codebase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated test.

@inheritDoc Bar will be tokenised as a tag with extra description.

continue;
}

$inheritDocTagName = $matches[0];
break;
}
}

if ($inheritDocTagName === null) {
return [];
}

$inheritanceClass = $scope->isInTrait() ? $scope->getTraitReflection() : $node->getClassReflection();
$methodName = $node->getMethodReflection()->getName();

$parentMethods = $this->parentMethodHelper->collectParentMethods($methodName, $inheritanceClass);

if ($parentMethods === []) {
return [
RuleErrorBuilder::message(sprintf(
'PHPDoc tag %s on method %s::%s() refers to non-existent parent method.',
$inheritDocTagName,
$inheritanceClass->getDisplayName(),
$methodName,
))->identifier('inheritDoc.noParent')->build(),
];
}

foreach ($parentMethods as [$parentMethod]) {
if ($parentMethod->getResolvedPhpDoc() !== null) {
return [];
}
}

return [
RuleErrorBuilder::message(sprintf(
'PHPDoc tag %s on method %s::%s() refers to a parent method that does not have a PHPDoc.',
$inheritDocTagName,
$inheritanceClass->getDisplayName(),
$methodName,
))->identifier('inheritDoc.parentWithoutPhpDoc')->build(),
];
}

}
72 changes: 72 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/InvalidInheritDocTagRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PhpDoc;

use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\Rules\Methods\ParentMethodHelper;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<InvalidInheritDocTagRule>
*/
class InvalidInheritDocTagRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new InvalidInheritDocTagRule(
self::getContainer()->getByType(Lexer::class),
self::getContainer()->getByType(PhpDocParser::class),
self::getContainer()->getByType(ParentMethodHelper::class),
);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/invalid-inherit-doc-tag.php'], [
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ChildWithInlineInheritDoc::methodWithoutPhpDoc() refers to a parent method that does not have a PHPDoc.',
31,
],
[
'PHPDoc tag @inheritdoc on method InvalidInheritDocTag\ChildWithBlockInheritDoc::methodWithoutPhpDoc() refers to a parent method that does not have a PHPDoc.',
52,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ClassWithoutParent::orphanedInheritDoc() refers to non-existent parent method.',
73,
],
[
'PHPDoc tag @inheritdoc on method InvalidInheritDocTag\ClassWithoutParent::orphanedBlockInheritDoc() refers to non-existent parent method.',
81,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ImplementsInterface::interfaceMethodWithoutPhpDoc() refers to a parent method that does not have a PHPDoc.',
106,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\UsesTraitWithoutPhpDoc::traitMethodWithoutPhpDoc() refers to non-existent parent method.',
216,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\UsesTraitWithPhpDoc::traitMethodWithPhpDoc() refers to non-existent parent method.',
231,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\IssueExampleChild::f() refers to a parent method that does not have a PHPDoc.',
254,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\ChildOfPrivateParentMethod::privateMethod() refers to non-existent parent method.',
280,
],
[
'PHPDoc tag {@inheritdoc} on method InvalidInheritDocTag\OrphanedInheritDocTrait::orphaned() refers to non-existent parent method.',
293,
],
]);
}

}
Loading
Loading