Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;

use Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo1;
use Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo2;
use Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\SomeInterface;

class OverrideFromInterface implements SomeInterface
{
public function run(): Foo1|Foo2
{
return new Foo1();
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;

use Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo1;
use Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo2;
use Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\SomeInterface;

class OverrideFromInterface implements SomeInterface
{
/**
* @return \Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo1|\Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\Foo2
*/
public function run(): \Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source\FooInterface
{
return new Foo1();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source;

class Foo1 implements FooInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source;

class Foo2 implements FooInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source;

interface FooInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Source;

interface SomeInterface
{
public function run(): FooInterface;
}
29 changes: 28 additions & 1 deletion src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
Expand All @@ -27,6 +29,7 @@
use Rector\Php\PhpVersionProvider;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
use Rector\PhpParser\AstResolver;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Reflection\ReflectionResolver;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -52,7 +55,8 @@ public function __construct(
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory,
private readonly ReflectionResolver $reflectionResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly PhpVersionProvider $phpVersionProvider
private readonly PhpVersionProvider $phpVersionProvider,
private readonly AstResolver $astResolver,
) {
$this->classMethodWillChangeReturnTypes = [
// @todo how to make list complete? is the method list needed or can we use just class names?
Expand Down Expand Up @@ -96,6 +100,29 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
return;
}

$ancestors = array_filter(
$classReflection->getAncestors(),
static fn (ClassReflection $ancestor): bool => $classReflection->getName() !== $ancestor->getName()
);

foreach ($ancestors as $ancestor) {
$classLike = $this->astResolver->resolveClassFromClassReflection($ancestor);
if (! $classLike instanceof ClassLike) {
continue;
}

$classMethod = $classLike->getMethod($functionLike->name->toString());
if (! $classMethod instanceof ClassMethod) {
continue;
}

$returnType = $classMethod->returnType;
if ($returnType instanceof Node && $returnType instanceof FullyQualified) {
$functionLike->returnType = new FullyQualified($returnType->toString());
break;
}
}

if (! $this->isRequireReturnTypeWillChange($classReflection, $functionLike)) {
return;
}
Expand Down
Loading