Skip to content

Commit

Permalink
Array cache reflection in mapper (#94)
Browse files Browse the repository at this point in the history
* Array cache reflection in mapper

* Fix namespaces
  • Loading branch information
Exanlv authored Jun 27, 2024
1 parent e6cc773 commit 1faec74
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 22 deletions.
50 changes: 41 additions & 9 deletions src/Mapping/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@

class Mapper
{
/**
* @var ReflectionClass[]
*/
private array $reflectionClasses = [];

/**
* @var ReflectionProperty[]
*/
private array $reflectionProperties = [];

/**
* @var (ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null)[]
*/
private array $reflectionTypes = [];

public function map(mixed $source, string $definition): CompletedMapping
{
if (is_object($source)) {
Expand All @@ -34,7 +49,6 @@ public function map(mixed $source, string $definition): CompletedMapping

private function mapFromObject(mixed $source, string $definition): CompletedMapping
{
$reflection = new ReflectionClass($definition);
$instance = new $definition();

$errors = [];
Expand All @@ -44,7 +58,8 @@ private function mapFromObject(mixed $source, string $definition): CompletedMapp
try {
$this->setProperty(
$value,
$reflection->getProperty($key),
$this->getReflectionProperty($definition, $key),
$this->getReflectionType($definition, $key),
$instance,
$errors
);
Expand All @@ -59,15 +74,14 @@ private function mapFromObject(mixed $source, string $definition): CompletedMapp
private function setProperty(
mixed $value,
ReflectionProperty $reflectionProperty,
ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $reflectionType,
mixed &$instance,
array &$errors,
): void {
$type = $reflectionProperty->getType();

/**
* Typing should match for Union Types & non-set types
*/
if ($type instanceof ReflectionUnionType || is_null($type)) {
if ($reflectionType instanceof ReflectionUnionType || is_null($reflectionType)) {
$this->setFlat($reflectionProperty, $instance, $value, $errors);
return;
}
Expand All @@ -76,20 +90,20 @@ private function setProperty(
* IntersecionType is not used
* e.g. TypeA&TypeB
*/
if ($type instanceof ReflectionIntersectionType) {
if ($reflectionType instanceof ReflectionIntersectionType) {
$errors[] = new MappingException('Unsupported typing', $reflectionProperty->getName(), get_class($instance));
return;
}

/**
* Scalar types
*/
if (($type instanceof ReflectionNamedType && $type->isBuiltin())) {
$this->setNamedType($reflectionProperty, $type, $instance, $value, $errors);
if (($reflectionType instanceof ReflectionNamedType && $reflectionType->isBuiltin())) {
$this->setNamedType($reflectionProperty, $reflectionType, $instance, $value, $errors);
return;
}

$typeName = $type->getName();
$typeName = $reflectionType->getName();

if (enum_exists($typeName)) {
$this->setEnum($reflectionProperty, $instance, $value, $typeName, $errors);
Expand Down Expand Up @@ -240,4 +254,22 @@ private function mapEnumArray(array $values, ArrayMapping $arrayMapping, array &

return $new;
}

private function getReflectionProperty(string $definition, string $key): ReflectionProperty
{
if (isset($this->reflectionProperties[$definition][$key])) {
return $this->reflectionProperties[$definition][$key];
}

$reflectionClass = $this->reflectionClasses[$definition]
?? $this->reflectionClasses[$definition] = new ReflectionClass($definition);

return $this->reflectionProperties[$definition][$key] = $reflectionClass->getProperty($key);
}

private function getReflectionType(string $definition, string $key): ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null
{
return $this->reflectionTypes[$definition][$key]
?? $this->reflectionTypes[$definition][$key] = $this->getReflectionProperty($definition, $key)->getType();
}
}
6 changes: 4 additions & 2 deletions tests/Gateway/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Gateway;

use Exan\Eventer\Eventer;
use Fakes\Ragnarok\Fenrir\PromiseFake;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Mock;
use Mockery\MockInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Ragnarok\Fenrir\Bitwise\Bitwise;
use Ragnarok\Fenrir\Constants\MetaEvents;
use Ragnarok\Fenrir\Constants\WebsocketEvents;
use Ragnarok\Fenrir\DataMapper;
use Ragnarok\Fenrir\EventHandler;
use Ragnarok\Fenrir\Gateway\Connection;
use Ragnarok\Fenrir\Gateway\Helpers\PresenceUpdateBuilder;
use Ragnarok\Fenrir\Gateway\Objects\Payload;
use Ragnarok\Fenrir\Gateway\Shard;
use Ragnarok\Fenrir\Websocket;
use Ratchet\RFC6455\Messaging\MessageInterface;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
Expand Down
8 changes: 4 additions & 4 deletions tests/Gateway/Handlers/ReadyEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function payloadProvider(): array
'session_id' => '::session id::',
]
],
'expectation' => true,
'shouldSet' => true,
],

'No resume url' => [
Expand All @@ -114,7 +114,7 @@ public static function payloadProvider(): array
'session_id' => '::session id::',
]
],
'expectation' => false,
'shouldSet' => false,
],

'No session id' => [
Expand All @@ -124,14 +124,14 @@ public static function payloadProvider(): array
'resume_gateway_url' => '::resume gateway url::',
]
],
'expectation' => false,
'shouldSet' => false,
],

'No d' => [
'payload' => (object) [
't' => Events::READY,
],
'expectation' => false,
'shouldSet' => false,
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Rest/GlobalCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Rest;

use Ragnarok\Fenrir\Parts\ApplicationCommand;
use Ragnarok\Fenrir\Rest\GlobalCommand;
Expand Down
2 changes: 1 addition & 1 deletion tests/Rest/GuildCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Rest;

use Ragnarok\Fenrir\Parts\ApplicationCommand;
use Ragnarok\Fenrir\Rest\GuildCommand;
Expand Down
1 change: 0 additions & 1 deletion tests/Rest/GuildStickerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Tests\Ragnarok\Fenrir\Rest;

use Ragnarok\Fenrir\Parts\StickerPack;
use Ragnarok\Fenrir\Parts\Sticker;
use Ragnarok\Fenrir\Rest\GuildSticker;
use Ragnarok\Fenrir\Rest\Helpers\GuildSticker\ModifyStickerBuilder;
Expand Down
2 changes: 1 addition & 1 deletion tests/Rest/Helpers/Command/CommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Rest\Helpers\Command;

use PHPUnit\Framework\TestCase;
use Ragnarok\Fenrir\Bitwise\Bitwise;
Expand Down
2 changes: 1 addition & 1 deletion tests/Rest/Helpers/Command/CommandOptionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Rest\Helpers\Command;

use Mockery;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Rest/Helpers/GetNewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Rest\Helpers;

use Ragnarok\Fenrir\Rest\Helpers\GetNew;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Rest/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Ragnarok\Fenrir;
namespace Tests\Ragnarok\Fenrir\Rest;

use Ragnarok\Fenrir\Enums\InteractionCallbackType;
use Ragnarok\Fenrir\Interaction\Helpers\InteractionCallbackBuilder;
Expand Down

0 comments on commit 1faec74

Please sign in to comment.