3
3
namespace PHPStan \Platform ;
4
4
5
5
use Cache \Adapter \PHPArray \ArrayCachePool ;
6
+ use Composer \InstalledVersions ;
7
+ use Composer \Semver \VersionParser ;
6
8
use Doctrine \Common \Annotations \AnnotationReader ;
7
9
use Doctrine \DBAL \Connection ;
8
10
use Doctrine \DBAL \DriverManager ;
9
11
use Doctrine \DBAL \Exception as DbalException ;
10
12
use Doctrine \ORM \Configuration ;
11
13
use Doctrine \ORM \EntityManager ;
12
14
use Doctrine \ORM \Mapping \Driver \AnnotationDriver ;
15
+ use Doctrine \ORM \Mapping \Driver \AttributeDriver ;
13
16
use Doctrine \ORM \Tools \SchemaTool ;
14
17
use LogicException ;
15
18
use mysqli ;
16
19
use PDO ;
17
- use PHPStan \Platform \MatrixEntity \ TestEntity ;
20
+ use PHPStan \Platform \Entity \ PlatformEntity ;
18
21
use PHPStan \Testing \PHPStanTestCase ;
19
22
use PHPStan \Type \ConstantTypeHelper ;
20
23
use PHPStan \Type \Doctrine \DescriptorRegistry ;
25
28
use SQLite3 ;
26
29
use function array_column ;
27
30
use function array_combine ;
31
+ use function array_fill ;
28
32
use function array_keys ;
33
+ use function class_exists ;
29
34
use function function_exists ;
30
35
use function get_debug_type ;
31
36
use function getenv ;
@@ -89,7 +94,13 @@ public function testFetchedTypes(
89
94
$ config ->setAutoGenerateProxyClasses (false );
90
95
$ config ->setSecondLevelCacheEnabled (false );
91
96
$ config ->setMetadataCache (new ArrayCachePool ());
92
- $ config ->setMetadataDriverImpl (new AnnotationDriver (new AnnotationReader (), [__DIR__ . '/MatrixEntity ' ]));
97
+
98
+ if (InstalledVersions::satisfies (new VersionParser (), 'doctrine/orm ' , '3.* ' )) {
99
+ $ config ->setMetadataDriverImpl (new AttributeDriver ([__DIR__ . '/Entity ' ]));
100
+ } else {
101
+ $ config ->setMetadataDriverImpl (new AnnotationDriver (new AnnotationReader (), [__DIR__ . '/Entity ' ]));
102
+ }
103
+
93
104
$ entityManager = new EntityManager ($ connection , $ config );
94
105
95
106
} catch (DbalException $ e ) {
@@ -104,7 +115,8 @@ public function testFetchedTypes(
104
115
$ schemaTool ->dropSchema ($ classes );
105
116
$ schemaTool ->createSchema ($ classes );
106
117
107
- $ entity = new TestEntity ();
118
+ $ entity = new PlatformEntity ();
119
+ $ entity ->id = '1 ' ;
108
120
$ entity ->col_bool = true ;
109
121
$ entity ->col_float = 0.125 ;
110
122
$ entity ->col_decimal = '0.1 ' ;
@@ -125,7 +137,7 @@ public function testFetchedTypes(
125
137
if ($ expectedType === null ) {
126
138
continue ; // e.g. no such function
127
139
}
128
- $ dql = sprintf ($ columnsQueryTemplate , $ select , TestEntity ::class);
140
+ $ dql = sprintf ($ columnsQueryTemplate , $ select , PlatformEntity ::class);
129
141
130
142
$ query = $ entityManager ->createQuery ($ dql );
131
143
$ result = $ query ->getSingleResult ();
@@ -214,7 +226,7 @@ public function provideCases(): iterable
214
226
'1 ' => ['int ' , 'int ' , 'int ' , 'int ' , 'string ' , 'string ' ],
215
227
'2147483648 ' => ['int ' , 'int ' , 'int ' , 'int ' , 'string ' , 'string ' ],
216
228
't.col_int ' => ['int ' , 'int ' , 'int ' , 'int ' , 'int ' , 'int ' ],
217
- 't.col_bigint ' => [ ' string ' , ' string ' , ' string ' , ' string ' , ' string ' , 'string ' ] ,
229
+ 't.col_bigint ' => self :: hasDbal4 () ? array_fill ( 0 , 6 , ' int ' ) : array_fill ( 0 , 6 , 'string ' ) ,
218
230
'SUM(t.col_int) ' => ['string ' , 'int ' , 'int ' , 'int ' , 'string ' , 'string ' ],
219
231
'SUM(t.col_bigint) ' => ['string ' , 'int ' , 'string ' , 'string ' , 'string ' , 'string ' ],
220
232
"LENGTH('') " => ['int ' , 'int ' , 'int ' , 'int ' , 'int ' , 'int ' ],
@@ -408,4 +420,13 @@ private function getNativeConnection(Connection $connection)
408
420
throw new LogicException ('Unable to get native connection ' );
409
421
}
410
422
423
+ private static function hasDbal4 (): bool
424
+ {
425
+ if (!class_exists (InstalledVersions::class)) {
426
+ return false ;
427
+ }
428
+
429
+ return InstalledVersions::satisfies (new VersionParser (), 'doctrine/dbal ' , '4.* ' );
430
+ }
431
+
411
432
}
0 commit comments