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

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
17 changes: 6 additions & 11 deletions packages/NodeTypeResolver/NodeTypeResolver/NewTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ObjectWithoutClassTypeWithParentTypes;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;

/**
Expand Down Expand Up @@ -72,27 +72,22 @@ private function resolveAnonymousClassType(New_ $new): ObjectWithoutClassType
return new ObjectWithoutClassType();
}

$types = [];
$directParentTypes = [];

/** @var Class_ $class */
$class = $new->class;
if ($class->extends !== null) {
$parentClass = (string) $class->extends;
$types[] = new FullyQualifiedObjectType($parentClass);
$directParentTypes[] = new FullyQualifiedObjectType($parentClass);
}

foreach ($class->implements as $implement) {
$parentClass = (string) $implement;
$types[] = new FullyQualifiedObjectType($parentClass);
$directParentTypes[] = new FullyQualifiedObjectType($parentClass);
}

if (count($types) > 1) {
$unionType = new UnionType($types);
return new ObjectWithoutClassType($unionType);
}

if (count($types) === 1) {
return new ObjectWithoutClassType($types[0]);
if ($directParentTypes !== []) {
return new ObjectWithoutClassTypeWithParentTypes($directParentTypes);
}

return new ObjectWithoutClassType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\NodeTypeResolver\PHPStan;

use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;

final class ObjectWithoutClassTypeWithParentTypes extends ObjectWithoutClassType
{
/**
* @param TypeWithClassName[] $parentTypes
*/
public function __construct(
private readonly array $parentTypes,
?Type $subtractedType = null
) {
parent::__construct($subtractedType);
}

/**
* @return TypeWithClassName[]
*/
public function getParentTypes(): array
{
return $this->parentTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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\Generic\TemplateObjectWithoutClassType;
Expand All @@ -14,6 +15,7 @@
use Rector\BetterPhpDocParser\ValueObject\Type\EmptyGenericTypeNode;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\PHPStan\ObjectWithoutClassTypeWithParentTypes;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\StaticTypeMapper\ValueObject\Type\ParentObjectWithoutClassType;

Expand Down Expand Up @@ -57,6 +59,15 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
// special case for anonymous classes that implement another type
if ($type instanceof ObjectWithoutClassTypeWithParentTypes) {
$parentTypes = $type->getParentTypes();
if (count($parentTypes) === 1) {
$parentType = $parentTypes[0];
return new FullyQualified($parentType->getClassName());
}
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::OBJECT_TYPE)) {
return null;
}
Expand Down
1 change: 0 additions & 1 deletion packages/StaticTypeMapper/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function mapPHPStanTypeToPHPStanPhpDocTypeNode(Type $phpStanType, string
public function mapPHPStanTypeToPhpParserNode(Type $phpStanType, string $typeKind): ?Node
{
$node = $this->phpStanStaticTypeMapper->mapToPhpParserNode($phpStanType, $typeKind);

if (! $node instanceof Node) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;

use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;

final class AddAnonymousClassExtendsType
{
public function getEventDispatcher()
{
return new class extends SomeEventDispatcher {
public function dispatch(object $event, string $eventName = null): object
{
}
};
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;

use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;

final class AddAnonymousClassExtendsType
{
public function getEventDispatcher(): \Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher
{
return new class extends SomeEventDispatcher {
public function dispatch(object $event, string $eventName = null): object
{
}
};
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;

use Psr\EventDispatcher\EventDispatcherInterface;

final class AddAnonymousClassImplementsType
{
public function getEventDispatcher()
{
return new class implements EventDispatcherInterface {
public function dispatch(object $event): void
{
}
};
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;

use Psr\EventDispatcher\EventDispatcherInterface;

final class AddAnonymousClassImplementsType
{
public function getEventDispatcher(): \Psr\EventDispatcher\EventDispatcherInterface
{
return new class implements EventDispatcherInterface {
public function dispatch(object $event): void
{
}
};
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;

use Psr\EventDispatcher\EventDispatcherInterface;
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;

final class GenericObjectWhenDoubleTypes
{
public function getEventDispatcher()
{
return new class extends SomeEventDispatcher implements EventDispatcherInterface {
public function dispatch(object $event, string $eventName = null): object
{
}
};
}
}
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture\AnonymousClass;

use Psr\EventDispatcher\EventDispatcherInterface;
use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass\SomeEventDispatcher;

final class GenericObjectWhenDoubleTypes
{
public function getEventDispatcher(): object
{
return new class extends SomeEventDispatcher implements EventDispatcherInterface {
public function dispatch(object $event, string $eventName = null): object
{
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ class ClosureNestedType
}
}

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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Source\AnonymousClass;

use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

abstract class SomeEventDispatcher implements EventDispatcherInterface
{
}