Skip to content

Commit

Permalink
Merge pull request #4638 from Ocramius/fix/#4637-restore-type-inferen…
Browse files Browse the repository at this point in the history
…ce-for-wrapped-connection-class

Fix #4637 by duplicating the type definition for `DriverManager::getConnection($args)` params
  • Loading branch information
morozov committed May 9, 2021
2 parents 35a4071 + cb8b389 commit b728198
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ jobs:
with:
composer_require_dev: true
args: --shepherd

- name: Psalm type inference tests
uses: docker://vimeo/psalm-github-actions:4.6.4
with:
composer_require_dev: true
args: --config=psalm-strict.xml
25 changes: 24 additions & 1 deletion lib/Doctrine/DBAL/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,30 @@ private function __construct()
* @throws Exception
*
* @phpstan-param array<string,mixed> $params
* @psalm-param Params $params
* @psalm-param array{
* charset?: string,
* dbname?: string,
* default_dbname?: string,
* driver?: key-of<self::DRIVER_MAP>,
* driverClass?: class-string<Driver>,
* driverOptions?: array<mixed>,
* host?: string,
* keepSlave?: bool,
* keepReplica?: bool,
* master?: OverrideParams,
* memory?: bool,
* password?: string,
* path?: string,
* pdo?: \PDO,
* platform?: Platforms\AbstractPlatform,
* port?: int,
* primary?: OverrideParams,
* replica?: array<OverrideParams>,
* sharding?: array<string,mixed>,
* slaves?: array<OverrideParams>,
* user?: string,
* wrapperClass?: class-string<T>,
* } $params
* @psalm-return ($params is array{wrapperClass:mixed} ? T : Connection)
* @template T of Connection
*/
Expand Down
2 changes: 1 addition & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<file>bin</file>
<file>lib</file>
<file>tests</file>
<file>tests/Doctrine/Tests</file>

<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes"/>
Expand Down
15 changes: 15 additions & 0 deletions psalm-strict.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="tests/Doctrine/StaticAnalysis" />
<ignoreFiles>
<directory name="lib" />
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
>
<projectFiles>
<directory name="lib/Doctrine/DBAL" />
<directory name="tests/Doctrine/StaticAnalysis" />
<directory name="tests/Doctrine/Tests" />
<ignoreFiles>
<directory name="vendor" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Doctrine\StaticAnalysis\DBAL;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;

final class MyConnection extends Connection
{
}

function makeMeACustomConnection(): MyConnection
{
return DriverManager::getConnection([
'wrapperClass' => MyConnection::class,
]);
}
58 changes: 40 additions & 18 deletions tests/Doctrine/Tests/DBAL/DriverManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use PDO;
use stdClass;

use function array_intersect_key;
use function array_merge;
use function extension_loaded;
use function get_class;
use function in_array;
Expand Down Expand Up @@ -159,18 +161,26 @@ public function testDatabaseUrlPrimaryReplica(): void
'password' => 'bar',
'host' => 'localhost',
'port' => 11211,
'dbname' => 'baz',
'driver' => 'pdo_mysql',
'url' => 'mysql://foo:bar@localhost:11211/baz',
];

foreach ($expected as $key => $value) {
self::assertArrayHasKey($key, $params['primary']);
self::assertEquals($value, $params['primary'][$key]);

self::assertArrayHasKey($key, $params['replica']['replica1']);
self::assertEquals($value, $params['replica']['replica1'][$key]);
}

self::assertEquals('baz', $params['primary']['dbname']);
self::assertEquals('baz_replica', $params['replica']['replica1']['dbname']);
self::assertEquals(
[
'primary' => $expected,
'replica' => [
'replica1' => array_merge(
$expected,
[
'dbname' => 'baz_replica',
'url' => 'mysql://foo:bar@localhost:11211/baz_replica',
]
),
],
],
array_intersect_key($params, ['primary' => null, 'replica' => null])
);
}

public function testDatabaseUrlShard(): void
Expand All @@ -182,7 +192,7 @@ public function testDatabaseUrlShard(): void
'shards' => [
[
'id' => 1,
'url' => 'mysql://foo:bar@localhost:11211/baz_slave',
'url' => 'mysql://foo:bar@localhost:11211/baz_replica',
],
],
'wrapperClass' => PoolingShardConnection::class,
Expand All @@ -198,15 +208,27 @@ public function testDatabaseUrlShard(): void
'password' => 'bar',
'host' => 'localhost',
'port' => 11211,
'dbname' => 'baz',
'driver' => 'pdo_mysql',
'url' => 'mysql://foo:bar@localhost:11211/baz',
];

foreach ($expected as $key => $value) {
self::assertEquals($value, $params['global'][$key]);
self::assertEquals($value, $params['shards'][0][$key]);
}

self::assertEquals('baz', $params['global']['dbname']);
self::assertEquals('baz_slave', $params['shards'][0]['dbname']);
self::assertEquals(
[
'global' => $expected,
'shards' => [
array_merge(
$expected,
[
'dbname' => 'baz_replica',
'id' => 1,
'url' => 'mysql://foo:bar@localhost:11211/baz_replica',
]
),
],
],
array_intersect_key($params, ['global' => null, 'shards' => null])
);
}

/**
Expand Down

0 comments on commit b728198

Please sign in to comment.