Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore reverse lookup determinism #6097

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Types/Exception/TypeAlreadyRegistered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Types\Exception;

use Doctrine\DBAL\Types\Type;
use Exception;

use function get_debug_type;
use function spl_object_hash;
use function sprintf;

/** @psalm-immutable */
final class TypeAlreadyRegistered extends Exception implements TypesException
{
public static function new(Type $type): self
{
return new self(sprintf(
'Type of the class %s@%s is already registered.',
get_debug_type($type),
spl_object_hash($type),
));
}
}
10 changes: 10 additions & 0 deletions src/Types/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Types\Exception\TypeAlreadyRegistered;
use Doctrine\DBAL\Types\Exception\TypeNotFound;
use Doctrine\DBAL\Types\Exception\TypeNotRegistered;
use Doctrine\DBAL\Types\Exception\TypesAlreadyExists;
use Doctrine\DBAL\Types\Exception\UnknownColumnType;

use function array_search;
use function in_array;

/**
* The type registry is responsible for holding a map of all known DBAL types.
Expand Down Expand Up @@ -71,6 +73,10 @@ public function register(string $name, Type $type): void
throw TypesAlreadyExists::new($name);
}

if (array_search($type, $this->instances, true) !== false) {
throw TypeAlreadyRegistered::new($type);
}

$this->instances[$name] = $type;
}

Expand All @@ -85,6 +91,10 @@ public function override(string $name, Type $type): void
throw TypeNotFound::new($name);
}

if (! in_array(array_search($type, $this->instances, true), [$name, false], true)) {
throw TypeAlreadyRegistered::new($type);
}

$this->instances[$name] = $type;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Types/Exception/TypeAlreadyRegisteredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Types\Exception;

use Doctrine\DBAL\Types\Exception\TypeAlreadyRegistered;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;

class TypeAlreadyRegisteredTest extends TestCase
{
public function testNew(): void
{
$exception = TypeAlreadyRegistered::new(Type::getType('string'));

self::assertMatchesRegularExpression(
'/Type of the class Doctrine\\\DBAL\\\Types\\\StringType@([0-9a-zA-Z]+) is already registered./',
$exception->getMessage(),
);
}
}
16 changes: 12 additions & 4 deletions tests/Types/TypeRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ public function testRegisterWithAlreadyRegisteredInstance(): void
$newType = new TextType();

$this->registry->register('type1', $newType);
$this->expectException(Exception::class);
$this->registry->register('type2', $newType);
self::assertSame(
$this->registry->get('type1'),
$this->registry->get('type2'),
);
}

public function testOverride(): void
Expand Down Expand Up @@ -129,6 +126,17 @@ public function testOverrideWithUnknownType(): void
$this->registry->override('unknown', new TextType());
}

public function testOverrideWithAlreadyRegisteredInstance(): void
{
$newType = new TextType();

$this->registry->register('first', $newType);
$this->registry->register('second', new StringType());

$this->expectException(Exception::class);
$this->registry->override('second', $newType);
}

public function testGetMap(): void
{
$registeredTypes = $this->registry->getMap();
Expand Down