Skip to content

Commit 664e380

Browse files
Fix compatibility with DBAL 4
1 parent 45aa443 commit 664e380

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

src/Stubs/Doctrine/StubFilesExtensionLoader.php

+32-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace PHPStan\Stubs\Doctrine;
44

5+
use Composer\InstalledVersions;
6+
use OutOfBoundsException;
57
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
68
use PHPStan\BetterReflection\Reflector\Reflector;
79
use PHPStan\PhpDoc\StubFilesExtension;
10+
use function class_exists;
811
use function dirname;
12+
use function file_exists;
13+
use function strpos;
914

1015
class StubFilesExtensionLoader implements StubFilesExtension
1116
{
@@ -34,11 +39,18 @@ public function getFiles(): array
3439
$path .= '/bleedingEdge';
3540
}
3641

37-
$files = [
38-
$path . '/DBAL/Connection.stub',
39-
$path . '/ORM/QueryBuilder.stub',
40-
$path . '/EntityRepository.stub',
41-
];
42+
$files = [];
43+
44+
if (file_exists($path . '/DBAL/Connection4.stub') && $this->isInstalledVersion('doctrine/dbal', 4)) {
45+
$files[] = $path . '/DBAL/Connection4.stub';
46+
$files[] = $path . '/DBAL/ArrayParameterType.stub';
47+
$files[] = $path . '/DBAL/ParameterType.stub';
48+
} else {
49+
$files[] = $path . '/DBAL/Connection.stub';
50+
}
51+
52+
$files[] = $path . '/ORM/QueryBuilder.stub';
53+
$files[] = $path . '/EntityRepository.stub';
4254

4355
$hasLazyServiceEntityRepositoryAsParent = false;
4456

@@ -62,4 +74,19 @@ public function getFiles(): array
6274
return $files;
6375
}
6476

77+
private function isInstalledVersion(string $package, int $majorVersion): bool
78+
{
79+
if (!class_exists(InstalledVersions::class)) {
80+
return false;
81+
}
82+
83+
try {
84+
$installedVersion = InstalledVersions::getVersion($package);
85+
} catch (OutOfBoundsException $e) {
86+
return false;
87+
}
88+
89+
return $installedVersion !== null && strpos($installedVersion, $majorVersion . '.') === 0;
90+
}
91+
6592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL;
4+
5+
enum ArrayParameterType
6+
{
7+
8+
case INTEGER;
9+
case STRING;
10+
case ASCII;
11+
case BINARY;
12+
13+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL;
4+
5+
use Doctrine\DBAL\Cache\CacheException;
6+
use Doctrine\DBAL\Cache\QueryCacheProfile;
7+
use Doctrine\DBAL\Types\Type;
8+
9+
/**
10+
* @phpstan-type WrapperParameterType = string|Type|ParameterType|ArrayParameterType
11+
* @phpstan-type WrapperParameterTypeArray = array<int<0, max>, WrapperParameterType>|array<string, WrapperParameterType>
12+
*/
13+
class Connection
14+
{
15+
/**
16+
* Executes an SQL statement with the given parameters and returns the number of affected rows.
17+
*
18+
* Could be used for:
19+
* - DML statements: INSERT, UPDATE, DELETE, etc.
20+
* - DDL statements: CREATE, DROP, ALTER, etc.
21+
* - DCL statements: GRANT, REVOKE, etc.
22+
* - Session control statements: ALTER SESSION, SET, DECLARE, etc.
23+
* - Other statements that don't yield a row set.
24+
*
25+
* This method supports PDO binding types as well as DBAL mapping types.
26+
*
27+
* @param literal-string $sql SQL statement
28+
* @param list<mixed>|array<string, mixed> $params Statement parameters
29+
* @param WrapperParameterTypeArray $types Parameter types
30+
*
31+
* @return int|string The number of affected rows.
32+
*
33+
* @throws Exception
34+
*/
35+
public function executeStatement($sql, array $params = [], array $types = []);
36+
37+
/**
38+
* Executes an, optionally parameterized, SQL query.
39+
*
40+
* If the query is parametrized, a prepared statement is used.
41+
* If an SQLLogger is configured, the execution is logged.
42+
*
43+
* @param literal-string $sql SQL query
44+
* @param list<mixed>|array<string, mixed> $params Query parameters
45+
* @param WrapperParameterTypeArray $types Parameter types
46+
*
47+
* @throws Exception
48+
*/
49+
public function executeQuery(
50+
string $sql,
51+
array $params = [],
52+
$types = [],
53+
?QueryCacheProfile $qcp = null
54+
): Result;
55+
56+
/**
57+
* Executes a caching query.
58+
*
59+
* @param literal-string $sql SQL query
60+
* @param list<mixed>|array<string, mixed> $params Query parameters
61+
* @param WrapperParameterTypeArray $types Parameter types
62+
*
63+
* @throws CacheException
64+
* @throws Exception
65+
*/
66+
public function executeCacheQuery($sql, $params, $types, QueryCacheProfile $qcp): Result;
67+
68+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL;
4+
5+
enum ParameterType
6+
{
7+
8+
case NULL;
9+
case INTEGER;
10+
case STRING;
11+
case LARGE_OBJECT;
12+
case BOOLEAN;
13+
case BINARY;
14+
case ASCII;
15+
16+
}

0 commit comments

Comments
 (0)