Skip to content

Support for doctrine/dbal v4 #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,81 @@ jobs:
# dependencies: "lowest"

- php-version: "7.4"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "pdo-mysql"
mode: "recording"
dependencies: "highest"
- php-version: "8.0"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "pdo-mysql"
mode: "recording"
dependencies: "highest"
- php-version: "8.0"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"

- php-version: "8.1"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"
- php-version: '8.1'
dbal-version: "^3"
db-image: 'mariadb:latest'
platform: 'mariadb'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"

- php-version: "8.2"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"
- php-version: '8.2'
dbal-version: "^3"
db-image: 'mariadb:latest'
platform: 'mariadb'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"

- php-version: "8.3"
dbal-version: "^4"
db-image: 'mysql:8.0'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"

- php-version: "8.4"
dbal-version: "^4"
db-image: 'mysql:8.0'
reflector: "mysqli"
mode: "recording"
dependencies: "highest"

- php-version: "8.1"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "pdo-mysql"
mode: "replay-and-recording"
dependencies: "highest"

- php-version: "8.1"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "pdo-mysql"
mode: "empty-recording"
dependencies: "highest"
- php-version: "8.1"
dbal-version: "^3"
db-image: 'mysql:8.0'
reflector: "pdo-mysql"
mode: "empty-replay-and-recording"
Expand Down Expand Up @@ -120,7 +144,7 @@ jobs:
run: composer require sqlftw/sqlftw --ignore-platform-req=php+

- name: Install doctrine/dbal (optional dependency)
run: composer require doctrine/dbal:^3 --ignore-platform-req=php+
run: "composer require doctrine/dbal:${{ matrix.dbal-version }} --ignore-platform-req=php+"

- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": "^7.4 || ^8.0",
"composer-runtime-api": "^2.0",
"composer/semver": "^3.2",
"doctrine/dbal": "3.*",
"doctrine/dbal": "3.*|4.*",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions config/rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ services:
- 'PDO::prepare#0'
- 'mysqli::query#0'
- 'mysqli::execute_query#0'
- 'Doctrine\DBAL\Connection::executeQuery#0'
- 'Doctrine\DBAL\Connection::executeStatement#0'
- 'Doctrine\DBAL\Connection::query#0' # deprecated in doctrine
- 'Doctrine\DBAL\Connection::exec#0' # deprecated in doctrine

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

// make sure we don't report wrong types in doctrine 2.x
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*|4.*')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

// make sure we don't report wrong types in doctrine 2.x
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*|4.*')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

// make sure we don't report wrong types in doctrine 2.x
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*|4.*')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

// make sure we don't report wrong types in doctrine 2.x
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*|4.*')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
// make sure we don't report wrong types in doctrine 2.x
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*|4.*')) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

// make sure we don't report wrong types in doctrine 2.x
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*|4.*')) {
return null;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/default/DbaInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/doctrine-dbal-union-result.php');
if (InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/doctrine-dbal3.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/doctrine-dbal.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/inference-placeholder.php');

Expand Down
10 changes: 0 additions & 10 deletions tests/default/data/doctrine-dbal-union-result.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ public function doFoo(Connection $conn)
assertType('array{adaid: int<-32768, 32767>}|array{email: string}|false', $fetch);
}
}

public function doBar(Connection $conn)
{
$queries = ['SELECT adaid FROM ada', 'SELECT email FROM ada'];

foreach ($queries as $query) {
$result = $conn->query($query);
assertType('array{adaid: int<-32768, 32767>}|array{email: string}|false', $result->fetchAssociative());
}
}
}
4 changes: 2 additions & 2 deletions tests/default/data/doctrine-dbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

class Foo
{
public function foo(Connection $conn)
public function resultFetching(Connection $conn)
{
$result = $conn->query('SELECT email, adaid FROM ada');
$result = $conn->executeQuery('SELECT email, adaid FROM ada');

$columnCount = $result->columnCount();
assertType('2', $columnCount);
Expand Down
60 changes: 60 additions & 0 deletions tests/default/data/doctrine-dbal3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace DoctrineDbal3Test;

use Doctrine\DBAL\Connection;
use function PHPStan\Testing\assertType;

class Foo
{
public function resultFetching(Connection $conn)
{
$result = $conn->query('SELECT email, adaid FROM ada');

$columnCount = $result->columnCount();
assertType('2', $columnCount);

$fetch = $result->fetchOne();
assertType('string|false', $fetch);

$fetch = $result->fetchNumeric();
assertType('array{string, int<-32768, 32767>}|false', $fetch);

$fetch = $result->fetchFirstColumn();
assertType('list<string>', $fetch);

$fetch = $result->fetchAssociative();
assertType('array{email: string, adaid: int<-32768, 32767>}|false', $fetch);

$fetch = $result->fetchAllNumeric();
assertType('list<array{string, int<-32768, 32767>}>', $fetch);

$fetch = $result->fetchAllAssociative();
assertType('list<array{email: string, adaid: int<-32768, 32767>}>', $fetch);

$fetch = $result->fetchAllKeyValue();
assertType('array<string, int<-32768, 32767>>', $fetch);

$fetch = $result->iterateNumeric();
assertType('Traversable<int, array{string, int<-32768, 32767>}>', $fetch);

$fetch = $result->iterateAssociative();
assertType('Traversable<int, array{email: string, adaid: int<-32768, 32767>}>', $fetch);

$fetch = $result->iterateColumn();
assertType('Traversable<int, string>', $fetch);

$fetch = $result->iterateKeyValue();
assertType('Traversable<string, int<-32768, 32767>>', $fetch);
}

public function unionQueriesResult(Connection $conn)
{
$queries = ['SELECT adaid FROM ada', 'SELECT email FROM ada'];

foreach ($queries as $query) {
$result = $conn->query($query);
assertType('array{adaid: int<-32768, 32767>}|array{email: string}|false', $result->fetchAssociative());
}
}
}
42 changes: 42 additions & 0 deletions tests/rules/SyntaxErrorInQueryMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace staabm\PHPStanDba\Tests;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use staabm\PHPStanDba\QueryReflection\MysqliQueryReflector;
Expand Down Expand Up @@ -302,6 +304,46 @@ public function testSyntaxErrorInQueryRule(): void
$this->analyse([__DIR__ . '/data/syntax-error-in-query-method.php'], $expected);
}

public function testSyntaxErrorInQueryRuleForDoctrineDbal3(): void
{
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
self::markTestSkipped('Doctrine DBAL 3.x test only.');
}

if (MysqliQueryReflector::NAME === getenv('DBA_REFLECTOR')) {
$expected = [
[
"Query error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (1064).",
12,
],
];
} elseif (PdoMysqlQueryReflector::NAME === getenv('DBA_REFLECTOR')) {
if ('mariadb' === $_ENV['DBA_PLATFORM']) {
self::markTestSkipped("We don't test all variants of expectations for all drivers");
}

$expected = [
[
"Query error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL/MariaDB server version for the right syntax to use near 'freigabe1u1 FROM ada LIMIT 0' at line 1 (42000).",
12,
],
];
} elseif (PdoPgSqlQueryReflector::NAME === getenv('DBA_REFLECTOR')) {
$expected = [
[
'Query error: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "freigabe1u1"
LINE 1: SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada LIMIT...
^ (42601).',
12,
],
];
} else {
throw new \RuntimeException('Unsupported DBA_REFLECTOR ' . getenv('DBA_REFLECTOR'));
}

$this->analyse([__DIR__ . '/data/syntax-error-in-query-method-dbal3.php'], $expected);
}

public function testMysqliExecuteQuery(): void
{
if (\PHP_VERSION_ID < 80200) {
Expand Down
20 changes: 20 additions & 0 deletions tests/rules/data/syntax-error-in-query-method-dbal3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace SyntaxErrorInQueryMethodDbal3RuleTest;

use PDO;

class Foo
{
public function syntaxErrorDoctrineDbal(\Doctrine\DBAL\Connection $conn)
{
$sql = 'SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada';
$conn->query($sql);
}

public function noErrorOnQueriesContainingPlaceholders(\Doctrine\DBAL\Connection $conn)
{
// errors in this scenario are reported by SyntaxErrorInPreparedStatementMethodRule only
$conn->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=?');
}
}
4 changes: 2 additions & 2 deletions tests/rules/data/syntax-error-in-query-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function syntaxErrorPdoPrepare(PDO $pdo)
public function syntaxErrorDoctrineDbal(\Doctrine\DBAL\Connection $conn)
{
$sql = 'SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada';
$conn->query($sql);
$conn->executeQuery($sql);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this changes?

Copy link
Owner

@staabm staabm May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

said differently: we also need the previous test with ->query() but just run it on 3.x

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Connection::query method was deprecated in DBAL 3 and was deleted in DBAL 4.

Target solution is to use executeQuery in both, so I adjusted test to be valid for both versions.
Also, I added Connection::executeQuery and Connection::executeStatement in injected methods to validate in SyntaxErrorInQueryMethodRule class - IMO they should be there from the very beginning as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to keep that test-case, I'd need to create separate test data file and include it conditionally only for dbal3 (simillary as with doctrine-dbal3.php file). It's because of the Connection::query method is missing in dbal4 and it wouldn't be parsed by phpstan (it would skip analyzing query syntax, so expected test error wouldn't be thrown)

Will that be fine?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

Copy link
Owner

@staabm staabm May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be precise: just copy the previous code into a new file and make it beeing analyzed by the corresponding rule-test.

we only need the 3.x only-api tested in the new file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm done :)

}

public function noErrorOnQueriesContainingPlaceholders(\Doctrine\DBAL\Connection $conn)
{
// errors in this scenario are reported by SyntaxErrorInPreparedStatementMethodRule only
$conn->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=?');
$conn->executeQuery('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE adaid=?');
}

public function conditionalSyntaxError(PDO $pdo)
Expand Down