Skip to content

Commit efa4488

Browse files
Fix BinaryType with dbal 4
1 parent cd42b17 commit efa4488

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

src/Type/Doctrine/Descriptors/BinaryType.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace PHPStan\Type\Doctrine\Descriptors;
44

5+
use Composer\InstalledVersions;
56
use PHPStan\Type\MixedType;
67
use PHPStan\Type\ResourceType;
78
use PHPStan\Type\StringType;
89
use PHPStan\Type\Type;
10+
use function class_exists;
11+
use function strpos;
912

1013
class BinaryType implements DoctrineTypeDescriptor
1114
{
@@ -17,6 +20,10 @@ public function getType(): string
1720

1821
public function getWritableToPropertyType(): Type
1922
{
23+
if ($this->hasDbal4()) {
24+
return new StringType();
25+
}
26+
2027
return new ResourceType();
2128
}
2229

@@ -30,4 +37,18 @@ public function getDatabaseInternalType(): Type
3037
return new StringType();
3138
}
3239

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

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,26 @@ public function testSuperclass(?string $objectManagerLoader): void
271271
{
272272
$this->allowNullablePropertyForRequiredField = false;
273273
$this->objectManagerLoader = $objectManagerLoader;
274-
$this->analyse([__DIR__ . '/data/MyBrokenSuperclass.php'], [
275-
[
276-
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenSuperclass::$five type mapping mismatch: database can contain resource but property expects int.',
277-
17,
278-
],
279-
]);
274+
275+
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
276+
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;
277+
if ($hasDbal4) {
278+
$errors = [
279+
[
280+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenSuperclass::$five type mapping mismatch: database can contain string but property expects int.',
281+
17,
282+
],
283+
];
284+
} else {
285+
$errors = [
286+
[
287+
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenSuperclass::$five type mapping mismatch: database can contain resource but property expects int.',
288+
17,
289+
],
290+
];
291+
}
292+
293+
$this->analyse([__DIR__ . '/data/MyBrokenSuperclass.php'], $errors);
280294
}
281295

282296
/**
@@ -495,4 +509,30 @@ public function testBugSingleEnum(?string $objectManagerLoader): void
495509
$this->analyse([__DIR__ . '/data/bug-single-enum.php'], []);
496510
}
497511

512+
public function testBug659(?string $objectManagerLoader): void
513+
{
514+
$this->allowNullablePropertyForRequiredField = false;
515+
$this->objectManagerLoader = $objectManagerLoader;
516+
517+
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
518+
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;
519+
if ($hasDbal4) {
520+
$errors = [
521+
[
522+
'Property PHPStan\Rules\Doctrine\ORM\MyEntity659::$binaryResource type mapping mismatch: database can contain string but property expects resource.',
523+
31,
524+
],
525+
];
526+
} else {
527+
$errors = [
528+
[
529+
'Property PHPStan\Rules\Doctrine\ORM\MyEntity659::$binaryString type mapping mismatch: database can contain resource but property expects string.',
530+
25,
531+
],
532+
];
533+
}
534+
535+
$this->analyse([__DIR__ . '/data/bug-659.php'], $errors);
536+
}
537+
498538
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php // lint >= 8.0
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity()
9+
*/
10+
class MyEntity659
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\GeneratedValue()
16+
* @ORM\Column(type="integer")
17+
* @var int
18+
*/
19+
private $id;
20+
21+
/**
22+
* @var string
23+
* @ORM\Column(type="binary")
24+
*/
25+
private $binaryString;
26+
27+
/**
28+
* @var resource
29+
* @ORM\Column(type="binary")
30+
*/
31+
private $binaryResource;
32+
}

0 commit comments

Comments
 (0)