Skip to content

Commit

Permalink
BigIntType - always uses integer in DBAL 4
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 9, 2024
1 parent d32e399 commit 8c36077
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/Type/Doctrine/Descriptors/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Composer\InstalledVersions;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function class_exists;
use function strpos;

class BigIntType implements DoctrineTypeDescriptor
{
Expand All @@ -18,6 +21,10 @@ public function getType(): string

public function getWritableToPropertyType(): Type
{
if ($this->hasDbal4()) {
return new IntegerType();
}

return TypeCombinator::intersect(new StringType(), new AccessoryNumericStringType());
}

Expand All @@ -31,4 +38,15 @@ public function getDatabaseInternalType(): Type
return new IntegerType();
}

private function hasDbal4(): bool
{
if (!class_exists(InstalledVersions::class)) {
return false;
}

$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');

return strpos($dbalVersion, '4.') === 0;
}

}
43 changes: 31 additions & 12 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Doctrine\CarbonImmutableType;
use Carbon\Doctrine\CarbonType;
use Composer\InstalledVersions;
use Doctrine\DBAL\Types\Type;
use Iterator;
use PHPStan\Rules\Rule;
Expand All @@ -23,6 +24,8 @@
use PHPStan\Type\Doctrine\Descriptors\SimpleArrayType;
use PHPStan\Type\Doctrine\Descriptors\StringType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use function array_unshift;
use function strpos;
use const PHP_VERSION_ID;

/**
Expand Down Expand Up @@ -103,11 +106,8 @@ public function testRule(?string $objectManagerLoader): void
{
$this->allowNullablePropertyForRequiredField = false;
$this->objectManagerLoader = $objectManagerLoader;
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
19,
],

$errors = [
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$one type mapping mismatch: database can contain string|null but property expects string.',
25,
Expand Down Expand Up @@ -168,7 +168,18 @@ public function testRule(?string $objectManagerLoader): void
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array<int> but database expects array<string>.',
162,
],
]);
];

$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
if (!$hasDbal4) {
array_unshift($errors, [
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
19,
]);
}

$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], $errors);
}

/**
Expand All @@ -178,11 +189,8 @@ public function testRuleWithAllowedNullableProperty(?string $objectManagerLoader
{
$this->allowNullablePropertyForRequiredField = true;
$this->objectManagerLoader = $objectManagerLoader;
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
19,
],

$errors = [
[
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$one type mapping mismatch: database can contain string|null but property expects string.',
25,
Expand Down Expand Up @@ -231,7 +239,18 @@ public function testRuleWithAllowedNullableProperty(?string $objectManagerLoader
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array<int> but database expects array<string>.',
162,
],
]);
];

$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
if (!$hasDbal4) {
array_unshift($errors, [
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
19,
]);
}

$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], $errors);
}

/**
Expand Down

0 comments on commit 8c36077

Please sign in to comment.