Skip to content

Commit 8c36077

Browse files
committed
BigIntType - always uses integer in DBAL 4
1 parent d32e399 commit 8c36077

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/Type/Doctrine/Descriptors/BigIntType.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PHPStan\Type\Doctrine\Descriptors;
44

5+
use Composer\InstalledVersions;
56
use PHPStan\Type\Accessory\AccessoryNumericStringType;
67
use PHPStan\Type\IntegerType;
78
use PHPStan\Type\StringType;
89
use PHPStan\Type\Type;
910
use PHPStan\Type\TypeCombinator;
11+
use function class_exists;
12+
use function strpos;
1013

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

1922
public function getWritableToPropertyType(): Type
2023
{
24+
if ($this->hasDbal4()) {
25+
return new IntegerType();
26+
}
27+
2128
return TypeCombinator::intersect(new StringType(), new AccessoryNumericStringType());
2229
}
2330

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

41+
private function hasDbal4(): bool
42+
{
43+
if (!class_exists(InstalledVersions::class)) {
44+
return false;
45+
}
46+
47+
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
48+
49+
return strpos($dbalVersion, '4.') === 0;
50+
}
51+
3452
}

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Carbon\Doctrine\CarbonImmutableType;
66
use Carbon\Doctrine\CarbonType;
7+
use Composer\InstalledVersions;
78
use Doctrine\DBAL\Types\Type;
89
use Iterator;
910
use PHPStan\Rules\Rule;
@@ -23,6 +24,8 @@
2324
use PHPStan\Type\Doctrine\Descriptors\SimpleArrayType;
2425
use PHPStan\Type\Doctrine\Descriptors\StringType;
2526
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
27+
use function array_unshift;
28+
use function strpos;
2629
use const PHP_VERSION_ID;
2730

2831
/**
@@ -103,11 +106,8 @@ public function testRule(?string $objectManagerLoader): void
103106
{
104107
$this->allowNullablePropertyForRequiredField = false;
105108
$this->objectManagerLoader = $objectManagerLoader;
106-
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
107-
[
108-
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
109-
19,
110-
],
109+
110+
$errors = [
111111
[
112112
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$one type mapping mismatch: database can contain string|null but property expects string.',
113113
25,
@@ -168,7 +168,18 @@ public function testRule(?string $objectManagerLoader): void
168168
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array<int> but database expects array<string>.',
169169
162,
170170
],
171-
]);
171+
];
172+
173+
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
174+
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
175+
if (!$hasDbal4) {
176+
array_unshift($errors, [
177+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
178+
19,
179+
]);
180+
}
181+
182+
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], $errors);
172183
}
173184

174185
/**
@@ -178,11 +189,8 @@ public function testRuleWithAllowedNullableProperty(?string $objectManagerLoader
178189
{
179190
$this->allowNullablePropertyForRequiredField = true;
180191
$this->objectManagerLoader = $objectManagerLoader;
181-
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], [
182-
[
183-
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
184-
19,
185-
],
192+
193+
$errors = [
186194
[
187195
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$one type mapping mismatch: database can contain string|null but property expects string.',
188196
25,
@@ -231,7 +239,18 @@ public function testRuleWithAllowedNullableProperty(?string $objectManagerLoader
231239
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$invalidSimpleArray type mapping mismatch: property can contain array<int> but database expects array<string>.',
232240
162,
233241
],
234-
]);
242+
];
243+
244+
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
245+
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
246+
if (!$hasDbal4) {
247+
array_unshift($errors, [
248+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',
249+
19,
250+
]);
251+
}
252+
253+
$this->analyse([__DIR__ . '/data/MyBrokenEntity.php'], $errors);
235254
}
236255

237256
/**

0 commit comments

Comments
 (0)