Skip to content

Commit

Permalink
support PHP8, currently not backward compatible with earlier PHP vers…
Browse files Browse the repository at this point in the history
…ions.

# method signatures
- sync signatures of PDO methods with PHP8
	- PHP8 has named parameters, so also names of parameters should be compatible with parent
- change Exception thrown to sync with PHP8

# phpunit
- at() was deprecated as of phpunit9.3, so added a polyfill to avoid warnings for now. sebastianbergmann/phpunit#4297
- assertInternalType() was removed in phpunit9. sebastianbergmann/phpunit#3370
- setUp() and tearDown() have return type declaration of void as of phpunit8. sebastianbergmann/phpunit#3288
- @ExpectedException was removed in phpunit9. sebastianbergmann/phpunit#3333
  • Loading branch information
sj-i committed Jan 27, 2021
1 parent 3ac8607 commit dc0e41b
Show file tree
Hide file tree
Showing 23 changed files with 161 additions and 250 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
@@ -1,10 +1,7 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4
- 8.0

cache:
directories:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -9,14 +9,14 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=8.0",
"ext-pdo": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpunit": "^7.0",
"vimeo/psalm": "~3.11.0"
"phpunit/phpunit": "^7.0|^9.3",
"vimeo/psalm": "^4.4"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 8 additions & 8 deletions src/AbstractConnector.php
Expand Up @@ -81,7 +81,7 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement)
{
return $this->getPdo()->exec($statement);
}
Expand All @@ -97,33 +97,33 @@ public function inTransaction()
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null)
{
return $this->getPdo()->lastInsertId();
}

/**
* {@inheritdoc}
*/
public function prepare($statement)
public function prepare(string $statement, array $options = [])
{
return $this->getPdo()->prepare($statement);
return $this->getPdo()->prepare($statement, $options);
}

/**
* {@inheritdoc}
*/
public function query($statement, $param1 = null, $param2 = null, $param3 = null)
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
{
return $this->getPdo()->query($statement, $param1, $param2, $param3);
return $this->getPdo()->query($query, $fetchMode, ...$fetchModeArgs);
}

/**
* {@inheritdoc}
*/
public function quote($string, $parameter_type = \PDO::PARAM_STR)
public function quote(string $string, int $type = \PDO::PARAM_STR)
{
return $this->getPdo()->quote($string, $parameter_type);
return $this->getPdo()->quote($string, $type);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/ListenableConnection.php
Expand Up @@ -67,7 +67,7 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement)
{
$start = microtime(true);

Expand All @@ -93,17 +93,17 @@ public function inTransaction()
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null)
{
return $this->delegate->lastInsertId();
}

/**
* {@inheritdoc}
*/
public function prepare($statement)
public function prepare(string $statement, array $options = [])
{
$stmt = $this->delegate->prepare($statement);
$stmt = $this->delegate->prepare($statement, $options);
if ($stmt !== false) {
$stmt = new ListenableStatement($this->delegate, $this->listeners, $stmt, $statement);
}
Expand All @@ -113,20 +113,20 @@ public function prepare($statement)
/**
* {@inheritdoc}
*/
public function query($statement, $param1 = null, $param2 = null, $param3 = null)
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
{
$start = microtime(true);

$stmt = $this->delegate->query($statement, $param1, $param2, $param3);
$stmt = $this->delegate->query($query, $fetchMode, ...$fetchModeArgs);

if ($stmt !== false) {
$elapsedTime = microtime(true) - $start;

foreach ($this->listeners as $listener) {
$listener->onQuery($this->delegate, $statement, [], $elapsedTime);
$listener->onQuery($this->delegate, $query, [], $elapsedTime);
}

$stmt = new ListenableStatement($this->delegate, $this->listeners, $stmt, $statement);
$stmt = new ListenableStatement($this->delegate, $this->listeners, $stmt, $query);
}

return $stmt;
Expand All @@ -135,9 +135,9 @@ public function query($statement, $param1 = null, $param2 = null, $param3 = null
/**
* {@inheritdoc}
*/
public function quote($string, $parameter_type = \PDO::PARAM_STR)
public function quote(string $string, int $type = \PDO::PARAM_STR)
{
return $this->delegate->quote($string, $parameter_type);
return $this->delegate->quote($string, $type);
}

/**
Expand Down
28 changes: 14 additions & 14 deletions src/ListenableStatement.php
Expand Up @@ -51,11 +51,11 @@ public function getIterator()
/**
* {@inheritdoc}
*/
public function bindValue($parameter, $value, $data_type = \PDO::PARAM_STR)
public function bindValue(string $param, $value, int $type = \PDO::PARAM_STR)
{
$this->bindings[] = $value;

return $this->delegate->bindValue($parameter, $value, $data_type);
return $this->delegate->bindValue($param, $value, $type);
}

/**
Expand All @@ -77,16 +77,16 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function execute($input_parameters = null)
public function execute(?array $params = null)
{
$start = microtime(true);

try {
return $this->delegate->execute($input_parameters);
return $this->delegate->execute($params);
} finally {
$elapsedTime = microtime(true) - $start;
$bindings = $input_parameters !== null
? array_merge($this->bindings, $input_parameters)
$bindings = $params !== null
? array_merge($this->bindings, $params)
: $this->bindings;

foreach ($this->listeners as $listener) {
Expand All @@ -98,25 +98,25 @@ public function execute($input_parameters = null)
/**
* {@inheritdoc}
*/
public function fetch($fetch_style = null, $cursor_orientation = null, $cursor_offset = null)
public function fetch(int $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, int $cursorOrientation = PDO::FETCH_ORI_NEXT, int $cursorOffset = 0)
{
return $this->delegate->Fetch($fetch_style, $cursor_orientation, $cursor_offset);
return $this->delegate->Fetch($mode, $cursorOrientation, $cursorOffset);
}

/**
* {@inheritdoc}
*/
public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = null)
public function fetchAll(int $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, ...$args)
{
return $this->delegate->FetchAll($fetch_style, $fetch_argument, $ctor_args);
return $this->delegate->FetchAll($mode, ...$args);
}

/**
* {@inheritdoc}
*/
public function fetchColumn($column_number = 0)
public function fetchColumn(int $column = 0)
{
return $this->delegate->fetchColumn($column_number);
return $this->delegate->fetchColumn($column);
}

/**
Expand All @@ -130,8 +130,8 @@ public function rowCount()
/**
* {@inheritdoc}
*/
public function setFetchMode($mode, $param1 = null, $param2 = null)
public function setFetchMode(int $mode, ...$args)
{
return $this->delegate->setFetchMode($mode, $param1, $param2);
return $this->delegate->setFetchMode($mode, ...$args);
}
}
14 changes: 7 additions & 7 deletions src/MasterSlaveConnection.php
Expand Up @@ -77,7 +77,7 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement)
{
return $this->activePdo->exec($statement);
}
Expand All @@ -93,33 +93,33 @@ public function inTransaction()
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null)
{
return $this->activePdo->lastInsertId();
}

/**
* {@inheritdoc}
*/
public function prepare($statement)
public function prepare(string $statement, array $options = [])
{
return $this->activePdo->prepare($statement);
}

/**
* {@inheritdoc}
*/
public function query($statement, $param1 = null, $param2 = null, $param3 = null)
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
{
return $this->activePdo->query($statement, $param1, $param2, $param3);
return $this->activePdo->query($query, $fetchMode, ...$fetchModeArgs);
}

/**
* {@inheritdoc}
*/
public function quote($string, $parameter_type = \PDO::PARAM_STR)
public function quote(string $string, int $type = \PDO::PARAM_STR)
{
return $this->activePdo->quote($string, $parameter_type);
return $this->activePdo->quote($string, $type);
}

/**
Expand Down
23 changes: 9 additions & 14 deletions src/MysqliAdapter.php
Expand Up @@ -77,7 +77,7 @@ public function errorInfo()
/**
* {@inheritdoc}
*/
public function exec($statement)
public function exec(string $statement)
{
if (!$this->mysqli->real_query($statement)) {
return false;
Expand All @@ -96,15 +96,15 @@ public function inTransaction()
/**
* {@inheritdoc}
*/
public function lastInsertId($name = null)
public function lastInsertId(?string $name = null)
{
return $this->mysqli->insert_id;
return (string)$this->mysqli->insert_id;
}

/**
* {@inheritdoc}
*/
public function prepare($statement)
public function prepare(string $statement, array $options = [])
{
$stmt = $this->mysqli->prepare($statement);
return $stmt !== false ? new MysqliStmtAdapter($stmt) : false;
Expand All @@ -113,18 +113,13 @@ public function prepare($statement)
/**
* {@inheritdoc}
*/
public function query($statement, $param1 = null, $param2 = null, $param3 = null)
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
{
$stmt = $this->prepare($statement);
$stmt = $this->prepare($query);

if ($stmt !== false) {
if ($param1 === null) {
} elseif ($param2 === null) {
$stmt->setFetchMode($param1);
} elseif ($param3 !== null) {
$stmt->setFetchMode($param1, $param2);
} else {
$stmt->setFetchMode($param1, $param2, $param3);
if (!is_null($fetchMode)) {
$stmt->setFetchMode($fetchMode, ...$fetchModeArgs);
}

$stmt->execute();
Expand All @@ -136,7 +131,7 @@ public function query($statement, $param1 = null, $param2 = null, $param3 = null
/**
* {@inheritdoc}
*/
public function quote($string, $parameter_type = \PDO::PARAM_STR)
public function quote(string $string, int $type = \PDO::PARAM_STR)
{
return "'" . $this->mysqli->real_escape_string($string) . "'";
}
Expand Down

0 comments on commit dc0e41b

Please sign in to comment.