From 4f047be9a02eaa6cf3a56cb1ff840853c40fa285 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 31 Dec 2022 17:06:32 +0100 Subject: [PATCH 1/7] Allow to explicitly set the wrapper class to the default one --- src/DriverManager.php | 13 ++++--------- tests/DriverManagerTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/DriverManager.php b/src/DriverManager.php index 38c239fa68b..a5f44783260 100644 --- a/src/DriverManager.php +++ b/src/DriverManager.php @@ -16,8 +16,8 @@ use function assert; use function class_implements; use function in_array; +use function is_a; use function is_string; -use function is_subclass_of; use function parse_str; use function parse_url; use function preg_replace; @@ -212,14 +212,9 @@ public static function getConnection( $driver = $middleware->wrap($driver); } - $wrapperClass = Connection::class; - if (isset($params['wrapperClass'])) { - if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) { - throw Exception::invalidWrapperClass($params['wrapperClass']); - } - - /** @var class-string $wrapperClass */ - $wrapperClass = $params['wrapperClass']; + $wrapperClass = $params['wrapperClass'] ?? Connection::class; + if (! is_a($wrapperClass, Connection::class, true)) { + throw Exception::invalidWrapperClass($wrapperClass); } return new $wrapperClass($params, $driver, $config, $eventManager); diff --git a/tests/DriverManagerTest.php b/tests/DriverManagerTest.php index 83e6ab66411..cc77f5ad6e6 100644 --- a/tests/DriverManagerTest.php +++ b/tests/DriverManagerTest.php @@ -64,6 +64,18 @@ public function testCustomWrapper(): void self::assertInstanceOf($wrapperClass, $conn); } + /** @requires extension pdo_sqlite */ + public function testDefaultWrapper(): void + { + $options = [ + 'url' => 'sqlite::memory:', + 'wrapperClass' => Connection::class, + ]; + + $conn = DriverManager::getConnection($options); + self::assertSame(Connection::class, get_class($conn)); + } + /** * @requires extension pdo_sqlite * @psalm-suppress InvalidArgument From 6c4838b83963bfd41243f1fa3580a51966b9492d Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Sat, 31 Dec 2022 17:23:46 +0100 Subject: [PATCH 2/7] Remove database URLs from tests --- tests/DriverManagerTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/DriverManagerTest.php b/tests/DriverManagerTest.php index 7c493fd8fa1..0f44798a2ac 100644 --- a/tests/DriverManagerTest.php +++ b/tests/DriverManagerTest.php @@ -46,7 +46,8 @@ public function testCustomPlatform(): void { $platform = $this->createMock(AbstractPlatform::class); $options = [ - 'url' => 'sqlite::memory:', + 'driver' => 'pdo_sqlite', + 'memory' => true, 'platform' => $platform, ]; @@ -61,7 +62,8 @@ public function testCustomWrapper(): void $wrapperClass = get_class($wrapper); $options = [ - 'url' => 'sqlite::memory:', + 'driver' => 'pdo_sqlite', + 'memory' => true, 'wrapperClass' => $wrapperClass, ]; @@ -78,7 +80,8 @@ public function testInvalidWrapperClass(): void $this->expectException(Exception::class); $options = [ - 'url' => 'sqlite::memory:', + 'driver' => 'pdo_sqlite', + 'memory' => true, 'wrapperClass' => stdClass::class, ]; From 2eb9f52ac5be85646a73cdbe21fec5f2d2109d6f Mon Sep 17 00:00:00 2001 From: Rudolph Gottesheim Date: Tue, 3 Jan 2023 11:24:13 +0100 Subject: [PATCH 3/7] Use a more specific return type for DriverManager::getAvailableDrivers() --- src/DriverManager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DriverManager.php b/src/DriverManager.php index 5a4ae99ae1e..210bf1cfc22 100644 --- a/src/DriverManager.php +++ b/src/DriverManager.php @@ -222,6 +222,7 @@ public static function getConnection( * Returns the list of supported drivers. * * @return string[] + * @psalm-return list> */ public static function getAvailableDrivers(): array { From 9ed1ebcacb319aa58ccb30555930b429c13f56b0 Mon Sep 17 00:00:00 2001 From: Rudolph Gottesheim Date: Tue, 3 Jan 2023 13:40:07 +0100 Subject: [PATCH 4/7] Test the return type of DriverManager::getAvailableDrivers() --- phpstan.neon.dist | 1 + ...nager-get-available-drivers-return-type.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 static-analysis/driver-manager-get-available-drivers-return-type.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 99b3f8e4b7a..c0483abdaba 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,6 +3,7 @@ parameters: phpVersion: 80200 paths: - src + - static-analysis treatPhpDocTypesAsCertain: false reportUnmatchedIgnoredErrors: false checkMissingIterableValueType: false diff --git a/static-analysis/driver-manager-get-available-drivers-return-type.php b/static-analysis/driver-manager-get-available-drivers-return-type.php new file mode 100644 index 00000000000..e8beff9081f --- /dev/null +++ b/static-analysis/driver-manager-get-available-drivers-return-type.php @@ -0,0 +1,18 @@ + $driver]); From c66fcbd019c70ede811a68489a871efc49bec027 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 3 Jan 2023 19:47:46 +0100 Subject: [PATCH 5/7] Reuse the Params type alias for getConnection() --- src/DriverManager.php | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/DriverManager.php b/src/DriverManager.php index 210bf1cfc22..1a3f0bc6420 100644 --- a/src/DriverManager.php +++ b/src/DriverManager.php @@ -147,32 +147,9 @@ private function __construct() * * @param Configuration|null $config The configuration to use. * @param EventManager|null $eventManager The event manager to use. - * @psalm-param array{ - * charset?: string, - * dbname?: string, - * default_dbname?: string, - * driver?: key-of, - * driverClass?: class-string, - * driverOptions?: array, - * 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, - * sharding?: array, - * slaves?: array, - * user?: string, - * wrapperClass?: class-string, - * } $params + * @psalm-param Params $params * - * @psalm-return ($params is array{wrapperClass:mixed} ? T : Connection) + * @psalm-return ($params is array{wrapperClass: class-string} ? T : Connection) * * @throws Exception * From c709e352580d2ae59f0ad70ae3e4c0a6af9476ea Mon Sep 17 00:00:00 2001 From: Rudolph Gottesheim Date: Wed, 4 Jan 2023 15:57:10 +0100 Subject: [PATCH 6/7] Use narrower return types for convertTo*Value methods --- src/Platforms/AbstractPlatform.php | 6 ++++-- src/Platforms/PostgreSQLPlatform.php | 6 ++++++ src/Types/BigIntType.php | 6 ++++++ src/Types/BooleanType.php | 6 ++++++ src/Types/DateImmutableType.php | 12 ++++++++++++ src/Types/DateIntervalType.php | 12 ++++++++++++ src/Types/DateTimeImmutableType.php | 12 ++++++++++++ src/Types/DateTimeType.php | 12 ++++++++++++ src/Types/DateTimeTzImmutableType.php | 12 ++++++++++++ src/Types/DateTimeTzType.php | 12 ++++++++++++ src/Types/DateType.php | 12 ++++++++++++ src/Types/FloatType.php | 6 ++++++ src/Types/IntegerType.php | 6 ++++++ src/Types/JsonType.php | 6 ++++++ src/Types/ObjectType.php | 4 ++++ src/Types/SimpleArrayType.php | 8 ++++++++ src/Types/SmallIntType.php | 6 ++++++ src/Types/TimeImmutableType.php | 12 ++++++++++++ src/Types/TimeType.php | 12 ++++++++++++ src/Types/VarDateTimeImmutableType.php | 12 ++++++++++++ src/Types/VarDateTimeType.php | 7 +++++++ 21 files changed, 185 insertions(+), 2 deletions(-) diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index 28a0eb5807b..28e0782e8a9 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -3499,9 +3499,11 @@ public function convertBooleans($item) * * The default conversion tries to convert value into bool "(bool)$item" * - * @param mixed $item + * @param T $item * - * @return bool|null + * @return (T is null ? null : bool) + * + * @template T */ public function convertFromBoolean($item) { diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index c3e41d7302a..7c4592651b5 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -957,6 +957,12 @@ static function ($value): ?int { /** * {@inheritDoc} + * + * @param T $item + * + * @return (T is null ? null : bool) + * + * @template T */ public function convertFromBoolean($item) { diff --git a/src/Types/BigIntType.php b/src/Types/BigIntType.php index e5d6dcbbf65..795883b3a27 100644 --- a/src/Types/BigIntType.php +++ b/src/Types/BigIntType.php @@ -36,6 +36,12 @@ public function getBindingType() /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/BooleanType.php b/src/Types/BooleanType.php index 92f53055871..440ca72bd46 100644 --- a/src/Types/BooleanType.php +++ b/src/Types/BooleanType.php @@ -30,6 +30,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : bool) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateImmutableType.php b/src/Types/DateImmutableType.php index 98fa6dc61a4..86f05447044 100644 --- a/src/Types/DateImmutableType.php +++ b/src/Types/DateImmutableType.php @@ -21,6 +21,12 @@ public function getName() /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -41,6 +47,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeImmutable) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateIntervalType.php b/src/Types/DateIntervalType.php index 72156f17d6f..ac4885d97c9 100644 --- a/src/Types/DateIntervalType.php +++ b/src/Types/DateIntervalType.php @@ -36,6 +36,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -52,6 +58,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateInterval) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateTimeImmutableType.php b/src/Types/DateTimeImmutableType.php index 6f789620241..cb1a17f2973 100644 --- a/src/Types/DateTimeImmutableType.php +++ b/src/Types/DateTimeImmutableType.php @@ -23,6 +23,12 @@ public function getName() /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -43,6 +49,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeImmutable) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateTimeType.php b/src/Types/DateTimeType.php index 738c6bfb52c..4a75dfd7e1c 100644 --- a/src/Types/DateTimeType.php +++ b/src/Types/DateTimeType.php @@ -31,6 +31,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -47,6 +53,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeInterface) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateTimeTzImmutableType.php b/src/Types/DateTimeTzImmutableType.php index 222a9c3b086..aab826aa402 100644 --- a/src/Types/DateTimeTzImmutableType.php +++ b/src/Types/DateTimeTzImmutableType.php @@ -21,6 +21,12 @@ public function getName() /** * {@inheritdoc} + * + * @psalm-param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -41,6 +47,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeImmutable) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateTimeTzType.php b/src/Types/DateTimeTzType.php index ee08f9d1e6d..c3f3fc9cb1d 100644 --- a/src/Types/DateTimeTzType.php +++ b/src/Types/DateTimeTzType.php @@ -42,6 +42,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -62,6 +68,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeInterface) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/DateType.php b/src/Types/DateType.php index 533667dd573..7ce9656a77b 100644 --- a/src/Types/DateType.php +++ b/src/Types/DateType.php @@ -29,6 +29,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @psalm-param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -45,6 +51,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeInterface) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/FloatType.php b/src/Types/FloatType.php index 98ead4a9d6a..d8cb33557c5 100644 --- a/src/Types/FloatType.php +++ b/src/Types/FloatType.php @@ -24,6 +24,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : float) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/IntegerType.php b/src/Types/IntegerType.php index 0df606e2982..57ba7087e7c 100644 --- a/src/Types/IntegerType.php +++ b/src/Types/IntegerType.php @@ -28,6 +28,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : int) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/JsonType.php b/src/Types/JsonType.php index 20ee79deb7b..797f5bc3d9e 100644 --- a/src/Types/JsonType.php +++ b/src/Types/JsonType.php @@ -29,6 +29,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { diff --git a/src/Types/ObjectType.php b/src/Types/ObjectType.php index f8bcc94378c..1c497a3ffc7 100644 --- a/src/Types/ObjectType.php +++ b/src/Types/ObjectType.php @@ -29,6 +29,10 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param mixed $value + * + * @return string */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { diff --git a/src/Types/SimpleArrayType.php b/src/Types/SimpleArrayType.php index 3ec695cb721..0961ae3980e 100644 --- a/src/Types/SimpleArrayType.php +++ b/src/Types/SimpleArrayType.php @@ -29,6 +29,10 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param mixed $value + * + * @return string|null */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -41,6 +45,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param mixed $value + * + * @return list */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/SmallIntType.php b/src/Types/SmallIntType.php index 90e63495137..e38f4ca7217 100644 --- a/src/Types/SmallIntType.php +++ b/src/Types/SmallIntType.php @@ -28,6 +28,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : int) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/TimeImmutableType.php b/src/Types/TimeImmutableType.php index eef8ba91faf..a3f0040b44d 100644 --- a/src/Types/TimeImmutableType.php +++ b/src/Types/TimeImmutableType.php @@ -21,6 +21,12 @@ public function getName() /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -41,6 +47,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeImmutable) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/TimeType.php b/src/Types/TimeType.php index 9c8f03e98a9..0123d4ba821 100644 --- a/src/Types/TimeType.php +++ b/src/Types/TimeType.php @@ -29,6 +29,12 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -45,6 +51,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeInterface) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/VarDateTimeImmutableType.php b/src/Types/VarDateTimeImmutableType.php index 24bddc44360..9ac6905145d 100644 --- a/src/Types/VarDateTimeImmutableType.php +++ b/src/Types/VarDateTimeImmutableType.php @@ -23,6 +23,12 @@ public function getName() /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : string) + * + * @template T */ public function convertToDatabaseValue($value, AbstractPlatform $platform) { @@ -43,6 +49,12 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform) /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeImmutable) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { diff --git a/src/Types/VarDateTimeType.php b/src/Types/VarDateTimeType.php index b11ef287152..58c1208af54 100644 --- a/src/Types/VarDateTimeType.php +++ b/src/Types/VarDateTimeType.php @@ -3,6 +3,7 @@ namespace Doctrine\DBAL\Types; use DateTime; +use DateTimeInterface; use Doctrine\DBAL\Platforms\AbstractPlatform; use function date_create; @@ -18,6 +19,12 @@ class VarDateTimeType extends DateTimeType { /** * {@inheritdoc} + * + * @param T $value + * + * @return (T is null ? null : DateTimeInterface) + * + * @template T */ public function convertToPHPValue($value, AbstractPlatform $platform) { From 36a32f76ff86d5f65c268a420a924bf6765179f1 Mon Sep 17 00:00:00 2001 From: someniatko Date: Fri, 6 Jan 2023 15:47:10 +0200 Subject: [PATCH 7/7] Formally allow `url` in `DriverManager::getConnection()` --- src/DriverManager.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DriverManager.php b/src/DriverManager.php index a5f44783260..6a70af81db9 100644 --- a/src/DriverManager.php +++ b/src/DriverManager.php @@ -175,6 +175,7 @@ private function __construct() * replica?: array, * sharding?: array, * slaves?: array, + * url?: string, * user?: string, * wrapperClass?: class-string, * } $params