From 1d70e1a9548f3706c3ca5a23992fcf5b8521fa1f Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 10 Apr 2024 11:40:22 +0200 Subject: [PATCH] fix: change return type of hasOne and belongsTo to nullable object type. Maybe there is a different sollution than with UnionType, but I found no hints about setting the ObjectType as nullable one. --- php-packages/phpstan/src/Relations/RelationProperty.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/php-packages/phpstan/src/Relations/RelationProperty.php b/php-packages/phpstan/src/Relations/RelationProperty.php index 108ceee570..2996d580c9 100644 --- a/php-packages/phpstan/src/Relations/RelationProperty.php +++ b/php-packages/phpstan/src/Relations/RelationProperty.php @@ -18,6 +18,8 @@ use PHPStan\Type\Generic\GenericObjectType; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; +use PHPStan\Type\NullType; +use PHPStan\Type\UnionType; class RelationProperty implements PropertyReflection { @@ -66,7 +68,10 @@ public function getReadableType(): Type case 'hasOne': case 'belongsTo': - return new ObjectType($this->methodCall->arguments[1]->class->toString()); + return new UnionType([ + new ObjectType($this->methodCall->arguments[1]->class->toString()), + new NullType(), + ]); default: throw new Exception('Unknown relationship type for relation: '.$this->methodCall->methodName);