From 2f74584b83506d430404af1197e872b6318d1433 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 24 Jan 2025 20:29:55 +0100 Subject: [PATCH] FileTypeMapper - fix getting PHPDoc of abstract trait method --- .../Php/PhpClassReflectionExtension.php | 4 +- .../Rules/Methods/MethodSignatureRuleTest.php | 11 +++++ ...overriden-abstract-trait-method-phpdoc.php | 46 +++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/PHPStan/Rules/Methods/data/overriden-abstract-trait-method-phpdoc.php diff --git a/src/Reflection/Php/PhpClassReflectionExtension.php b/src/Reflection/Php/PhpClassReflectionExtension.php index 9b9a9132e1..f08d329483 100644 --- a/src/Reflection/Php/PhpClassReflectionExtension.php +++ b/src/Reflection/Php/PhpClassReflectionExtension.php @@ -676,8 +676,8 @@ public function createUserlandMethodReflection(ClassReflection $fileDeclaringCla $resolvedPhpDoc = $this->phpDocInheritanceResolver->resolvePhpDocForMethod( $docComment, - $fileDeclaringClass->getFileName(), - $fileDeclaringClass, + $actualDeclaringClass->getFileName(), + $actualDeclaringClass, $declaringTraitName, $methodReflection->getName(), $positionalParameterNames, diff --git a/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php b/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php index 93c6a67b9d..6c07d330ed 100644 --- a/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php +++ b/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php @@ -550,4 +550,15 @@ public function testBug3580(): void $this->analyse([__DIR__ . '/data/bug-3580.php'], []); } + public function testOverridenAbstractTraitMethodPhpDoc(): void + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Test requires PHP 8.0.'); + } + + $this->reportMaybes = true; + $this->reportStatic = true; + $this->analyse([__DIR__ . '/data/overriden-abstract-trait-method-phpdoc.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/overriden-abstract-trait-method-phpdoc.php b/tests/PHPStan/Rules/Methods/data/overriden-abstract-trait-method-phpdoc.php new file mode 100644 index 0000000000..6a89da7862 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/overriden-abstract-trait-method-phpdoc.php @@ -0,0 +1,46 @@ + + */ +trait FooTrait +{ + /** + * Offset checker + * + * @phpstan-param Offset $offset + * @return bool + * @template Offset of key-of + */ + abstract public function offsetExists(mixed $offset): bool; +} + +/** + * @template DataArray of array + * @phpstan-type DataKey key-of + * @phpstan-type DataValue DataArray[DataKey] + */ +class FooClass +{ + + /** @phpstan-use FooTrait */ + use FooTrait; + + /** @phpstan-var DataArray|array{} */ + public array $data = []; + + + /** + * Data checker + * + * @phpstan-param Offset $offset + * @return bool + * @template Offset of key-of + */ + public function offsetExists(mixed $offset): bool + { + return array_key_exists($offset, $this->data); + } +}