mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 03:35:01 +01:00
Merge pull request #3576 from rectorphp/fix-return-child-type
fix return type in abstract class
This commit is contained in:
commit
1bc1c6fde7
@ -53,9 +53,12 @@ final class ReturnTypeInferer extends AbstractPriorityAwareTypeInferer
|
||||
$type = $this->typeNormalizer->uniqueateConstantArrayType($type);
|
||||
$type = $this->typeNormalizer->normalizeArrayOfUnionToUnionArray($type);
|
||||
|
||||
if (! $type instanceof MixedType) {
|
||||
return $type;
|
||||
// in case of void, check return type of children methods
|
||||
if ($type instanceof MixedType) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
return new MixedType();
|
||||
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
@ -30,15 +31,18 @@ final class ReturnedNodesReturnTypeInferer extends AbstractTypeInferer implement
|
||||
{
|
||||
/** @var Class_|Trait_|Interface_|null $classLike */
|
||||
$classLike = $functionLike->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if ($classLike === null) {
|
||||
return new MixedType();
|
||||
}
|
||||
|
||||
if ($functionLike instanceof ClassMethod && $classLike instanceof Interface_) {
|
||||
return new MixedType();
|
||||
}
|
||||
|
||||
$localReturnNodes = $this->collectReturns($functionLike);
|
||||
|
||||
if ($localReturnNodes === []) {
|
||||
// void type
|
||||
if ($functionLike instanceof ClassMethod && ! $functionLike->isAbstract()) {
|
||||
if (! $this->isAbstractMethod($classLike, $functionLike)) {
|
||||
return new VoidType();
|
||||
}
|
||||
|
||||
@ -92,4 +96,15 @@ final class ReturnedNodesReturnTypeInferer extends AbstractTypeInferer implement
|
||||
|
||||
return $returns;
|
||||
}
|
||||
|
||||
private function isAbstractMethod(ClassLike $classLike, FunctionLike $functionLike): bool
|
||||
{
|
||||
// abstract class method
|
||||
if ($functionLike instanceof ClassMethod && $functionLike->isAbstract()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// abstract class
|
||||
return $classLike instanceof Class_ && $classLike->isAbstract();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ReturnTypeDeclarationRector\Fixture;
|
||||
|
||||
use Rector\Core\Exception\NotImplementedYetException;
|
||||
|
||||
abstract class ParentOverride
|
||||
{
|
||||
public function getValue()
|
||||
{
|
||||
throw new NotImplementedYetException();
|
||||
}
|
||||
}
|
||||
|
||||
class OverrideParent extends ParentOverride
|
||||
{
|
||||
public function getValue()
|
||||
{
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ReturnTypeDeclarationRector\Fixture;
|
||||
|
||||
use Rector\Core\Exception\NotImplementedYetException;
|
||||
|
||||
abstract class ParentOverride
|
||||
{
|
||||
public function getValue()
|
||||
{
|
||||
throw new NotImplementedYetException();
|
||||
}
|
||||
}
|
||||
|
||||
class OverrideParent extends ParentOverride
|
||||
{
|
||||
public function getValue(): string
|
||||
{
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user