Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX: do not abuse "inheritdoc" tag #5663

Merged
merged 1 commit into from Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AbstractFixer.php
Expand Up @@ -176,7 +176,7 @@ public function configure(array $configuration = null)
}

/**
* {@inheritdoc}
* @return FixerConfigurationResolverInterface
*/
public function getConfigurationDefinition()
{
Expand Down
56 changes: 56 additions & 0 deletions tests/AutoReview/ProjectCodeTest.php
Expand Up @@ -522,6 +522,62 @@ public function testAllCodeContainSingleClassy($className)
}
}

/**
* @dataProvider provideSrcClassCases
*
* @param string $className
*/
public function testInheritdocIsNotAbused($className)
{
$rc = new \ReflectionClass($className);

$allowedMethods = array_map(
function (\ReflectionClass $interface) {
return $this->getPublicMethodNames($interface);
},
$rc->getInterfaces()
);

if (\count($allowedMethods)) {
$allowedMethods = array_merge(...array_values($allowedMethods));
}

$parentClass = $rc;
while (false !== $parentClass = $parentClass->getParentClass()) {
Copy link
Member

Choose a reason for hiding this comment

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

should we also check interfaces and traits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We check interfaces, see line 538, about traits I'm not sure, can we use @inheritdoc to inherit from trait? I've never seen such situation.

foreach ($parentClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) {
$allowedMethods[] = $method->getName();
}
}

$allowedMethods = array_unique($allowedMethods);

$methodsWithInheritdoc = array_filter(
$rc->getMethods(),
static function (\ReflectionMethod $rm) {
return false !== $rm->getDocComment() && stripos($rm->getDocComment(), '@inheritdoc');
}
);
$methodsWithInheritdoc = array_map(
static function (\ReflectionMethod $rm) {
return $rm->getName();
},
$methodsWithInheritdoc
);

$extraMethods = array_diff($methodsWithInheritdoc, $allowedMethods);

static::assertEmpty(
$extraMethods,
sprintf(
"Class '%s' should not have methods with '@inheritdoc' in PHPDoc that are not inheriting PHPDoc.\nViolations:\n%s",
$className,
implode("\n", array_map(static function ($item) {
return " * {$item}";
}, $extraMethods))
)
);
}

public function provideSrcClassCases()
{
return array_map(
Expand Down