33namespace PHPStan \Platform ;
44
55use Cache \Adapter \PHPArray \ArrayCachePool ;
6+ use Composer \InstalledVersions ;
7+ use Composer \Semver \VersionParser ;
68use Doctrine \Common \Annotations \AnnotationReader ;
79use Doctrine \DBAL \Connection ;
810use Doctrine \DBAL \DriverManager ;
911use Doctrine \DBAL \Exception as DbalException ;
1012use Doctrine \ORM \Configuration ;
1113use Doctrine \ORM \EntityManager ;
1214use Doctrine \ORM \Mapping \Driver \AnnotationDriver ;
15+ use Doctrine \ORM \Mapping \Driver \AttributeDriver ;
1316use Doctrine \ORM \Tools \SchemaTool ;
1417use LogicException ;
1518use mysqli ;
1619use PDO ;
17- use PHPStan \Platform \MatrixEntity \ TestEntity ;
20+ use PHPStan \Platform \Entity \ PlatformEntity ;
1821use PHPStan \Testing \PHPStanTestCase ;
1922use PHPStan \Type \ConstantTypeHelper ;
2023use PHPStan \Type \Doctrine \DescriptorRegistry ;
2528use SQLite3 ;
2629use function array_column ;
2730use function array_combine ;
31+ use function array_fill ;
2832use function array_keys ;
33+ use function class_exists ;
2934use function function_exists ;
3035use function get_debug_type ;
3136use function getenv ;
@@ -89,7 +94,13 @@ public function testFetchedTypes(
8994 $ config ->setAutoGenerateProxyClasses (false );
9095 $ config ->setSecondLevelCacheEnabled (false );
9196 $ 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+
93104 $ entityManager = new EntityManager ($ connection , $ config );
94105
95106 } catch (DbalException $ e ) {
@@ -104,7 +115,8 @@ public function testFetchedTypes(
104115 $ schemaTool ->dropSchema ($ classes );
105116 $ schemaTool ->createSchema ($ classes );
106117
107- $ entity = new TestEntity ();
118+ $ entity = new PlatformEntity ();
119+ $ entity ->id = '1 ' ;
108120 $ entity ->col_bool = true ;
109121 $ entity ->col_float = 0.125 ;
110122 $ entity ->col_decimal = '0.1 ' ;
@@ -125,7 +137,7 @@ public function testFetchedTypes(
125137 if ($ expectedType === null ) {
126138 continue ; // e.g. no such function
127139 }
128- $ dql = sprintf ($ columnsQueryTemplate , $ select , TestEntity ::class);
140+ $ dql = sprintf ($ columnsQueryTemplate , $ select , PlatformEntity ::class);
129141
130142 $ query = $ entityManager ->createQuery ($ dql );
131143 $ result = $ query ->getSingleResult ();
@@ -214,7 +226,7 @@ public function provideCases(): iterable
214226 '1 ' => ['int ' , 'int ' , 'int ' , 'int ' , 'string ' , 'string ' ],
215227 '2147483648 ' => ['int ' , 'int ' , 'int ' , 'int ' , 'string ' , 'string ' ],
216228 '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 ' ) ,
218230 'SUM(t.col_int) ' => ['string ' , 'int ' , 'int ' , 'int ' , 'string ' , 'string ' ],
219231 'SUM(t.col_bigint) ' => ['string ' , 'int ' , 'string ' , 'string ' , 'string ' , 'string ' ],
220232 "LENGTH('') " => ['int ' , 'int ' , 'int ' , 'int ' , 'int ' , 'int ' ],
@@ -408,4 +420,13 @@ private function getNativeConnection(Connection $connection)
408420 throw new LogicException ('Unable to get native connection ' );
409421 }
410422
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+
411432}
0 commit comments