File tree Expand file tree Collapse file tree 3 files changed +82
-0
lines changed
src/Type/Doctrine/Descriptors Expand file tree Collapse file tree 3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace PHPStan \Type \Doctrine \Descriptors ;
4
4
5
+ use Composer \InstalledVersions ;
5
6
use PHPStan \Type \MixedType ;
6
7
use PHPStan \Type \ResourceType ;
7
8
use PHPStan \Type \StringType ;
8
9
use PHPStan \Type \Type ;
10
+ use function class_exists ;
11
+ use function strpos ;
9
12
10
13
class BinaryType implements DoctrineTypeDescriptor
11
14
{
@@ -17,6 +20,10 @@ public function getType(): string
17
20
18
21
public function getWritableToPropertyType (): Type
19
22
{
23
+ if ($ this ->hasDbal4 ()) {
24
+ return new StringType ();
25
+ }
26
+
20
27
return new ResourceType ();
21
28
}
22
29
@@ -30,4 +37,18 @@ public function getDatabaseInternalType(): Type
30
37
return new StringType ();
31
38
}
32
39
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
+
33
54
}
Original file line number Diff line number Diff line change @@ -478,4 +478,33 @@ public function testBug679(?string $objectManagerLoader): void
478
478
$ this ->analyse ([__DIR__ . '/data/bug-679.php ' ], []);
479
479
}
480
480
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
+
481
510
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments