Skip to content

Commit

Permalink
fix: change return type of hasOne and belongsTo to nullable object type.
Browse files Browse the repository at this point in the history
Maybe there is a different sollution than with UnionType, but I found no hints about setting the ObjectType as nullable one.
  • Loading branch information
novacuum authored Apr 10, 2024
1 parent 64cd6d1 commit 1d70e1a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion php-packages/phpstan/src/Relations/RelationProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1d70e1a

Please sign in to comment.