Skip to content

Commit 6ea7082

Browse files
authored
implement DbaApi (#480)
Co-authored-by: Jakub Vojacek <jakub@motv.eu>
1 parent 39ec522 commit 6ea7082

20 files changed

+422
-20
lines changed

src/Extensions/DibiConnectionFetchDynamicReturnTypeExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\Type;
1717
use PHPStan\Type\TypeCombinator;
1818
use staabm\PHPStanDba\DibiReflection\DibiReflection;
19+
use staabm\PHPStanDba\QueryReflection\DbaApi;
1920
use staabm\PHPStanDba\QueryReflection\QueryReflection;
2021
use staabm\PHPStanDba\QueryReflection\QueryReflector;
2122
use staabm\PHPStanDba\UnresolvableQueryException;
@@ -60,7 +61,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6061

6162
private function inferType(MethodReflection $methodReflection, Expr $queryExpr, Scope $scope): ?Type
6263
{
63-
$queryReflection = new QueryReflection();
64+
$queryReflection = new QueryReflection(new DbaApi(DbaApi::API_DIBI));
6465
$queryStrings = $queryReflection->resolveQueryStrings($queryExpr, $scope);
6566

6667
return $this->createFetchType($queryStrings, $methodReflection);
@@ -71,7 +72,7 @@ private function inferType(MethodReflection $methodReflection, Expr $queryExpr,
7172
*/
7273
private function createFetchType(iterable $queryStrings, MethodReflection $methodReflection): ?Type
7374
{
74-
$queryReflection = new QueryReflection();
75+
$queryReflection = new QueryReflection(new DbaApi(DbaApi::API_DIBI));
7576
$dibiReflection = new DibiReflection();
7677

7778
$fetchTypes = [];

src/QueryReflection/BasePdoQueryReflector.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ abstract class BasePdoQueryReflector implements QueryReflector, RecordingReflect
7272
*/
7373
protected $pdo;
7474

75-
public function __construct(PDO $pdo, TypeMapper $typeMapper)
75+
public function __construct(PDO $pdo)
7676
{
7777
$this->pdo = $pdo;
7878
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
79-
80-
$this->typeMapper = $typeMapper;
8179
}
8280

8381
public function validateQueryString(string $queryString): ?Error

src/QueryReflection/ChainedReflector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@ public function getResultType(string $queryString, int $fetchType): ?Type
6161

6262
return null;
6363
}
64+
65+
public function setupDbaApi(?DbaApi $dbaApi): void
66+
{
67+
foreach ($this->reflectors as $reflector) {
68+
$reflector->setupDbaApi($dbaApi);
69+
}
70+
}
6471
}

src/QueryReflection/DbaApi.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace staabm\PHPStanDba\QueryReflection;
6+
7+
final class DbaApi
8+
{
9+
public const API_DIBI = 'dibi';
10+
11+
/** @var self::API_* */
12+
private $api;
13+
14+
/** @param self::API_* $api */
15+
public function __construct($api)
16+
{
17+
$this->api = $api;
18+
}
19+
20+
public function returnsDateTimeImmutable(): bool
21+
{
22+
return self::API_DIBI === $this->api;
23+
}
24+
}

src/QueryReflection/LazyQueryReflector.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ final class LazyQueryReflector implements QueryReflector
1818
*/
1919
private $reflector;
2020

21+
/**
22+
* @var DbaApi|null
23+
*/
24+
private $dbaApi;
25+
2126
/**
2227
* @param callable():QueryReflector $reflectorFactory
2328
*/
@@ -28,19 +33,30 @@ public function __construct(callable $reflectorFactory)
2833

2934
public function validateQueryString(string $queryString): ?Error
3035
{
31-
if (null === $this->reflector) {
32-
$this->reflector = ($this->reflectorFactory)();
33-
}
36+
$this->reflector = $this->createReflector();
3437

3538
return $this->reflector->validateQueryString($queryString);
3639
}
3740

3841
public function getResultType(string $queryString, int $fetchType): ?Type
42+
{
43+
$this->reflector = $this->createReflector();
44+
45+
return $this->reflector->getResultType($queryString, $fetchType);
46+
}
47+
48+
public function setupDbaApi(?DbaApi $dbaApi): void
49+
{
50+
$this->dbaApi = $dbaApi;
51+
}
52+
53+
private function createReflector(): QueryReflector
3954
{
4055
if (null === $this->reflector) {
4156
$this->reflector = ($this->reflectorFactory)();
57+
$this->reflector->setupDbaApi($this->dbaApi);
4258
}
4359

44-
return $this->reflector->getResultType($queryString, $fetchType);
60+
return $this->reflector;
4561
}
4662
}

src/QueryReflection/MysqliQueryReflector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public function __construct(mysqli $mysqli)
5555
$this->db->set_charset('utf8');
5656
// enable exception throwing on php <8.1
5757
mysqli_report(\MYSQLI_REPORT_ERROR | \MYSQLI_REPORT_STRICT);
58-
59-
$this->typeMapper = new MysqliTypeMapper();
6058
}
6159

6260
public function validateQueryString(string $queryString): ?Error
@@ -122,6 +120,11 @@ public function getResultType(string $queryString, int $fetchType): ?Type
122120
return $arrayBuilder->getArray();
123121
}
124122

123+
public function setupDbaApi(?DbaApi $dbaApi): void
124+
{
125+
$this->typeMapper = new MysqliTypeMapper($dbaApi);
126+
}
127+
125128
/**
126129
* @return mysqli_sql_exception|list<object>|null
127130
*/

src/QueryReflection/PdoMysqlQueryReflector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(PDO $pdo)
2525
{
2626
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2727

28-
parent::__construct($pdo, new MysqlTypeMapper());
28+
parent::__construct($pdo);
2929
}
3030

3131
/** @return PDOException|list<ColumnMeta>|null */
@@ -90,6 +90,11 @@ protected function simulateQuery(string $queryString)
9090
return $this->cache[$queryString];
9191
}
9292

93+
public function setupDbaApi(?DbaApi $dbaApi): void
94+
{
95+
$this->typeMapper = new MysqlTypeMapper($dbaApi);
96+
}
97+
9398
/**
9499
* @return Iterator<string, TypeMapper::FLAG_*>
95100
*/

src/QueryReflection/PdoPgSqlQueryReflector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ final class PdoPgSqlQueryReflector extends BasePdoQueryReflector
2121

2222
public function __construct(PDO $pdo)
2323
{
24-
$typeMapper = new PgsqlTypeMapper();
25-
26-
parent::__construct($pdo, $typeMapper);
24+
parent::__construct($pdo);
2725
}
2826

2927
/** @return PDOException|list<PDOColumnMeta>|null */
@@ -91,6 +89,11 @@ protected function simulateQuery(string $queryString)
9189
return $this->cache[$queryString];
9290
}
9391

92+
public function setupDbaApi(?DbaApi $dbaApi): void
93+
{
94+
$this->typeMapper = new PgsqlTypeMapper($dbaApi);
95+
}
96+
9497
/**
9598
* @return Iterator<string, PgsqlTypeMapper::FLAG_*>
9699
*/

src/QueryReflection/QueryReflection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ final class QueryReflection
4343
*/
4444
private static $runtimeConfiguration;
4545

46+
public function __construct(?DbaApi $dbaApi = null)
47+
{
48+
self::reflector()->setupDbaApi($dbaApi);
49+
}
50+
4651
public static function setupReflector(QueryReflector $reflector, RuntimeConfiguration $runtimeConfiguration): void
4752
{
4853
self::$reflector = $reflector;

src/QueryReflection/QueryReflector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public function validateQueryString(string $queryString): ?Error;
2222
* @param self::FETCH_TYPE* $fetchType
2323
*/
2424
public function getResultType(string $queryString, int $fetchType): ?Type;
25+
26+
public function setupDbaApi(?DbaApi $dbaApi): void;
2527
}

0 commit comments

Comments
 (0)