diff --git a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/AllAssignNodePropertyTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/AllAssignNodePropertyTypeInferer.php index caedd8ab7df..84166b8db44 100644 --- a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/AllAssignNodePropertyTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/AllAssignNodePropertyTypeInferer.php @@ -4,6 +4,7 @@ namespace Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Property; +use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\TypeDeclaration\Contract\TypeInferer\PropertyTypeInfererInterface; @@ -24,8 +25,12 @@ final class AllAssignNodePropertyTypeInferer extends AbstractTypeInferer impleme public function inferProperty(Property $property): Type { - /** @var ClassLike $class */ + /** @var ClassLike|null $class */ $class = $property->getAttribute(AttributeKey::CLASS_NODE); + if ($class === null) { + // anonymous class + return new MixedType(); + } $propertyName = $this->nameResolver->getName($property); diff --git a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php index 4073afc3d37..d2f4f3443f0 100644 --- a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php @@ -28,8 +28,12 @@ final class ConstructorPropertyTypeInferer extends AbstractTypeInferer implement { public function inferProperty(Property $property): Type { - /** @var Class_ $class */ + /** @var Class_|null $class */ $class = $property->getAttribute(AttributeKey::CLASS_NODE); + if ($class === null) { + // anonymous class + return new MixedType(); + } $classMethod = $class->getMethod('__construct'); if ($classMethod === null) { diff --git a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterPropertyTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterPropertyTypeInferer.php index d2ff3235e68..f9c670151f9 100644 --- a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterPropertyTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterPropertyTypeInferer.php @@ -45,8 +45,12 @@ final class GetterPropertyTypeInferer extends AbstractTypeInferer implements Pro public function inferProperty(Property $property): Type { - /** @var Class_ $class */ + /** @var Class_|null $class */ $class = $property->getAttribute(AttributeKey::CLASS_NODE); + if ($class === null) { + // anonymous class + return new MixedType(); + } /** @var string $propertyName */ $propertyName = $this->nameResolver->getName($property); diff --git a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterTypeDeclarationPropertyTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterTypeDeclarationPropertyTypeInferer.php index 9fe1bc12031..7be9f7cc4d9 100644 --- a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterTypeDeclarationPropertyTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/GetterTypeDeclarationPropertyTypeInferer.php @@ -29,8 +29,12 @@ final class GetterTypeDeclarationPropertyTypeInferer extends AbstractTypeInferer public function inferProperty(Property $property): Type { - /** @var Class_ $class */ + /** @var Class_|null $class */ $class = $property->getAttribute(AttributeKey::CLASS_NODE); + if ($class === null) { + // anonymous class + return new MixedType(); + } /** @var string $propertyName */ $propertyName = $this->nameResolver->getName($property); diff --git a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/SingleMethodAssignedNodePropertyTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/SingleMethodAssignedNodePropertyTypeInferer.php index e794fe84f44..eb14375180e 100644 --- a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/SingleMethodAssignedNodePropertyTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/SingleMethodAssignedNodePropertyTypeInferer.php @@ -19,8 +19,12 @@ final class SingleMethodAssignedNodePropertyTypeInferer extends AbstractTypeInfe { public function inferProperty(Property $property): Type { - /** @var Class_ $class */ + /** @var Class_|null $class */ $class = $property->getAttribute(AttributeKey::CLASS_NODE); + if ($class === null) { + // anonymous class + return new MixedType(); + } $classMethod = $class->getMethod('__construct'); if ($classMethod === null) { diff --git a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/skip_anonymous_class.php.inc b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/skip_anonymous_class.php.inc new file mode 100644 index 00000000000..49cb543472c --- /dev/null +++ b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/Fixture/skip_anonymous_class.php.inc @@ -0,0 +1,15 @@ +bar = $bar; + } +} diff --git a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/PropertyTypeDeclarationRectorTest.php b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/PropertyTypeDeclarationRectorTest.php index 07db7cb8ac7..76919f54223 100644 --- a/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/PropertyTypeDeclarationRectorTest.php +++ b/packages/TypeDeclaration/tests/Rector/Property/PropertyTypeDeclarationRector/PropertyTypeDeclarationRectorTest.php @@ -35,6 +35,7 @@ final class PropertyTypeDeclarationRectorTest extends AbstractRectorTestCase yield [__DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc']; yield [__DIR__ . '/Fixture/setter_type.php.inc']; yield [__DIR__ . '/Fixture/skip_multi_vars.php.inc']; + yield [__DIR__ . '/Fixture/skip_anonymous_class.php.inc']; } protected function getRectorClass(): string