Skip to content

Commit 40d87af

Browse files
authored
[TypeDeclaration] Skip class not found on ObjectTypedPropertyFromJMSSerializerAttributeTypeRector (#7774)
* [TypeDeclaration] Skip class not found on ObjectTypedPropertyFromJMSSerializerAttributeTypeRector * fix
1 parent 6a1aa20 commit 40d87af

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ObjectTypedPropertyFromJMSSerializerAttributeTypeRector\Fixture;
4+
5+
use JMS\Serializer\Annotation as JMS;
6+
7+
final class SkipClassNotFound
8+
{
9+
#[JMS\Type('SomeNonExistentClass')]
10+
protected $dynamicRef;
11+
}

rules/TypeDeclaration/NodeFactory/JMSTypePropertyTypeFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Name\FullyQualified;
910
use PhpParser\Node\Stmt\Property;
11+
use PHPStan\Reflection\ReflectionProvider;
1012
use PHPStan\Type\FloatType;
1113
use PHPStan\Type\MixedType;
1214
use PHPStan\Type\ObjectType;
@@ -25,6 +27,7 @@ public function __construct(
2527
private StaticTypeMapper $staticTypeMapper,
2628
private PhpDocInfoFactory $phpDocInfoFactory,
2729
private VarTagRemover $varTagRemover,
30+
private ReflectionProvider $reflectionProvider,
2831
) {
2932
}
3033

@@ -41,7 +44,13 @@ public function createObjectTypeNode(string $typeValue): ?Node
4144
$type = new ObjectType($typeValue);
4245
}
4346

44-
return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PROPERTY);
47+
$node = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PROPERTY);
48+
49+
if ($node instanceof FullyQualified && ! $this->reflectionProvider->hasClass($node->toString())) {
50+
return null;
51+
}
52+
53+
return $node;
4554
}
4655

4756
public function createScalarTypeNode(string $typeValue, Property $property): ?Node

0 commit comments

Comments
 (0)