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 15, 2020
1 parent 0533e6f commit f0daabc
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 86 deletions.
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": "[email protected]"}
],
"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.

22 changes: 2 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 PDOConnectionTrait;

/**
* @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
62 changes: 62 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOConnectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Doctrine\DBAL\Driver;

use Doctrine\DBAL\Driver\PDO\Exception;
use PDOException;
use PDOStatement;

use const PHP_VERSION_ID;

use function assert;
use function func_get_args;

if (PHP_VERSION_ID >= 80000) {
/**
* @internal
*/
trait PDOConnectionTrait
{
/**
* {@inheritdoc}
*
* @return PDOStatement
*/
public function query(?string $query = null, ?int $fetchMode = null, mixed ...$fetchModeArgs)
{
try {
$stmt = parent::query($query, $fetchMode, ...$fetchModeArgs);
assert($stmt instanceof PDOStatement);

return $stmt;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}
}
} else {
/**
* @internal
*/
trait PDOConnectionTrait
{
/**
* {@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);
}
}
}
}
65 changes: 2 additions & 63 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
use PDOException;

use function array_slice;
use function assert;
use function func_get_args;
use function is_array;
use function sprintf;
use function trigger_error;

Expand All @@ -26,6 +24,8 @@
*/
class PDOStatement extends \PDOStatement implements StatementInterface, Result
{
use PDOStatementTrait;

private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
ParameterType::INTEGER => PDO::PARAM_INT,
Expand Down Expand Up @@ -54,34 +54,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 +136,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
138 changes: 138 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOStatementTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace Doctrine\DBAL\Driver;

use Doctrine\DBAL\Driver\PDO\Exception;
use PDOException;

use const PHP_VERSION_ID;

use function assert;
use function func_get_args;
use function is_array;

if (PHP_VERSION_ID >= 80000) {
/**
* @internal
*/
trait PDOStatementTrait
{
/**
* {@inheritdoc}
*
* @deprecated Use one of the fetch- or iterate-related methods.
*/
public function setFetchMode($mode, ...$args)
{
$mode = $this->convertFetchMode($mode);

$filteredArgs = array_filter($args, static function (mixed $value): bool {
return $value !== null;
});
if (empty($filteredArgs)) {
$args = [];
}

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

/**
* {@inheritdoc}
*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead.
*/
public function fetchAll($mode = null, ...$args)
{
if (null !== $mode) {
$mode = $this->convertFetchMode($mode);
}

$filteredArgs = array_filter($args, static function (mixed $value): bool {
return $value !== null;
});
if (empty($filteredArgs)) {
$args = [];
}

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

return $data;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}
}
} else {
/**
* @internal
*/
trait PDOStatementTrait
{
/**
* {@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}
*
* @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);
}
}
}
}
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Types\Type;
use IteratorAggregate;
use PDO;
use PDOStatement;
use Throwable;
use Traversable;

Expand Down Expand Up @@ -135,6 +136,10 @@ public function bindParam($param, &$variable, $type = ParameterType::STRING, $le
$this->params[$param] = $variable;
$this->types[$param] = $type;

if ($this->stmt instanceof PDOStatement) {
$length = $length ?? 0;
}

return $this->stmt->bindParam($param, $variable, $type, $length);
}

Expand Down

0 comments on commit f0daabc

Please sign in to comment.