Skip to content

Commit

Permalink
Merge pull request #45 from ray-di/fix_nullable
Browse files Browse the repository at this point in the history
Fix nullable return type issue
  • Loading branch information
koriym authored Feb 22, 2023
2 parents ef3dda2 + 1f364d7 commit b5fd626
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ReturnEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use phpDocumentor\Reflection\Types\ContextFactory;
use phpDocumentor\Reflection\Types\Object_;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionType;

use function assert;
use function class_exists;
Expand All @@ -26,14 +28,24 @@ public function __invoke(ReflectionMethod $method): string|null
return null;
}

$returnTypeClass = (string) $returnType;
$returnTypeClass = $this->getReturnTypeName($returnType);

if (class_exists($returnTypeClass) && $returnTypeClass !== Pages::class) {
return $returnTypeClass;
}

return $this->docblock($method);
}

private function getReturnTypeName(ReflectionType $reflectionType): string
{
if ($reflectionType instanceof ReflectionNamedType) {
return $reflectionType->getName();
}

return (string) $reflectionType;
}

/** @return ?class-string */
private function docblock(ReflectionMethod $method): string|null
{
Expand Down
13 changes: 13 additions & 0 deletions tests/DbQueryModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Ray\MediaQuery\Entity\TodoConstruct;
use Ray\MediaQuery\Exception\InvalidPerPageVarNameException;
use Ray\MediaQuery\Exception\PerPageNotIntTypeException;
use Ray\MediaQuery\Fake\Queries\TodoEntityNullableInterface;
use Ray\MediaQuery\Fake\Queries\TodoFactoryInterface;
use Ray\MediaQuery\Fake\Queries\TodoFactoryUnionInterface;
use Ray\MediaQuery\Queries\DynamicPerPageInterface;
Expand Down Expand Up @@ -53,6 +54,7 @@ protected function setUp(): void
PromiseItemInterface::class,
PromiseListInterface::class,
TodoEntityInterface::class,
TodoEntityNullableInterface::class,
TodoConstcuctEntityInterface::class,
DynamicPerPageInterface::class,
DynamicPerPageInvalidInterface::class,
Expand Down Expand Up @@ -145,6 +147,17 @@ public function testEntity(): void
$this->assertInstanceOf(Todo::class, $item);
}

public function testEntityNullable(): void
{
/** @var TodoEntityInterface $todoList */
$todoList = $this->injector->getInstance(TodoEntityNullableInterface::class);
$list = $todoList->getList();
$this->assertInstanceOf(Todo::class, $list[0]);
$this->assertSame('run', $list[0]->title);
$item = $todoList->getItem('1');
$this->assertInstanceOf(Todo::class, $item);
}

public function testEntityWithConstructor(): void
{
/** @var TodoEntityInterface $todoList */
Expand Down
20 changes: 20 additions & 0 deletions tests/Fake/Queries/TodoEntityNullableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Ray\MediaQuery\Fake\Queries;

use Ray\MediaQuery\Annotation\DbQuery;
use Ray\MediaQuery\Entity\Todo;

interface TodoEntityNullableInterface
{
#[DbQuery('todo_item')]
public function getItem(string $id): ?Todo;

#[DbQuery('todo_list')]
/**
* @return array<Todo>
*/
public function getList(): ?array;
}

0 comments on commit b5fd626

Please sign in to comment.