Skip to content

Commit

Permalink
Deal with older dbal versiosn
Browse files Browse the repository at this point in the history
  • Loading branch information
BackEndTea committed Feb 9, 2024
1 parent 462d7c1 commit e5cab77
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/Rules/Doctrine/DBAL/ArrayParameterTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PHPStan\Rules\Doctrine\DBAL;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

Expand All @@ -11,8 +13,44 @@
final class ArrayParameterTypeRuleTest extends RuleTestCase
{

public function testRuleOlderDbal(): void
{
if(InstalledVersions::satisfies(
new VersionParser(),
'doctrine/dbal',
'^3.6 || ^4.0'
)) {
self::markTestSkipped('Test requires dbal 2.');
}
$this->analyse([__DIR__ . '/data/connection_dbal2.php'], [
[
'Parameter at 0 is an array, but is not hinted as such to doctrine.',
10,
],
[
"Parameter at 'a' is an array, but is not hinted as such to doctrine.",
19,
],
[
"Parameter at 'a' is an array, but is not hinted as such to doctrine.",
28,
],
[
"Parameter at 'a' is an array, but is not hinted as such to doctrine.",
39,
],
]);
}

public function testRule(): void
{
if(InstalledVersions::satisfies(
new VersionParser(),
'doctrine/dbal',
'<3.6'
)) {
self::markTestSkipped('Test requires dbal 3 or 4.');
}
$this->analyse([__DIR__ . '/data/connection.php'], [
[
'Parameter at 0 is an array, but is not hinted as such to doctrine.',
Expand Down
64 changes: 64 additions & 0 deletions tests/Rules/Doctrine/DBAL/data/connection_dbal2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace PHPStan\Rules\Doctrine\DBAL;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;

function check(Connection $connection, array $data) {

$connection->executeQuery(
'SELECT 1 FROM table WHERE a IN (?) AND b = ?',
[

$data,
3
]
);

$connection->fetchOne(
'SELECT 1 FROM table WHERE a IN (:a) AND b = :b',
[

'a' => $data,
'b' => 3
]
);

$connection->fetchOne(
'SELECT 1 FROM table WHERE a IN (:a) AND b = :b',
[
'a' => $data,
'b' => 3
],
[
'b' => ParameterType::INTEGER,
]
);

$connection->fetchOne(
'SELECT 1 FROM table WHERE a IN (:a) AND b = :b',
[
'a' => $data,
'b' => 3
],
[
'a' => ParameterType::INTEGER,
'b' => ParameterType::INTEGER,
]
);


$connection->fetchOne(
'SELECT 1 FROM table WHERE a IN (:a) AND b = :b',
[
'a' => $data,
'b' => 3
],
[
'a' => Connection::PARAM_INT_ARRAY,
'b' => ParameterType::INTEGER,
]
);

}

0 comments on commit e5cab77

Please sign in to comment.