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 Jan 13, 2025
1 parent 8c9efcf commit 57a74a7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/Types/Exception/TypeArgumentCountError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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('Type "%s" cannot be registered through Type::addType because it requires arguments. 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
10 changes: 10 additions & 0 deletions tests/Types/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\DBAL\Tests\Types;

use Doctrine\DBAL\Tests\Functional\Schema\Types\MoneyType;
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 +41,12 @@ public static function defaultTypesProvider(): iterable
yield [$constantValue];
}
}

public function testAddTypeWhenTypeRequiresArguments(): void
{
self::expectException(TypeArgumentCountError::class);
self::expectExceptionMessage('Type "some_type" cannot be registered through Type::addType because it requires arguments. Use Type::getTypeRegistry()->register instead.');

Type::addType('some_type', TypeWithConstructor::class);
}
}
20 changes: 20 additions & 0 deletions tests/Types/TypeWithConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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(bool $requirement)
{
}

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

0 comments on commit 57a74a7

Please sign in to comment.