[TypeDeclaration] Skip return type defined Closure on ReturnTypeDeclarationRector (#2179)

* [TypeDeclaration] Skip return type defined Closure on ReturnTypeDeclarationRector

* Fixed 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fix

* final touch: clean up

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-04-27 20:30:48 +07:00 committed by GitHub
parent ae45be1cf3
commit 4fea4e23b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 28 deletions

View File

@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClosureType;
@ -55,7 +55,7 @@ final class ClosureTypeMapper implements TypeMapperInterface
return null;
}
return new Name('callable');
return new FullyQualified('Closure');
}
#[Required]

View File

@ -0,0 +1,53 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
class ClosureNestedType
{
private function collectionCallback(): callable
{
return function (int $offset, int $itemCountPerPage): iterable {
$stmt = $this->connection->executeQuery('SELECT * FROM *');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
yield EventsEntity::forCollection();
}
};
}
private function again(): callable
{
return function (int $offset, int $itemCountPerPage): iterable {
return 25;
};
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
class ClosureNestedType
{
private function collectionCallback(): \Closure
{
return function (int $offset, int $itemCountPerPage): iterable {
$stmt = $this->connection->executeQuery('SELECT * FROM *');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
yield EventsEntity::forCollection();
}
};
}
private function again(): \Closure
{
return function (int $offset, int $itemCountPerPage): iterable {
return 25;
};
}
}
?>

View File

@ -67,7 +67,7 @@ class KnownStatic
return [];
}
public function getCallable(): callable
public function getCallable(): \Closure
{
return function () {
};

View File

@ -1,24 +0,0 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
class SkipAnonymousFunctionNestedType
{
private function collectionCallback(): callable
{
return function (int $offset, int $itemCountPerPage): iterable {
$stmt = $this->connection->executeQuery('SELECT * FROM *');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
yield EventsEntity::forCollection();
}
};
}
private function again(): callable
{
return function (int $offset, int $itemCountPerPage): iterable {
return 25;
};
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
use Closure;
final class skipReturnClosureToCallable
{
public function run(): Closure
{
return fn ($query) => $query->where('id', 1);
}
}

View File

@ -46,7 +46,7 @@ final class ReturnTypeDeclarationRector extends AbstractRector implements MinPhp
private readonly ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard,
private readonly VendorLockResolver $vendorLockResolver,
private readonly PhpParserTypeAnalyzer $phpParserTypeAnalyzer,
private readonly ObjectTypeComparator $objectTypeComparator,
private readonly ObjectTypeComparator $objectTypeComparator
) {
}