From 4288aca3a4b40096c184bef9fda52cacd7e091cf Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Fri, 29 Jul 2022 15:16:31 -0700 Subject: [PATCH] Remove support for using NULL as prepared statement parameter type --- UPGRADE.md | 5 + phpcs.xml.dist | 5 - src/Cache/QueryCacheProfile.php | 6 +- src/Connection.php | 125 ++++++++---------- src/ExpandArrayParameters.php | 10 +- src/Query.php | 6 +- src/Query/QueryBuilder.php | 34 ++--- .../Connection/ExpandArrayParametersTest.php | 18 +-- tests/Query/QueryBuilderTest.php | 38 +++--- 9 files changed, 109 insertions(+), 138 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index b5333f01e85..de2949f06c2 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,11 @@ awareness about deprecated code. # Upgrade to 4.0 +## BC BREAK: removed support using NULL as prepared statement parameter type. + +The value of parameter type used in the wrapper layer (e.g. in `Connection::executeQuery()` +or `Statement::bindValue()`) can no longer be `NULL`. + ## BC BREAK: converted enum-like classes to enums The following classes have been converted to enums: diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 951ab36a0e3..4acb7c33cfa 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -124,9 +124,4 @@ src/ParameterType.php - - - - src/Cache/QueryCacheProfile.php - diff --git a/src/Cache/QueryCacheProfile.php b/src/Cache/QueryCacheProfile.php index 4633dc01396..619d5e44aa5 100644 --- a/src/Cache/QueryCacheProfile.php +++ b/src/Cache/QueryCacheProfile.php @@ -52,9 +52,9 @@ public function getCacheKey(): string /** * Generates the real cache key from query, params, types and connection parameters. * - * @param list|array $params - * @param array|array $types - * @param array $connectionParams + * @param list|array $params + * @param array|array $types + * @param array $connectionParams * * @return string[] */ diff --git a/src/Connection.php b/src/Connection.php index a993755fa96..9f8c244b7a6 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -35,7 +35,6 @@ use Throwable; use Traversable; -use function array_key_exists; use function assert; use function count; use function implode; @@ -361,8 +360,8 @@ public function setAutoCommit(bool $autoCommit): void * Prepares and executes an SQL query and returns the first row of the result * as an associative array. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return array|false False is returned if no rows are found. * @@ -377,8 +376,8 @@ public function fetchAssociative(string $query, array $params = [], array $types * Prepares and executes an SQL query and returns the first row of the result * as a numerically indexed array. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list|false False is returned if no rows are found. * @@ -393,8 +392,8 @@ public function fetchNumeric(string $query, array $params = [], array $types = [ * Prepares and executes an SQL query and returns the value of a single column * of the first row of the result. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return mixed|false False is returned if no rows are found. * @@ -456,8 +455,8 @@ private function addCriteriaCondition( * * Table expression and columns are not escaped and are not safe for user-input. * - * @param array $criteria - * @param array|array $types + * @param array $criteria + * @param array|array $types * * @return int|string The number of affected rows. * @@ -520,9 +519,9 @@ public function getTransactionIsolation(): TransactionIsolationLevel * * Table expression and columns are not escaped and are not safe for user-input. * - * @param array $data - * @param array $criteria - * @param array|array $types + * @param array $data + * @param array $criteria + * @param array|array $types * * @return int|string The number of affected rows. * @@ -555,8 +554,8 @@ public function update(string $table, array $data, array $criteria, array $types * * Table expression and columns are not escaped and are not safe for user-input. * - * @param array $data - * @param array|array $types + * @param array $data + * @param array|array $types * * @return int|string The number of affected rows. * @@ -589,10 +588,10 @@ public function insert(string $table, array $data, array $types = []): int|strin /** * Extract ordered type list from an ordered column list and type map. * - * @param array $columns - * @param array|array $types + * @param array $columns + * @param array|array $types * - * @return array|array + * @return array|array */ private function extractTypeValues(array $columns, array $types): array { @@ -636,8 +635,8 @@ public function quote(string $value): string /** * Prepares and executes an SQL query and returns the result as an array of numeric arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list> * @@ -651,8 +650,8 @@ public function fetchAllNumeric(string $query, array $params = [], array $types /** * Prepares and executes an SQL query and returns the result as an array of associative arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list> * @@ -667,8 +666,8 @@ public function fetchAllAssociative(string $query, array $params = [], array $ty * Prepares and executes an SQL query and returns the result as an associative array with the keys * mapped to the first column and the values mapped to the second column. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return array * @@ -684,8 +683,8 @@ public function fetchAllKeyValue(string $query, array $params = [], array $types * to the first column and the values being an associative array representing the rest of the columns * and their values. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return array> * @@ -699,8 +698,8 @@ public function fetchAllAssociativeIndexed(string $query, array $params = [], ar /** * Prepares and executes an SQL query and returns the result as an array of the first column values. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return list * @@ -714,8 +713,8 @@ public function fetchFirstColumn(string $query, array $params = [], array $types /** * Prepares and executes an SQL query and returns the result as an iterator over rows represented as numeric arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable> * @@ -730,8 +729,8 @@ public function iterateNumeric(string $query, array $params = [], array $types = * Prepares and executes an SQL query and returns the result as an iterator over rows represented * as associative arrays. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable> * @@ -746,8 +745,8 @@ public function iterateAssociative(string $query, array $params = [], array $typ * Prepares and executes an SQL query and returns the result as an iterator with the keys * mapped to the first column and the values mapped to the second column. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable * @@ -779,8 +778,8 @@ public function iterateAssociativeIndexed(string $query, array $params = [], arr /** * Prepares and executes an SQL query and returns the result as an iterator over the first column values. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return Traversable * @@ -816,8 +815,8 @@ public function prepare(string $sql): Statement * * If the query is parametrized, a prepared statement is used. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws Exception */ @@ -857,8 +856,8 @@ public function executeQuery( /** * Executes a caching query. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws CacheException * @throws Exception @@ -915,8 +914,8 @@ public function executeCacheQuery(string $sql, array $params, array $types, Quer * * This method supports PDO binding types as well as DBAL mapping types. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws Exception */ @@ -1309,8 +1308,8 @@ public function convertToPHPValue(mixed $value, string $type): mixed * Binds a set of parameters, some or all of which are typed with a PDO binding type * or DBAL mapping type, to a given statement. * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @throws Exception */ @@ -1326,15 +1325,6 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t [$value, $bindingType] = $this->getBindingInfo($value, $type); $stmt->bindValue($bindIndex, $value, $bindingType); } else { - if (array_key_exists($key, $types)) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5550', - 'Using NULL as prepared statement parameter type is deprecated.' - . 'Omit or use Parameter::STRING instead' - ); - } - $stmt->bindValue($bindIndex, $value); } @@ -1348,15 +1338,6 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t [$value, $bindingType] = $this->getBindingInfo($value, $type); $stmt->bindValue($name, $value, $bindingType); } else { - if (array_key_exists($name, $types)) { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5550', - 'Using NULL as prepared statement parameter type is deprecated.' - . 'Omit or use Parameter::STRING instead' - ); - } - $stmt->bindValue($name, $value); } } @@ -1366,14 +1347,14 @@ private function _bindTypedValues(DriverStatement $stmt, array $params, array $t /** * Gets the binding type of a given type. * - * @param mixed $value The value to bind. - * @param string|ParameterType|Type|null $type The type to bind. + * @param mixed $value The value to bind. + * @param string|ParameterType|Type $type The type to bind. * * @return array{mixed, ParameterType} [0] => the (escaped) value, [1] => the binding type. * * @throws Exception */ - private function getBindingInfo(mixed $value, string|ParameterType|Type|null $type): array + private function getBindingInfo(mixed $value, string|ParameterType|Type $type): array { if (is_string($type)) { $type = Type::getType($type); @@ -1383,7 +1364,7 @@ private function getBindingInfo(mixed $value, string|ParameterType|Type|null $ty $value = $type->convertToDatabaseValue($value, $this->getDatabasePlatform()); $bindingType = $type->getBindingType(); } else { - $bindingType = $type ?? ParameterType::STRING; + $bindingType = $type; } return [$value, $bindingType]; @@ -1400,8 +1381,8 @@ public function createQueryBuilder(): QueryBuilder /** * @internal * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types */ final public function convertExceptionDuringQuery( Driver\Exception $e, @@ -1421,13 +1402,13 @@ final public function convertException(Driver\Exception $e): DriverException } /** - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * * @return array{ * string, * array|array, - * array|array + * array|array * } */ private function expandArrayParameters(string $sql, array $params, array $types): array @@ -1483,8 +1464,8 @@ private function handleDriverException( * * @deprecated This API is deprecated and will be removed after 2022 * - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * * @throws Exception */ diff --git a/src/ExpandArrayParameters.php b/src/ExpandArrayParameters.php index 0e7e8ad2c50..4daae7bc7df 100644 --- a/src/ExpandArrayParameters.php +++ b/src/ExpandArrayParameters.php @@ -27,12 +27,12 @@ final class ExpandArrayParameters implements Visitor /** @var list */ private array $convertedParameters = []; - /** @var array */ + /** @var array */ private array $convertedTypes = []; /** - * @param array|array $parameters - * @param array|array $types + * @param array|array $parameters + * @param array|array $types */ public function __construct(private readonly array $parameters, private readonly array $types) { @@ -115,7 +115,7 @@ private function acceptParameter(int|string $key, mixed $value): void } /** - * @return array + * @return array */ public function getTypes(): array { @@ -125,7 +125,7 @@ public function getTypes(): array /** * @param list $values */ - private function appendTypedParameter(array $values, string|ParameterType|Type|null $type): void + private function appendTypedParameter(array $values, string|ParameterType|Type $type): void { $this->convertedSQL[] = implode(', ', array_fill(0, count($values), '?')); diff --git a/src/Query.php b/src/Query.php index 82e92f3a23b..8caae6ef57b 100644 --- a/src/Query.php +++ b/src/Query.php @@ -14,8 +14,8 @@ final class Query { /** - * @param array $params - * @param array $types + * @param array $params + * @param array $types * * @psalm-suppress ImpurePropertyAssignment */ @@ -40,7 +40,7 @@ public function getParams(): array } /** - * @return array + * @return array */ public function getTypes(): array { diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 6976cb08059..ec23a5b8147 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -52,7 +52,7 @@ class QueryBuilder /** * The parameter type map of this query. * - * @var array|array + * @var array|array */ private array $types = []; @@ -342,29 +342,19 @@ public function getSQL(): string * ->setParameter('user_id', 1); * * - * @param int|string $key Parameter position or name - * @param mixed $value Parameter value - * @param string|ParameterType|Type|null $type Parameter type + * @param int|string $key Parameter position or name + * @param mixed $value Parameter value + * @param string|ParameterType|Type $type Parameter type * * @return $this This QueryBuilder instance. */ public function setParameter( int|string $key, mixed $value, - string|ParameterType|Type|null $type = ParameterType::STRING + string|ParameterType|Type $type = ParameterType::STRING ): self { - if ($type !== null) { - $this->types[$key] = $type; - } else { - Deprecation::trigger( - 'doctrine/dbal', - 'https://github.com/doctrine/dbal/pull/5550', - 'Using NULL as prepared statement parameter type is deprecated.' - . 'Omit or use Parameter::STRING instead' - ); - } - $this->params[$key] = $value; + $this->types[$key] = $type; return $this; } @@ -383,8 +373,8 @@ public function setParameter( * )); * * - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @return $this This QueryBuilder instance. */ @@ -421,7 +411,7 @@ public function getParameter(string|int $key): mixed /** * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. * - * @return array|array + * @return array|array */ public function getParameterTypes(): array { @@ -435,7 +425,7 @@ public function getParameterTypes(): array * * @return int|string|ParameterType|Type The value of the bound parameter type */ - public function getParameterType(int|string $key): int|string|ParameterType|Type|null + public function getParameterType(int|string $key): int|string|ParameterType|Type { return $this->types[$key] ?? ParameterType::STRING; } @@ -1320,7 +1310,7 @@ public function __toString(): string */ public function createNamedParameter( mixed $value, - string|ParameterType|Type|null $type = ParameterType::STRING, + string|ParameterType|Type $type = ParameterType::STRING, ?string $placeHolder = null ): string { if ($placeHolder === null) { @@ -1352,7 +1342,7 @@ public function createNamedParameter( */ public function createPositionalParameter( mixed $value, - string|ParameterType|Type|null $type = ParameterType::STRING + string|ParameterType|Type $type = ParameterType::STRING ): string { $this->setParameter($this->boundCounter, $value, $type); $this->boundCounter++; diff --git a/tests/Connection/ExpandArrayParametersTest.php b/tests/Connection/ExpandArrayParametersTest.php index fb4c63bfb47..f69bf5703fb 100644 --- a/tests/Connection/ExpandArrayParametersTest.php +++ b/tests/Connection/ExpandArrayParametersTest.php @@ -318,10 +318,10 @@ public static function dataExpandListParameters(): iterable } /** - * @param array|array $params - * @param array|array $types - * @param list $expectedParams - * @param array $expectedTypes + * @param array|array $params + * @param array|array $types + * @param list $expectedParams + * @param array $expectedTypes * * @dataProvider dataExpandListParameters */ @@ -370,8 +370,8 @@ public static function missingNamedParameterProvider(): iterable } /** - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * * @dataProvider missingNamedParameterProvider */ @@ -412,10 +412,10 @@ public static function missingPositionalParameterProvider(): iterable } /** - * @param array|array $params - * @param array|array $types + * @param array|array $params + * @param array|array $types * - * @return array{string, list, array} + * @return array{string, list, array} */ private function expandArrayParameters(string $sql, array $params, array $types): array { diff --git a/tests/Query/QueryBuilderTest.php b/tests/Query/QueryBuilderTest.php index 1a165d36059..8c6128e4ef2 100644 --- a/tests/Query/QueryBuilderTest.php +++ b/tests/Query/QueryBuilderTest.php @@ -825,8 +825,8 @@ public function testJoinWithNonUniqueAliasThrowsException(): void } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -855,8 +855,8 @@ public function testFetchAssociative( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -885,8 +885,8 @@ public function testFetchNumeric( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -915,8 +915,8 @@ public function testFetchOne( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -956,8 +956,8 @@ public function testFetchAllAssociative( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -997,8 +997,8 @@ public function testFetchAllNumeric( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1038,8 +1038,8 @@ public function testFetchAllKeyValue( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1083,8 +1083,8 @@ public function testFetchAllAssociativeIndexed( } /** - * @param list|array $parameters - * @param array|array $parameterTypes + * @param list|array $parameters + * @param array|array $parameterTypes * * @dataProvider fetchProvider */ @@ -1131,7 +1131,7 @@ public function testFetchFirstColumn( * string, * string, * list|array, - * array|array, + * array|array, * string * }> */ @@ -1175,8 +1175,8 @@ public static function fetchProvider(): iterable } /** - * @param list|array $params - * @param array|array $types + * @param list|array $params + * @param array|array $types * * @dataProvider fetchProvider */