Skip to content

Commit 07e5c48

Browse files
Fix BinaryType with dbal 4
1 parent 934f573 commit 07e5c48

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/Type/Doctrine/Descriptors/BinaryType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
@@ -17,6 +18,10 @@ public function getType(): string
1718

1819
public function getWritableToPropertyType(): Type
1920
{
21+
if ($this->hasDbal4()) {
22+
return new StringType();
23+
}
24+
2025
return new ResourceType();
2126
}
2227

@@ -30,4 +35,18 @@ public function getDatabaseInternalType(): Type
3035
return new StringType();
3136
}
3237

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

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,4 +478,33 @@ public function testBug679(?string $objectManagerLoader): void
478478
$this->analyse([__DIR__ . '/data/bug-679.php'], []);
479479
}
480480

481+
/**
482+
* @dataProvider dataObjectManagerLoader
483+
*/
484+
public function testBug659(?string $objectManagerLoader): void
485+
{
486+
$this->allowNullablePropertyForRequiredField = false;
487+
$this->objectManagerLoader = $objectManagerLoader;
488+
489+
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
490+
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;
491+
if ($hasDbal4) {
492+
$errors = [
493+
[
494+
'Property PHPStan\Rules\Doctrine\ORM\MyEntity659::$binaryString type mapping mismatch: database can contain string but property expects resource.',
495+
31,
496+
],
497+
];
498+
} else {
499+
$errors = [
500+
[
501+
'Property PHPStan\Rules\Doctrine\ORM\MyEntity659::$binaryString type mapping mismatch: database can contain resource but property expects string.',
502+
25,
503+
],
504+
];
505+
}
506+
507+
$this->analyse([__DIR__ . '/data/bug-659.php'], $errors);
508+
}
509+
481510
}
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)