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

Allow overriding the constructor #5528

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 0 additions & 7 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ abstract class Type

private static ?TypeRegistry $typeRegistry = null;

/**
* @internal Do not instantiate directly - use {@see Type::addType()} method instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A type registered through addType is still required to have no arguments in the constructor. We still need a check for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we need to deprecate the static Type methods that accept string $className in favor of TypeRegistry and the corresponding counterparts which accept Type $type.

Only after the deprecated methods have been removed, this constraint could be dropped.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #5530

*/
final public function __construct()
{
}

/**
* Converts a value from its PHP representation to its database representation
* of this type.
Expand Down
26 changes: 26 additions & 0 deletions tests/Types/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

namespace Doctrine\DBAL\Tests\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use ReflectionClass;

use function strtr;

class TypeTest extends TestCase
{
/**
Expand All @@ -34,4 +38,26 @@ public function defaultTypesProvider(): iterable
yield [$constantValue];
}
}

/**
* @doesNotPerformAssertions
*/
public function testOverridingTheConstructorIsAllowed(): void
{
new class (['a' => 'b']) extends StringType
{
/**
* @param array<string, string> $replacements
*/
public function __construct(
private array $replacements,
) {
}

public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): string
{
return strtr($value, $this->replacements);
}
};
}
}