Skip to content

Commit

Permalink
Fix BIGINT validation
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Apr 15, 2024
1 parent 8709fb3 commit f756989
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ class SchemaValidator
* It maps built-in Doctrine types to PHP types
*/
private const BUILTIN_TYPES_MAP = [
AsciiStringType::class => 'string',
BigIntType::class => 'string',
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',
AsciiStringType::class => ['string'],
BigIntType::class => ['int', 'string'],
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(EntityManagerInterface $em, bool $validatePropertyTypes = true)
Expand Down Expand Up @@ -390,21 +390,21 @@ function (array $fieldMapping) use ($class): ?string {
$propertyType = $propertyType->getName();

// If the property type is the same as the metadata field type, we are ok
if ($propertyType === $metadataFieldType) {
if (in_array($propertyType, $metadataFieldType, true)) {
return null;
}

if (is_a($propertyType, BackedEnum::class, true)) {
$backingType = (string) (new ReflectionEnum($propertyType))->getBackingType();

if ($metadataFieldType !== $backingType) {
if (! in_array($backingType, $metadataFieldType, true)) {
return sprintf(
"The field '%s#%s' has the property type '%s' with a backing type of '%s' that differs from the metadata field type '%s'.",
$class->name,
$fieldName,
$propertyType,
$backingType,
$metadataFieldType
implode('|', $metadataFieldType)
);
}

Expand All @@ -429,7 +429,7 @@ function (array $fieldMapping) use ($class): ?string {
) {
$backingType = (string) (new ReflectionEnum($fieldMapping['enumType']))->getBackingType();

if ($metadataFieldType === $backingType) {
if (in_array($backingType, $metadataFieldType, true)) {
return null;
}

Expand All @@ -439,7 +439,7 @@ function (array $fieldMapping) use ($class): ?string {
$fieldName,
$fieldMapping['enumType'],
$backingType,
$metadataFieldType
implode('|', $metadataFieldType)
);
}

Expand All @@ -455,7 +455,7 @@ function (array $fieldMapping) use ($class): ?string {
$class->name,
$fieldName,
$propertyType,
$metadataFieldType,
implode('|', $metadataFieldType),
$fieldMapping['type']
);
},
Expand All @@ -468,8 +468,10 @@ function (array $fieldMapping) use ($class): ?string {
/**
* The exact DBAL type must be used (no subclasses), since consumers of doctrine/orm may have their own
* customization around field types.
*
* @return list<string>|null
*/
private function findBuiltInType(Type $type): ?string
private function findBuiltInType(Type $type): ?array
{
$typeName = get_class($type);

Expand Down
27 changes: 27 additions & 0 deletions tests/Tests/Models/BigIntegers/BigIntegers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\BigIntegers;

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class BigIntegers
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
public ?int $id = null;

/** @ORM\Column(type="bigint") */
public int $one = 1;

/** @ORM\Column(type="bigint") */
public string $two = '2';

/** @ORM\Column(type="bigint") */
public float $three = 3.0;
}
14 changes: 14 additions & 0 deletions tests/Tests/ORM/Tools/SchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\ORM\Mapping\OrderBy;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\Tests\Models\BigIntegers\BigIntegers;
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
use Doctrine\Tests\OrmTestCase;

Expand Down Expand Up @@ -240,6 +241,19 @@ public function testInvalidAssociationTowardsMappedSuperclass(): void
$ce
);
}

/**
* @requires PHP 7.4
*/
public function testBigIntProperty(): void
{
$class = $this->em->getClassMetadata(BigIntegers::class);

self::assertSame(
['The field \'Doctrine\Tests\Models\BigIntegers\BigIntegers#three\' has the property type \'float\' that differs from the metadata field type \'int|string\' returned by the \'bigint\' DBAL type.'],
$this->validator->validateClass($class)
);
}
}

/** @MappedSuperclass */
Expand Down

0 comments on commit f756989

Please sign in to comment.