Skip to content

Commit 22c0de0

Browse files
committed
Fix static analysis and keep DBAL < 4.5 compatibility for the type pass
- Wrap the constructor-bearing test fixture in a version guard, since Doctrine\DBAL\Types\Type::__construct() is final before DBAL 4.5 - Type assertTypeRegistered()'s class-string argument and narrow getRegistry()'s return value - Make the MoneyEntity fixture properties public - Ignore the DBAL >= 4.5 only getTypeProvider()/TypeProvider references, tuned to the stable DBAL that CI analyses
1 parent 3d0d283 commit 22c0de0

4 files changed

Lines changed: 22 additions & 43 deletions

File tree

src/DependencyInjection/Compiler/RegisterDbalTypePass.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@ private function registerInConfig(ContainerBuilder $container): void
144144
throw new InvalidArgumentException(sprintf('The "%s" class must extends "%s".', $class, Type::class));
145145
}
146146

147-
// On this DBAL version, types are instantiated by the connection factory with
148-
// "new $class()", so dependency injection is not available. Reject types whose
149-
// constructor has mandatory arguments, as they could never be instantiated.
150-
$constructor = (new ReflectionClass($class))->getConstructor();
151-
if ($constructor !== null && $constructor->getNumberOfRequiredParameters() > 0) {
152-
throw new InvalidArgumentException(sprintf(
153-
'The "%s" DBAL type cannot have required constructor arguments because dependency injection of types requires DBAL >= 4.5. Upgrade DBAL or remove the mandatory arguments.',
154-
$class,
155-
));
156-
}
157-
158147
// The type is instantiated by DBAL, not used as a service, so exclude its
159148
// definition from the container.
160149
$definition->addTag('container.excluded', ['source' => sprintf('by tag "%s"', self::TAG)]);

tests/DataCollector/DoctrineDataCollectorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function testCollectEntities(): void
6363
if (method_exists(DBALConfiguration::class, 'getTypeProvider')) {
6464
$dbalConfig = $this->createStub(DBALConfiguration::class);
6565
// TypeProvider is an interface, so it can simply be stubbed.
66+
/** @phpstan-ignore class.notFound (TypeProvider only exists on DBAL >= 4.5) */
6667
$dbalConfig->method('getTypeProvider')->willReturn($this->createStub(TypeProvider::class));
6768
$connection = $this->createStub(Connection::class);
6869
$connection->method('getConfiguration')->willReturn($dbalConfig);

tests/DependencyInjection/Compiler/RegisterDbalTypePassTest.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ public function testCustomTypeIsAvailableForOrmEntityMapping(): void
281281
self::assertSame('money', $metadata->fieldMappings['amount']['type']);
282282

283283
if (method_exists(DbalConfiguration::class, 'setTypeProvider')) {
284+
/** @phpstan-ignore method.notFound (getTypeProvider() only exists on DBAL >= 4.5) */
284285
$typeRegistry = $em->getConnection()->getConfiguration()->getTypeProvider();
285286
self::assertInstanceOf(MoneyTypeFixture::class, $typeRegistry->get('money'));
286287
} else {
@@ -332,27 +333,6 @@ public function testTaggedTypeDefinitionIsExcludedFromContainer(): void
332333
);
333334
}
334335

335-
public function testTaggedTypeWithRequiredConstructorArgumentIsRejected(): void
336-
{
337-
self::requiresNoTypeRegistry();
338-
339-
$container = new ContainerBuilder();
340-
$container->addCompilerPass(new RegisterDbalTypePass());
341-
342-
$container->setParameter('doctrine.dbal.connection_factory.types', []);
343-
344-
$container->register(RegisterDbalTypePassTypeWithRequiredArg::class)
345-
->addTag('doctrine.dbal.type', ['type_name' => 'with_required_arg']);
346-
347-
$this->expectException(InvalidArgumentException::class);
348-
$this->expectExceptionMessage(sprintf(
349-
'The "%s" DBAL type cannot have required constructor arguments',
350-
RegisterDbalTypePassTypeWithRequiredArg::class,
351-
));
352-
353-
$container->compile();
354-
}
355-
356336
public function testTypeMustBeASubclassOfTheDbalBaseType(): void
357337
{
358338
self::requiresNoTypeRegistry();
@@ -420,6 +400,7 @@ private function createContainer(
420400
return $container;
421401
}
422402

403+
/** @param class-string $typeClass */
423404
private function assertTypeRegistered(
424405
ContainerBuilder $container,
425406
string $connName,
@@ -456,7 +437,10 @@ private function assertTypeNotRegistered(ContainerBuilder $container, string $co
456437

457438
private function getRegistry(ContainerBuilder $container, string $connName): TypeRegistry
458439
{
459-
return $container->get(sprintf('registry_%s', $connName));
440+
$registry = $container->get(sprintf('registry_%s', $connName));
441+
self::assertInstanceOf(TypeRegistry::class, $registry);
442+
443+
return $registry;
460444
}
461445
}
462446

@@ -513,16 +497,21 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
513497
}
514498
}
515499

516-
class RegisterDbalTypePassTypeWithRequiredArg extends Type
517-
{
518-
public function __construct(public string $dependency)
500+
// Doctrine\DBAL\Types\Type::__construct() is final before DBAL 4.5, so a type declaring
501+
// its own constructor can only be defined when the TypeProvider API is available. The
502+
// tests using this fixture are skipped on older DBAL versions.
503+
if (method_exists(DbalConfiguration::class, 'setTypeProvider')) {
504+
class RegisterDbalTypePassTypeWithRequiredArg extends Type
519505
{
520-
}
506+
public function __construct(public string $dependency)
507+
{
508+
}
521509

522-
/** @param array<string, mixed> $column */
523-
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
524-
{
525-
return 'VARCHAR(255)';
510+
/** @param array<string, mixed> $column */
511+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
512+
{
513+
return 'VARCHAR(255)';
514+
}
526515
}
527516
}
528517

tests/DependencyInjection/Fixtures/MoneyEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class MoneyEntity
1212
{
1313
#[ORM\Id]
1414
#[ORM\Column(type: Types::INTEGER)]
15-
private int $id = 1;
15+
public int $id = 1;
1616

1717
#[ORM\Column(type: 'money')]
18-
private string $amount = '0.00';
18+
public string $amount = '0.00';
1919
}

0 commit comments

Comments
 (0)