From 7da60a00f41ed8e9019cbc31e6d2b216ff3b6d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Wed, 28 Apr 2021 13:03:32 +0200 Subject: [PATCH] DX: do not abuse "inheritdoc" tag --- src/AbstractFixer.php | 2 +- tests/AutoReview/ProjectCodeTest.php | 56 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/AbstractFixer.php b/src/AbstractFixer.php index b8ae6ddb411..03d8c68544b 100644 --- a/src/AbstractFixer.php +++ b/src/AbstractFixer.php @@ -176,7 +176,7 @@ public function configure(array $configuration = null) } /** - * {@inheritdoc} + * @return FixerConfigurationResolverInterface */ public function getConfigurationDefinition() { diff --git a/tests/AutoReview/ProjectCodeTest.php b/tests/AutoReview/ProjectCodeTest.php index 6ede551241b..11579569ffc 100644 --- a/tests/AutoReview/ProjectCodeTest.php +++ b/tests/AutoReview/ProjectCodeTest.php @@ -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()) { + 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(