Skip to content

Commit

Permalink
PHP 8 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Oct 19, 2020
1 parent a1f820e commit a3135e2
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 84 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
php-version:
- "7.3"
- "7.4"
- "8.0"
deps:
- "fixed"
include:
Expand Down Expand Up @@ -183,6 +184,9 @@ jobs:
- "11"
- "12"
- "13"
include:
- php-version: "8.0"
postgres-version: "13"

services:
postgres:
Expand Down Expand Up @@ -247,6 +251,13 @@ jobs:
extension:
- "mysqli"
- "pdo_mysql"
include:
- php-version: "8.0"
mariadb-version: "10.5"
extension: "mysqli"
- php-version: "8.0"
mariadb-version: "10.5"
extension: "pdo_mysql"

services:
mariadb:
Expand Down Expand Up @@ -304,6 +315,7 @@ jobs:
matrix:
php-version:
- "7.4"
- "8.0"
mysql-version:
- "5.7"
- "8.0"
Expand Down
15 changes: 15 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Upgrade to 2.12

## PDO signature changes with php 8

In php 8.0, the method signatures of two PDO classes which are extended by DBAL have changed. This affects the following classes:

* `Doctrine\DBAL\Driver\PDOConnection`
* `Doctrine\DBAL\Driver\PDOStatement`

Code that extends either of the classes needs to be adjusted in order to function properly on php 8. The updated method signatures are:

* `PDOConnection::query(?string $query = null, ?int $fetchMode = null, mixed ...$fetchModeArgs)`
* `PDOStatement::setFetchMode($mode, ...$args)`
* `PDOStatement::fetchAll($mode = null, ...$args)`

# Upgrade to 2.11

## Deprecated `Abstraction\Result`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{"name": "Jonathan Wage", "email": "jonwage@gmail.com"}
],
"require": {
"php": "^7.3",
"php": "^7.3 || ^8",
"ext-pdo": "*",
"doctrine/cache": "^1.0",
"doctrine/event-manager": "^1.0"
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 18 additions & 20 deletions lib/Doctrine/DBAL/Driver/PDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PDOStatement;

use function assert;
use function func_get_args;

/**
* PDO implementation of the Connection interface.
Expand All @@ -21,6 +20,8 @@
*/
class PDOConnection extends PDO implements ConnectionInterface, ServerInfoAwareConnection
{
use PDOQueryImplementation;

/**
* @internal The connection can be only instantiated by its driver.
*
Expand Down Expand Up @@ -83,25 +84,6 @@ public function prepare($sql, $driverOptions = [])
}
}

/**
* {@inheritdoc}
*
* @return PDOStatement
*/
public function query()
{
$args = func_get_args();

try {
$stmt = parent::query(...$args);
assert($stmt instanceof PDOStatement);

return $stmt;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -133,4 +115,20 @@ public function requiresQueryForServerVersion()
{
return false;
}

/**
* @param mixed ...$args
*/
private function doQuery(...$args): PDOStatement
{
try {
$stmt = parent::query(...$args);
} catch (PDOException $exception) {
throw Exception::new($exception);
}

assert($stmt instanceof PDOStatement);

return $stmt;
}
}
39 changes: 39 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOQueryImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\DBAL\Driver;

use PDOStatement;

use function func_get_args;

use const PHP_VERSION_ID;

if (PHP_VERSION_ID >= 80000) {
/**
* @internal
*/
trait PDOQueryImplementation
{
/**
* @return PDOStatement
*/
public function query(?string $query = null, ?int $fetchMode = null, mixed ...$fetchModeArgs)
{
return $this->doQuery($query, $fetchMode, ...$fetchModeArgs);
}
}
} else {
/**
* @internal
*/
trait PDOQueryImplementation
{
/**
* @return PDOStatement
*/
public function query()
{
return $this->doQuery(...func_get_args());
}
}
}
123 changes: 62 additions & 61 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
class PDOStatement extends \PDOStatement implements StatementInterface, Result
{
use PDOStatementImplementations;

private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
ParameterType::INTEGER => PDO::PARAM_INT,
Expand Down Expand Up @@ -54,34 +56,6 @@ protected function __construct()
{
}

/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
{
$fetchMode = $this->convertFetchMode($fetchMode);

// This thin wrapper is necessary to shield against the weird signature
// of PDOStatement::setFetchMode(): even if the second and third
// parameters are optional, PHP will not let us remove it from this
// declaration.
try {
if ($arg2 === null && $arg3 === null) {
return parent::setFetchMode($fetchMode);
}

if ($arg3 === null) {
return parent::setFetchMode($fetchMode, $arg2);
}

return parent::setFetchMode($fetchMode, $arg2, $arg3);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -164,39 +138,6 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX
}
}

/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
$args = func_get_args();

if (isset($args[0])) {
$args[0] = $this->convertFetchMode($args[0]);
}

if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) {
$args = [];
} elseif ($fetchArgument === null && $ctorArgs === null) {
$args = [$fetchMode];
} elseif ($ctorArgs === null) {
$args = [$fetchMode, $fetchArgument];
} else {
$args = [$fetchMode, $fetchArgument, $ctorArgs];
}

try {
$data = parent::fetchAll(...$args);
assert(is_array($data));

return $data;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -264,6 +205,66 @@ public function free(): void
parent::closeCursor();
}

/**
* @param mixed ...$args
*/
private function doSetFetchMode(int $fetchMode, ...$args): bool
{
$fetchMode = $this->convertFetchMode($fetchMode);

// This thin wrapper is necessary to shield against the weird signature
// of PDOStatement::setFetchMode(): even if the second and third
// parameters are optional, PHP will not let us remove it from this
// declaration.
$slice = [];

foreach ($args as $arg) {
if ($arg === null) {
break;
}

$slice[] = $arg;
}

try {
return parent::setFetchMode($fetchMode, ...$slice);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* @param mixed ...$args
*
* @return mixed[]
*/
private function doFetchAll(...$args): array
{
if (isset($args[0])) {
$args[0] = $this->convertFetchMode($args[0]);
}

$slice = [];

foreach ($args as $arg) {
if ($arg === null) {
break;
}

$slice[] = $arg;
}

try {
$data = parent::fetchAll(...$slice);
} catch (PDOException $exception) {
throw Exception::new($exception);
}

assert(is_array($data));

return $data;
}

/**
* Converts DBAL parameter type to PDO parameter type
*
Expand Down
Loading

0 comments on commit a3135e2

Please sign in to comment.