Skip to content
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
24 changes: 24 additions & 0 deletions src/Types/Exception/TypeArgumentCountError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Types\Exception;

use ArgumentCountError;
use Exception;

use function sprintf;

final class TypeArgumentCountError extends Exception implements TypesException
{
public static function new(string $name, ArgumentCountError $previous): self
{
return new self(
sprintf(
'To register "%s" pass an instance to `Type::addType` instead.',
$name,
),
previous: $previous,
);
}
}
38 changes: 26 additions & 12 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Doctrine\DBAL\Types;

use ArgumentCountError;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\TypeArgumentCountError;

use function array_map;
use function is_string;

/**
* The base class for so-called Doctrine mapping types.
Expand Down Expand Up @@ -51,11 +54,6 @@

private static ?TypeRegistry $typeRegistry = null;

/** @internal Do not instantiate directly - use {@see Type::addType()} method instead. */
final public function __construct()
{
}

/**
* Converts a value from its PHP representation to its database representation
* of this type.
Expand Down Expand Up @@ -137,14 +135,22 @@
/**
* Adds a custom type to the type map.
*
* @param string $name The name of the type.
* @param class-string<Type> $className The class name of the custom type.
* @param string $name The name of the type.
* @param class-string<Type>|Type $type The custom type or the class name of the custom type.
*
* @throws Exception
*/
public static function addType(string $name, string $className): void
public static function addType(string $name, string|Type $type): void
{
self::getTypeRegistry()->register($name, new $className());
if (is_string($type)) {
try {
$type = new $type();
} catch (ArgumentCountError $e) { // @phpstan-ignore catch.neverThrown (it can be thrown)
throw TypeArgumentCountError::new($name, $e);
}
}

self::getTypeRegistry()->register($name, $type);
}

/**
Expand All @@ -162,13 +168,21 @@
/**
* Overrides an already defined type to use a different implementation.
*
* @param class-string<Type> $className
* @param class-string<Type>|Type $type The custom type or the class name of the custom type.
*
* @throws Exception
*/
public static function overrideType(string $name, string $className): void
public static function overrideType(string $name, string|Type $type): void
{
self::getTypeRegistry()->override($name, new $className());
if (is_string($type)) {
try {
$type = new $type();
} catch (ArgumentCountError $e) { // @phpstan-ignore catch.neverThrown (it can be thrown)
throw TypeArgumentCountError::new($name, $e);

Check warning on line 181 in src/Types/Type.php

View check run for this annotation

Codecov / codecov/patch

src/Types/Type.php#L180-L181

Added lines #L180 - L181 were not covered by tests
}
}

self::getTypeRegistry()->override($name, $type);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Types/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\DBAL\Tests\Types;

use Doctrine\DBAL\Types\Exception\TypeArgumentCountError;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -39,4 +40,19 @@ public static function defaultTypesProvider(): iterable
yield [$constantValue];
}
}

public function testAddTypeWhenTypeRequiresArguments(): void
{
self::expectException(TypeArgumentCountError::class);
self::expectExceptionMessage('To register "some_type" pass an instance to `Type::addType` instead.');

Type::addType('some_type', TypeWithConstructor::class);
}

public function testAddTypeInstance(): void
{
self::assertFalse(Type::hasType('some_type'));
Type::addType('some_type', new TypeWithConstructor(true));
self::assertTrue(Type::hasType('some_type'));
}
}
23 changes: 23 additions & 0 deletions tests/Types/TypeWithConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

class TypeWithConstructor extends Type
{
public function __construct(public bool $requirement)
{
}

/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return '';
}
}
Loading