Skip to content

Commit

Permalink
Remove final constructor for Type
Browse files Browse the repository at this point in the history
  • Loading branch information
ruudk committed Feb 11, 2025
1 parent d199a3e commit ca6bc55
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/Types/Exception/TypeArgumentCountError.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 ArgumentCountError;
use Exception;

use function sprintf;

/** @psalm-immutable */
final class TypeArgumentCountError extends Exception implements TypesException
{
public static function new(string $name, ArgumentCountError $previous): self
{
return new self(
sprintf(
'To register "%s" use Type::getTypeRegistry()->register instead.',
$name,
),
previous: $previous,
);
}
}
13 changes: 7 additions & 6 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

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;

Expand Down Expand Up @@ -51,11 +53,6 @@ abstract class Type

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 @@ -144,7 +141,11 @@ public static function lookupName(self $type): string
*/
public static function addType(string $name, string $className): void
{
self::getTypeRegistry()->register($name, new $className());
try {
self::getTypeRegistry()->register($name, new $className());
} catch (ArgumentCountError $e) {
throw TypeArgumentCountError::new($name, $e);
}
}

/**
Expand Down
9 changes: 9 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,12 @@ public static function defaultTypesProvider(): iterable
yield [$constantValue];
}
}

public function testAddTypeWhenTypeRequiresArguments(): void
{
self::expectException(TypeArgumentCountError::class);
self::expectExceptionMessage('To register "some_type" use Type::getTypeRegistry()->register instead.');

Type::addType('some_type', TypeWithConstructor::class);
}
}
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(private bool $requirement)
{
}

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

0 comments on commit ca6bc55

Please sign in to comment.