Skip to content

Commit

Permalink
SchemaValidator: Changing mapping of BIGINT to string|int
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLandauer committed Mar 22, 2024
1 parent 9c56071 commit e44b245
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use function in_array;
use function interface_exists;
use function is_a;
use function method_exists;
use function sprintf;

/**
Expand All @@ -49,9 +50,9 @@
class SchemaValidator
{
/**
* It maps built-in Doctrine types to PHP types
* Map built-in Doctrine DBAL 3 types to PHP types
*/
private const BUILTIN_TYPES_MAP = [
private const BUILTIN_TYPES_MAP_DBAL3 = [
AsciiStringType::class => 'string',
BigIntType::class => 'string',
BooleanType::class => 'bool',
Expand All @@ -66,6 +67,24 @@ class SchemaValidator
TextType::class => 'string',
];

/**
* Map built-in Doctrine DBAL 4+ types to PHP types
*/
private const BUILTIN_TYPES_MAP = [
AsciiStringType::class => 'string',
BigIntType::class => 'string|int',
BooleanType::class => 'bool',
DecimalType::class => 'string',
FloatType::class => 'float',
GuidType::class => 'string',
IntegerType::class => 'int',
JsonType::class => 'array',
SimpleArrayType::class => 'array',
SmallIntType::class => 'int',
StringType::class => 'string',
TextType::class => 'string',
];

public function __construct(
private readonly EntityManagerInterface $em,
private readonly bool $validatePropertyTypes = true,
Expand Down Expand Up @@ -436,6 +455,10 @@ private function findBuiltInType(Type $type): string|null
{
$typeName = $type::class;

return self::BUILTIN_TYPES_MAP[$typeName] ?? null;
if (method_exists(BigIntType::class, 'getName')) { // DBAL 3
return self::BUILTIN_TYPES_MAP_DBAL3[$typeName] ?? null;
} else { // DBAL 4+
return self::BUILTIN_TYPES_MAP[$typeName] ?? null;
}
}
}
19 changes: 19 additions & 0 deletions tests/Tests/ORM/Tools/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorMap;
Expand Down Expand Up @@ -228,6 +229,14 @@ public function testInvalidAssociationTowardsMappedSuperclass(): void
$ce,
);
}

public function testBigintMappedToStringInt(): void
{
$class = $this->em->getClassMetadata(BigintMappedToStringInt::class);
$ce = $this->validator->validateClass($class);

Check failure on line 237 in tests/Tests/ORM/Tools/SchemaValidatorTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Whitespace found at end of line
$this->assertEquals([], $ce);
}
}

#[MappedSuperclass]
Expand Down Expand Up @@ -547,3 +556,13 @@ class InvalidMappedSuperClass
#[ManyToMany(targetEntity: 'InvalidMappedSuperClass', mappedBy: 'invalid')]
private $selfWhatever;
}

#[Entity]
class BigintMappedToStringInt
{
#[Id, Column, GeneratedValue]

Check failure on line 563 in tests/Tests/ORM/Tools/SchemaValidatorTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

3 attributes are joined.
private int $id;

#[Column(type: Types::BIGINT)]
private string|int $bigint;
}

0 comments on commit e44b245

Please sign in to comment.