Skip to content

Commit

Permalink
Restore reverse lookup determinism
Browse files Browse the repository at this point in the history
In 96c2ebf, it looks like I wrongly
assumed I wouldn't be able to allow having 2 classes of the same type
in the type registry without also allowing having 2 objects of the same
type. array_search can perfectly tell the difference between both
situations, so let us continue forbidding that last part, it will allow
us to make sure a given type matches exactly one name.
  • Loading branch information
greg0ire committed Jul 15, 2023
1 parent 6982657 commit 94703ef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
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),
));
}
}
5 changes: 5 additions & 0 deletions src/Types/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -71,6 +72,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 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(),
);
}
}
5 changes: 1 addition & 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

0 comments on commit 94703ef

Please sign in to comment.