Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e77c03

Browse files
committed
[TypeDeclaration] Make anonymous class return specific type, if implements
1 parent 72884ba commit 2e77c03

7 files changed

Lines changed: 99 additions & 4 deletions

File tree

packages/PHPStanStaticTypeMapper/TypeMapper/ObjectWithoutClassTypeMapper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Name;
9+
use PhpParser\Node\Name\FullyQualified;
910
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1011
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
1112
use PHPStan\Type\Generic\TemplateObjectWithoutClassType;
1213
use PHPStan\Type\ObjectWithoutClassType;
1314
use PHPStan\Type\Type;
15+
use PHPStan\Type\TypeWithClassName;
1416
use Rector\BetterPhpDocParser\ValueObject\Type\EmptyGenericTypeNode;
1517
use Rector\Core\Php\PhpVersionProvider;
1618
use Rector\Core\ValueObject\PhpVersionFeature;
@@ -57,6 +59,13 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
5759
*/
5860
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
5961
{
62+
// special case for anonymous classes that implement another type
63+
if ($type->getSubtractedType() instanceof TypeWithClassName) {
64+
$objectType = $type->getSubtractedType();
65+
66+
return new FullyQualified($objectType->getClassName());
67+
}
68+
6069
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::OBJECT_TYPE)) {
6170
return null;
6271
}

packages/StaticTypeMapper/StaticTypeMapper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType, string
6565
public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, string $typeKind): ?Node
6666
{
6767
$node = $this->phpStanStaticTypeMapper->mapToPhpParserNode($phpStanType, $typeKind);
68-
6968
if (! $node instanceof Node) {
7069
return null;
7170
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;
4+
5+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;
6+
7+
final class AddAnonymousClassExtendsType
8+
{
9+
public function getEventDispatcher()
10+
{
11+
return new class extends SomeEventDispatcher {
12+
public function dispatch(object $event, string $eventName = null): object
13+
{
14+
}
15+
};
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;
24+
25+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;
26+
27+
final class AddAnonymousClassExtendsType
28+
{
29+
public function getEventDispatcher(): \Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher
30+
{
31+
return new class extends SomeEventDispatcher {
32+
public function dispatch(object $event, string $eventName = null): object
33+
{
34+
}
35+
};
36+
}
37+
}
38+
39+
?>

rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/add_anonymous_class_implements_type.php.inc renamed to rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/AnonymousClass/add_anonymous_class_implements_type.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;
44

55
use Psr\EventDispatcher\EventDispatcherInterface;
66

@@ -20,7 +20,7 @@ final class AddAnonymousClassImplementsType
2020
-----
2121
<?php
2222

23-
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;
23+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;
2424

2525
use Psr\EventDispatcher\EventDispatcherInterface;
2626

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;
4+
5+
use Psr\EventDispatcher\EventDispatcherInterface;
6+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;
7+
8+
final class GenericObjectWhenDoubleTypes
9+
{
10+
public function getEventDispatcher()
11+
{
12+
return new class extends SomeEventDispatcher implements EventDispatcherInterface {
13+
public function dispatch(object $event, string $eventName = null): object
14+
{
15+
}
16+
};
17+
}
18+
}
19+
-----
20+
<?php
21+
22+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;
23+
24+
use Psr\EventDispatcher\EventDispatcherInterface;
25+
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;
26+
27+
final class GenericObjectWhenDoubleTypes
28+
{
29+
public function getEventDispatcher(): object
30+
{
31+
return new class extends SomeEventDispatcher implements EventDispatcherInterface {
32+
public function dispatch(object $event, string $eventName = null): object
33+
{
34+
}
35+
};
36+
}
37+
}

rules-tests/TypeDeclaration/Rector/FunctionLike/ReturnTypeDeclarationRector/Fixture/closure_nested_type.php.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ class ClosureNestedType
5050
}
5151
}
5252

53-
?>
53+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass;
6+
7+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
8+
9+
abstract class SomeEventDispatcher implements EventDispatcherInterface
10+
{
11+
}

0 commit comments

Comments
 (0)