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

Remove final constructor for Type #6705

Open
wants to merge 3 commits into
base: 4.3.x
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ awareness about deprecated code.

# Upgrade to 4.3

## Deprecated Type::addType

Use `Type::getTypeRegistry()->register()` instead.

## Deprecated `Table::columnsAreIndexed()`

The `Table::columnsAreIndexed()` method has been deprecated.
Expand Down
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);

Check warning on line 17 in src/Types/Exception/TypeArgumentCountError.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line exceeds 120 characters; contains 195 characters
}
}
15 changes: 9 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 @@

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 +134,20 @@
/**
* Adds a custom type to the type map.
*
* @param string $name The name of the type.

Check failure on line 137 in src/Types/Type.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Incorrect order of annotations groups.
* @param class-string<Type> $className The class name of the custom type.
*
* @throws Exception
*
* @deprecated Use Type::getTypeRegistry()->register() instead.
*/
public static function addType(string $name, string $className): void
{
self::getTypeRegistry()->register($name, new $className());
try {

Check failure on line 146 in src/Types/Type.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line indented incorrectly; expected 8 spaces, found 7
self::getTypeRegistry()->register($name, new $className());

Check failure on line 147 in src/Types/Type.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line indented incorrectly; expected at least 12 spaces, found 11
} catch (ArgumentCountError $e) {

Check failure on line 148 in src/Types/Type.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line indented incorrectly; expected 8 spaces, found 7
throw TypeArgumentCountError::new($name, $e);

Check failure on line 149 in src/Types/Type.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line indented incorrectly; expected at least 12 spaces, found 11
}

Check failure on line 150 in src/Types/Type.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line indented incorrectly; expected 8 spaces, found 7
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Schema/CustomIntrospectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
self::markTestSkipped('Skip on Oracle');
}

Type::addType(MoneyType::NAME, MoneyType::class);
Type::getTypeRegistry()->register(MoneyType::NAME, new MoneyType);

Check failure on line 32 in tests/Functional/Schema/CustomIntrospectionTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Parentheses must be used when instantiating a new class
}

public function testCustomColumnIntrospection(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Schema/MySQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
public static function setUpBeforeClass(): void
{
Type::addType('point', PointType::class);
Type::getTypeRegistry()->register('point', new PointType);

Check failure on line 30 in tests/Functional/Schema/MySQLSchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Parentheses must be used when instantiating a new class
}

protected function supportsPlatform(AbstractPlatform $platform): bool
Expand Down Expand Up @@ -591,7 +591,7 @@

public function testSchemaDiffWithCustomColumnTypeWhileDatabaseTypeDiffers(): void
{
Type::addType('custom_type', CustomType::class);
Type::getTypeRegistry()->register('custom_type', new CustomType);

Check failure on line 594 in tests/Functional/Schema/MySQLSchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Parentheses must be used when instantiating a new class

$metadataTable = new Table('table_with_custom_type');
$metadataTable->addColumn('col1', 'custom_type');
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
$table = $this->connection->createSchemaManager()->introspectTable('domain_type_test');
self::assertInstanceOf(DecimalType::class, $table->getColumn('value')->getType());

Type::addType('MyMoney', MoneyType::class);
Type::getTypeRegistry()->register('MyMoney', new MoneyType);

Check failure on line 56 in tests/Functional/Schema/PostgreSQLSchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Parentheses must be used when instantiating a new class
$this->connection->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'MyMoney');

$table = $this->connection->createSchemaManager()->introspectTable('domain_type_test');
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
if (Type::hasType($type->getName())) {
Type::overrideType($type->getName(), $type::class);
} else {
Type::addType($type->getName(), $type::class);
Type::getTypeRegistry()->register($type->getName(), new $type);
}

self::assertSame($type->getName(), $this->platform->getDoctrineTypeMapping('TeStTyPe'));
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 @@
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.');

Check warning on line 48 in tests/Types/TypeTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (PHP: 8.4)

Line exceeds 120 characters; contains 178 characters

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)

Check failure on line 12 in tests/Types/TypeWithConstructor.php

View workflow job for this annotation

GitHub Actions / Static Analysis / PHPStan (PHP: 8.4)

Constructor of class Doctrine\DBAL\Tests\Types\TypeWithConstructor has an unused parameter $requirement.
{
}

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