Skip to content

Commit fdc807a

Browse files
committed
Fix
1 parent da5e218 commit fdc807a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/override_from_interface.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class OverrideFromInterface implements SomeInterface
2929
/**
3030
* @return \Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo1|\Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo2
3131
*/
32-
public function run(): \Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\SomeInterface
32+
public function run(): \Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\FooInterface
3333
{
3434
return new Foo1();
3535
}

src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use PhpParser\Node\Expr\Closure;
1111
use PhpParser\Node\Identifier;
1212
use PhpParser\Node\Name;
13+
use PhpParser\Node\Name\FullyQualified;
1314
use PhpParser\Node\Param;
15+
use PhpParser\Node\Stmt\ClassLike;
1416
use PhpParser\Node\Stmt\ClassMethod;
1517
use PhpParser\Node\Stmt\Function_;
1618
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
@@ -27,6 +29,7 @@
2729
use Rector\Php\PhpVersionProvider;
2830
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
2931
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
32+
use Rector\PhpParser\AstResolver;
3033
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
3134
use Rector\Reflection\ReflectionResolver;
3235
use Rector\StaticTypeMapper\StaticTypeMapper;
@@ -52,7 +55,8 @@ public function __construct(
5255
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
5356
private readonly ReflectionResolver $reflectionResolver,
5457
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
55-
private readonly PhpVersionProvider $phpVersionProvider
58+
private readonly PhpVersionProvider $phpVersionProvider,
59+
private readonly AstResolver $astResolver,
5660
) {
5761
$this->classMethodWillChangeReturnTypes = [
5862
// @todo how to make list complete? is the method list needed or can we use just class names?
@@ -96,6 +100,29 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
96100
return;
97101
}
98102

103+
$ancestors = array_filter(
104+
$classReflection->getAncestors(),
105+
static fn (ClassReflection $ancestor): bool => $classReflection->getName() !== $ancestor->getName()
106+
);
107+
108+
foreach ($ancestors as $ancestor) {
109+
$classLike = $this->astResolver->resolveClassFromClassReflection($ancestor);
110+
if (! $classLike instanceof ClassLike) {
111+
continue;
112+
}
113+
114+
$classMethod = $classLike->getMethod($functionLike->name->toString());
115+
if (! $classMethod instanceof ClassMethod) {
116+
continue;
117+
}
118+
119+
$returnType = $classMethod->returnType;
120+
if ($returnType instanceof Node && $returnType instanceof FullyQualified) {
121+
$functionLike->returnType = new FullyQualified($returnType->toString());
122+
break;
123+
}
124+
}
125+
99126
if (! $this->isRequireReturnTypeWillChange($classReflection, $functionLike)) {
100127
return;
101128
}

0 commit comments

Comments
 (0)