From dc0e41b9afdc6b5e4c12221bb1b022cae2758f8d Mon Sep 17 00:00:00 2001 From: sji Date: Mon, 25 Jan 2021 07:00:47 +0900 Subject: [PATCH] support PHP8, currently not backward compatible with earlier PHP versions. # 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. https://github.com/sebastianbergmann/phpunit/issues/4297 - assertInternalType() was removed in phpunit9. https://github.com/sebastianbergmann/phpunit/issues/3370 - setUp() and tearDown() have return type declaration of void as of phpunit8. https://github.com/sebastianbergmann/phpunit/issues/3288 - @expectedException was removed in phpunit9. https://github.com/sebastianbergmann/phpunit/issues/3333 --- .travis.yml | 5 +-- composer.json | 6 +-- src/AbstractConnector.php | 16 ++++---- src/ListenableConnection.php | 20 +++++----- src/ListenableStatement.php | 28 ++++++------- src/MasterSlaveConnection.php | 14 +++---- src/MysqliAdapter.php | 23 +++++------ src/MysqliStmtAdapter.php | 62 +++++++++++++++-------------- src/PDO.php | 19 --------- src/PDOAdapter.php | 31 ++++++--------- src/PDOInterface.php | 26 ++++++------ src/PDOStatement.php | 56 -------------------------- src/PDOStatementInterface.php | 34 ++++++++-------- tests/AbstractPDOStatementTest.php | 15 +++---- tests/AbstractPDOTest.php | 16 +++----- tests/ListenableConnectionTest.php | 2 +- tests/ListenableStatementTest.php | 8 ++-- tests/MasterSlaveConnectionTest.php | 2 +- tests/MysqliAdapterTest.php | 4 +- tests/MysqliConnectorTest.php | 4 +- tests/MysqliStmtAdapterTest.php | 4 +- tests/NestedTransactionTest.php | 8 +++- tests/PDOStatementIteratorTest.php | 8 +++- 23 files changed, 161 insertions(+), 250 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0459e07..2139559 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,7 @@ language: php php: - - 7.1 - - 7.2 - - 7.3 - - 7.4 + - 8.0 cache: directories: diff --git a/composer.json b/composer.json index 2500fff..3ac9a38 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/AbstractConnector.php b/src/AbstractConnector.php index 6fd415b..0aba425 100644 --- a/src/AbstractConnector.php +++ b/src/AbstractConnector.php @@ -81,7 +81,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) { return $this->getPdo()->exec($statement); } @@ -97,7 +97,7 @@ public function inTransaction() /** * {@inheritdoc} */ - public function lastInsertId($name = null) + public function lastInsertId(?string $name = null) { return $this->getPdo()->lastInsertId(); } @@ -105,25 +105,25 @@ public function lastInsertId($name = null) /** * {@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); } /** diff --git a/src/ListenableConnection.php b/src/ListenableConnection.php index 181051d..b3a72bf 100644 --- a/src/ListenableConnection.php +++ b/src/ListenableConnection.php @@ -67,7 +67,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) { $start = microtime(true); @@ -93,7 +93,7 @@ public function inTransaction() /** * {@inheritdoc} */ - public function lastInsertId($name = null) + public function lastInsertId(?string $name = null) { return $this->delegate->lastInsertId(); } @@ -101,9 +101,9 @@ public function lastInsertId($name = null) /** * {@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); } @@ -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; @@ -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); } /** diff --git a/src/ListenableStatement.php b/src/ListenableStatement.php index fb906f6..7e8c0dc 100644 --- a/src/ListenableStatement.php +++ b/src/ListenableStatement.php @@ -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); } /** @@ -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) { @@ -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); } /** @@ -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); } } diff --git a/src/MasterSlaveConnection.php b/src/MasterSlaveConnection.php index 9740aca..c6aca0f 100644 --- a/src/MasterSlaveConnection.php +++ b/src/MasterSlaveConnection.php @@ -77,7 +77,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) { return $this->activePdo->exec($statement); } @@ -93,7 +93,7 @@ public function inTransaction() /** * {@inheritdoc} */ - public function lastInsertId($name = null) + public function lastInsertId(?string $name = null) { return $this->activePdo->lastInsertId(); } @@ -101,7 +101,7 @@ public function lastInsertId($name = null) /** * {@inheritdoc} */ - public function prepare($statement) + public function prepare(string $statement, array $options = []) { return $this->activePdo->prepare($statement); } @@ -109,17 +109,17 @@ public function 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); } /** diff --git a/src/MysqliAdapter.php b/src/MysqliAdapter.php index 376c18c..2521f5c 100644 --- a/src/MysqliAdapter.php +++ b/src/MysqliAdapter.php @@ -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; @@ -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; @@ -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(); @@ -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) . "'"; } diff --git a/src/MysqliStmtAdapter.php b/src/MysqliStmtAdapter.php index 9ec2ffe..e2915c0 100644 --- a/src/MysqliStmtAdapter.php +++ b/src/MysqliStmtAdapter.php @@ -20,7 +20,7 @@ class MysqliStmtAdapter implements \IteratorAggregate, PDOStatementInterface /** * @var int */ - private $fetch_style = MYSQLI_BOTH; + private $fetch_style = \PDO::FETCH_BOTH; /** * @var mixed @@ -65,9 +65,9 @@ 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) { - switch ($data_type) { + switch ($type) { case \PDO::PARAM_BOOL: $this->bind_types .= 'i'; $this->bind_values[] = $value ? 1 : 0; @@ -116,23 +116,23 @@ public function errorInfo() /** * {@inheritdoc} */ - public function execute($input_parameters = null) + public function execute(?array $params = null) { $bind_types = $this->bind_types; - $params = [&$bind_types]; + $bind_params = [&$bind_types]; - if ($input_parameters !== null) { - foreach ($input_parameters as $i => $_) { + if ($params !== null) { + foreach ($params as $i => $_) { $bind_types .= 's'; - $params[] = &$input_parameters[$i]; + $bind_params[] = &$params[$i]; } } if ($bind_types !== '') { foreach ($this->bind_values as $i => $_) { - $params[] = &$this->bind_values[$i]; + $bind_params[] = &$this->bind_values[$i]; } - $this->stmt->bind_param(...$params); + $this->stmt->bind_param(...$bind_params); } if (!$this->stmt->execute()) { @@ -147,17 +147,17 @@ 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) { if ($this->result === null) { return false; } - if ($fetch_style === null) { - $fetch_style = $this->fetch_style; + if ($mode === \PDO::ATTR_DEFAULT_FETCH_MODE) { + $mode = $this->fetch_style; } - switch ($fetch_style) { + switch ($mode) { case \PDO::FETCH_BOTH: return $this->result->fetch_array(MYSQLI_BOTH) ?: false; @@ -169,9 +169,9 @@ public function fetch($fetch_style = null, $cursor_orientation = null, $cursor_o case \PDO::FETCH_CLASS: /** @psalm-var string */ - $class = $cursor_orientation ?: $this->fetch_argument ?: \stdClass::class; + $class = $cursorOrientation ?: $this->fetch_argument ?: \stdClass::class; /** @psalm-var ?array */ - $params = $cursor_offset ?: $this->ctor_args; + $params = $cursorOffset ?: $this->ctor_args; if ($params !== null) { $result = $this->result->fetch_object($class, $params) ?: false; } else { @@ -179,27 +179,27 @@ public function fetch($fetch_style = null, $cursor_orientation = null, $cursor_o } return $result; case \PDO::FETCH_COLUMN: - $column_number = $cursor_orientation ?: $this->fetch_argument ?: 0; + $column_number = $cursorOrientation ?: $this->fetch_argument ?: 0; return $this->doFetchColumn($this->result, $column_number); } - throw new \UnexpectedValueException("Unsupported fetch style, got '$fetch_style'"); + throw new \ValueError("Unsupported fetch style, got '$mode'"); } /** * {@inheritdoc} */ - public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = null) + public function fetchAll(int $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, ...$args) { if ($this->result === null) { return []; } - if ($fetch_style === null) { - $fetch_style = $this->fetch_style; + if ($mode === \PDO::ATTR_DEFAULT_FETCH_MODE) { + $mode = $this->fetch_style; } - switch ($fetch_style) { + switch ($mode) { case \PDO::FETCH_BOTH: return $this->result->fetch_all(MYSQLI_BOTH); @@ -210,11 +210,14 @@ public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args return $this->result->fetch_all(MYSQLI_NUM); case \PDO::FETCH_CLASS: + $fetch_argument = $args[0] ?? null; + $ctor_args = $args[1] ?? null; /** @psalm-var string */ $class = $fetch_argument ?: $this->fetch_argument ?: \stdClass::class; $params = $ctor_args ?: $this->ctor_args; $rows = []; if ($params !== null) { + assert(is_array($params)); while (($row = $this->result->fetch_object($class, $params)) !== null) { $rows[] = $row; } @@ -226,29 +229,30 @@ public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args return $rows; case \PDO::FETCH_COLUMN: + $fetch_argument = $args[0] ?? null; $columns = []; $column_number = $fetch_argument ?: $this->fetch_argument ?: 0; while (($row = $this->result->fetch_array(MYSQLI_NUM)) !== null) { if (!isset($row[$column_number])) { - throw new \RuntimeException('Invalid column index'); + throw new \ValueError('Invalid column index'); } $columns[] = $row[$column_number]; } return $columns; } - throw new \UnexpectedValueException("Unsupported fetch style, got '$fetch_style'"); + throw new \ValueError("Unsupported fetch style, got '$mode'"); } /** * {@inheritdoc} */ - public function fetchColumn($column_number = 0) + public function fetchColumn(int $column = 0) { if ($this->result === null) { return false; } - return $this->doFetchColumn($this->result, $column_number); + return $this->doFetchColumn($this->result, $column); } /** @@ -262,11 +266,11 @@ public function rowCount() /** * {@inheritdoc} */ - public function setFetchMode($mode, $param1 = null, $param2 = null) + public function setFetchMode(int $mode, ...$args) { $this->fetch_style = $mode; - $this->fetch_argument = $param1; - $this->ctor_args = $param2; + $this->fetch_argument = $args[0] ?? null; + $this->ctor_args = $args[1] ?? null; return true; } diff --git a/src/PDO.php b/src/PDO.php index c372722..49dee12 100644 --- a/src/PDO.php +++ b/src/PDO.php @@ -16,23 +16,4 @@ public function __construct(string $dsn, ?string $user = null, ?string $password [PDOStatement::class, []] ); } - - /** - * {@inheritdoc} - */ - public function query($statement, $param1 = null, $param2 = null, $param3 = null) - { - if ($param1 === null || $param2 === null) { - /** @psalm-var PDOStatement|false */ - return parent::query($statement); - } - - if ($param3 === null) { - /** @psalm-var PDOStatement|false */ - return parent::query($statement, $param1, $param2); - } - - /** @psalm-var PDOStatement|false */ - return parent::query($statement, $param1, $param2, $param3); - } } diff --git a/src/PDOAdapter.php b/src/PDOAdapter.php index c02ab9b..186d24d 100644 --- a/src/PDOAdapter.php +++ b/src/PDOAdapter.php @@ -70,7 +70,7 @@ public function errorInfo() /** * {@inheritdoc} */ - public function exec($statement) + public function exec(string $statement) { return $this->pdo->exec($statement); } @@ -86,7 +86,7 @@ public function inTransaction() /** * {@inheritdoc} */ - public function lastInsertId($name = null) + public function lastInsertId(?string $name = null) { return $this->pdo->lastInsertId(); } @@ -94,37 +94,30 @@ public function lastInsertId($name = null) /** * {@inheritdoc} */ - public function prepare($statement) + public function prepare(string $statement, array $options = []) { /** @psalm-var PDOStatement|false */ - return $this->pdo->prepare($statement); + return $this->pdo->prepare($statement, $options); } /** * {@inheritdoc} */ - public function query($statement, $param1 = null, $param2 = null, $param3 = null) + public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs) { - if ($param1 === null || $param2 === null) { - /** @psalm-var PDOStatement|false */ - return $this->pdo->query($statement); - } - - if ($param3 === null) { - /** @psalm-var PDOStatement|false */ - return $this->pdo->query($statement, $param1, $param2); - } - - /** @psalm-var PDOStatement|false */ - return $this->pdo->query($statement, $param1, $param2, $param3); + /** + * @psalm-var PDOStatement|false + * @psalm-suppress PossiblyNullArgument + */ + return $this->pdo->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->pdo->quote($string, $parameter_type); + return $this->pdo->quote($string, $type); } /** diff --git a/src/PDOInterface.php b/src/PDOInterface.php index ecacf4b..12953ea 100644 --- a/src/PDOInterface.php +++ b/src/PDOInterface.php @@ -29,41 +29,41 @@ public function errorInfo(); * @param string $statement * @return int|false */ - public function exec($statement); + public function exec(string $statement); /** * Returns the ID of the last inserted row or sequence value. * - * @param string $name - * @return string + * @param string|null $name + * @return string|false */ - public function lastInsertId($name = null); + public function lastInsertId(?string $name = null); /** * Prepares a statement for execution and returns a statement object. * * @param string $statement + * @param array $options * @return PDOStatementInterface|false */ - public function prepare($statement); + public function prepare(string $statement, array $options = []); /** * Executes an SQL statement, returning a result set as a statement object. * - * @param string $statement - * @param mixed $param1 - * @param mixed $param2 - * @param mixed $param3 + * @param string $query + * @param int|null $fetchMode + * @param mixed ...$fetchModeArgs * @return PDOStatementInterface|false */ - public function query($statement, $param1 = null, $param2 = null, $param3 = null); + public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs); /** * Quotes a string for use in a query. * * @param string $string - * @param int $parameter_type - * @return string + * @param int $type + * @return string|false */ - public function quote($string, $parameter_type = \PDO::PARAM_STR); + public function quote(string $string, int $type = \PDO::PARAM_STR); } diff --git a/src/PDOStatement.php b/src/PDOStatement.php index 16dbe83..df5e703 100644 --- a/src/PDOStatement.php +++ b/src/PDOStatement.php @@ -10,60 +10,4 @@ class PDOStatement extends \PDOStatement implements PDOStatementInterface protected function __construct() { } - - /** - * {@inheritdoc} - */ - public function fetch($fetch_style = null, $cursor_orientation = null, $cursor_offset = null) - { - if ($fetch_style === null) { - return parent::fetch(); - } - - if ($cursor_orientation === null) { - return parent::fetch($fetch_style); - } - - if ($cursor_offset === null) { - return parent::fetch($fetch_style, $cursor_orientation); - } - - return parent::fetch($fetch_style, $cursor_orientation, $cursor_offset); - } - - /** - * {@inheritdoc} - */ - public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = null) - { - if ($fetch_style === null) { - return parent::fetchAll(); - } - - if ($fetch_argument === null) { - return parent::fetchAll($fetch_style); - } - - if ($ctor_args === null) { - return parent::fetchAll($fetch_style, $fetch_argument); - } - - return parent::fetchAll($fetch_style, $fetch_argument, $ctor_args); - } - - /** - * {@inheritdoc} - */ - public function setFetchMode($mode, $param1 = null, $param2 = null) - { - if ($param1 === null) { - return parent::setFetchMode($mode); - } - - if ($param2 === null) { - return parent::setFetchMode($mode, $param1); - } - - return parent::setFetchMode($mode, $param1, $param2); - } } diff --git a/src/PDOStatementInterface.php b/src/PDOStatementInterface.php index f3f7c44..e1570fe 100644 --- a/src/PDOStatementInterface.php +++ b/src/PDOStatementInterface.php @@ -12,12 +12,12 @@ interface PDOStatementInterface extends \Traversable /** * Binds a value to a parameter. * - * @param mixed $parameter + * @param string $param * @param mixed $value - * @param int $data_type + * @param int $type * @return bool */ - public function bindValue($parameter, $value, $data_type = \PDO::PARAM_STR); + public function bindValue(string $param, $value, int $type = \PDO::PARAM_STR); /** * Fetch the SQLSTATE associated with the last operation on the statement @@ -38,38 +38,37 @@ public function errorInfo(); /** * Executes a prepared statement. * - * @param array $input_parameters + * @param array|null $params * @return bool */ - public function execute($input_parameters = null); + public function execute(?array $params = null); /** * Fetches the next row from a result set. * - * @param int $fetch_style - * @param int $cursor_orientation - * @param int $cursor_offset + * @param int $mode + * @param int $cursorOrientation + * @param int $cursorOffset * @return mixed */ - 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); /** * Returns an array containing all of the result set rows. * - * @param int $fetch_style - * @param int|string $fetch_argument - * @param array $ctor_args + * @param int $mode + * @param mixed ...$args * @return array */ - public function fetchAll($fetch_style = null, $fetch_argument = null, $ctor_args = null); + public function fetchAll(int $mode = \PDO::ATTR_DEFAULT_FETCH_MODE, ...$args); /** * Returns a single column from the next row of a result set. * - * @param int $column_number + * @param int $column * @return string|false */ - public function fetchColumn($column_number = 0); + public function fetchColumn(int $column = 0); /** * Returns the number of rows affected by the last SQL statement. @@ -82,9 +81,8 @@ public function rowCount(); * Returns the number of rows affected by the last SQL statement. * * @param int $mode - * @param mixed $param1 - * @param mixed $param2 + * @param mixed ...$args * @return bool */ - public function setFetchMode($mode, $param1 = null, $param2 = null); + public function setFetchMode(int $mode, ...$args); } diff --git a/tests/AbstractPDOStatementTest.php b/tests/AbstractPDOStatementTest.php index 84fdea8..1cfaaa1 100644 --- a/tests/AbstractPDOStatementTest.php +++ b/tests/AbstractPDOStatementTest.php @@ -8,7 +8,7 @@ abstract class AbstractPDOStatementTest extends TestCase { protected $pdo; - public function setUp() + public function setUp(): void { $this->pdo = $this->preparePdo(); } @@ -83,11 +83,10 @@ public function providerFetchAll() /** * @dataProvider providerFetchAllThrowsRuntimeException - * - * @expectedException \RuntimeException */ public function testFetchAllThrowsException($fetch_args, $sql, $input_parameters) { + $this->expectException(\ValueError::class); $stmt = $this->pdo->prepare($sql); $stmt->execute($input_parameters); $stmt->fetchAll(...$fetch_args); @@ -106,11 +105,9 @@ public function testFetchAllWithoutExecute() $this->assertEquals([], $stmt->fetchAll()); } - /** - * @expectedException \RuntimeException - */ public function testFetchAllWithInvalidFetchMode() { + $this->expectException(\ValueError::class); $stmt = $this->pdo->prepare('SELECT 1'); $stmt->execute(); $stmt->fetchAll(-1); @@ -152,11 +149,9 @@ public function testFetchWithoutExecute() $this->assertFalse($stmt->fetch()); } - /** - * @expectedException \RuntimeException - */ public function testFetchWithInvalidFetchMode() { + $this->expectException(\ValueError::class); $stmt = $this->pdo->prepare('SELECT 1'); $stmt->execute(); $stmt->fetch(-1); @@ -202,7 +197,7 @@ public function testErrorInfo() $stmt = $this->pdo->prepare('SELECT 1'); $stmt->execute(); $error = $stmt->errorInfo(); - $this->assertInternalType('array', $error); + $this->assertIsArray($error); $this->assertCount(3, $error); } diff --git a/tests/AbstractPDOTest.php b/tests/AbstractPDOTest.php index f0b0e45..2d8dcea 100644 --- a/tests/AbstractPDOTest.php +++ b/tests/AbstractPDOTest.php @@ -9,7 +9,7 @@ abstract class AbstractPDOTest extends TestCase { protected $pdo; - public function setUp() + public function setUp(): void { $this->pdo = $this->preparePdo(); } @@ -20,11 +20,9 @@ public function testBeginTransaction() $this->assertTrue($this->pdo->rollback()); } - /** - * @expectedException \RuntimeException - */ public function testBeginTransactionThrowsRuntimeException() { + $this->expectException(\RuntimeException::class); $this->pdo->beginTransaction(); $this->pdo->beginTransaction(); } @@ -35,11 +33,9 @@ public function testCommit() $this->assertTrue($this->pdo->commit()); } - /** - * @expectedException \RuntimeException - */ public function testCommitThrowsRuntimeException() { + $this->expectException(\RuntimeException::class); $this->pdo->commit(); } @@ -51,7 +47,7 @@ public function testErrorCode() public function testErrorInfo() { $error = $this->pdo->errorInfo(); - $this->assertInternalType('array', $error); + $this->assertIsArray($error); $this->assertCount(3, $error); } @@ -112,11 +108,9 @@ public function testRollback() $this->assertTrue($this->pdo->rollback()); } - /** - * @expectedException \RuntimeException - */ public function testRollbackThrowsRuntimeException() { + $this->expectException(\RuntimeException::class); $this->pdo->rollback(); $this->pdo->rollback(); } diff --git a/tests/ListenableConnectionTest.php b/tests/ListenableConnectionTest.php index 4b85cd6..8f9ee46 100644 --- a/tests/ListenableConnectionTest.php +++ b/tests/ListenableConnectionTest.php @@ -20,7 +20,7 @@ class ListenableConnectionTest extends TestCase private $pdo; - public function setUp() + public function setUp(): void { $this->delegate = $this->createMock(PDOInterface::class); $this->listener = $this->createMock(PDOListenerInterface::class); diff --git a/tests/ListenableStatementTest.php b/tests/ListenableStatementTest.php index 73af44b..3452333 100644 --- a/tests/ListenableStatementTest.php +++ b/tests/ListenableStatementTest.php @@ -31,7 +31,7 @@ public function testDelegate() $delegate ->expects($this->once()) ->method('fetch') - ->with(\PDO::FETCH_CLASS, stdClass::class, []) + ->with(\PDO::FETCH_CLASS) ->willReturn((object) ['foo' => 123]); $delegate ->expects($this->once()) @@ -58,7 +58,7 @@ public function testDelegate() $this->assertSame($delegate, $stmt->getIterator()); $this->assertSame(123, $stmt->errorCode()); $this->assertEquals(['HY000', 1, 'error'], $stmt->errorInfo()); - $this->assertEquals((object) ['foo' => 123], $stmt->fetch(\PDO::FETCH_CLASS, stdClass::class, [])); + $this->assertEquals((object) ['foo' => 123], $stmt->fetch(\PDO::FETCH_CLASS)); $this->assertEquals([(object) ['foo' => 123]], $stmt->fetchAll(\PDO::FETCH_CLASS, stdClass::class, [])); $this->assertSame(123, $stmt->fetchColumn(0)); $this->assertSame(1, $stmt->rowCount()); @@ -98,9 +98,6 @@ public function testExecute() $this->assertTrue($stmt->execute([456])); } - /** - * @expectedException \RuntimeException - */ public function testExecuteWithException() { $queryString = 'SELECT ? AS foo, ? AS bar'; @@ -130,6 +127,7 @@ public function testExecuteWithException() $stmt = new ListenableStatement($pdo, [$listener], $delegate, $queryString); + $this->expectException(\RuntimeException::class); $this->assertTrue($stmt->bindValue(1, 123, \PDO::PARAM_INT)); $stmt->execute([456]); diff --git a/tests/MasterSlaveConnectionTest.php b/tests/MasterSlaveConnectionTest.php index e42b846..ec4a03e 100644 --- a/tests/MasterSlaveConnectionTest.php +++ b/tests/MasterSlaveConnectionTest.php @@ -18,7 +18,7 @@ class MasterSlaveConnectionTest extends TestCase private $pdo; - public function setUp() + public function setUp(): void { $this->masterPdo = $this->createMock(PDOInterface::class); diff --git a/tests/MysqliAdapterTest.php b/tests/MysqliAdapterTest.php index 3e762fc..12db832 100644 --- a/tests/MysqliAdapterTest.php +++ b/tests/MysqliAdapterTest.php @@ -15,7 +15,7 @@ class MysqliAdapterTest extends AbstractPDOTest private static $previous_report_mode; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { self::$driver = $driver = new \mysqli_driver(); self::$previous_report_mode = $driver->report_mode; @@ -23,7 +23,7 @@ public static function setUpBeforeClass() $driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX; } - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { self::$driver->report_mode = self::$previous_report_mode; diff --git a/tests/MysqliConnectorTest.php b/tests/MysqliConnectorTest.php index 878be00..13ca3f8 100644 --- a/tests/MysqliConnectorTest.php +++ b/tests/MysqliConnectorTest.php @@ -16,7 +16,7 @@ class MysqliConnectorTest extends AbstractConnectorTest private static $previous_report_mode; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { self::$driver = $driver = new \mysqli_driver(); self::$previous_report_mode = $driver->report_mode; @@ -24,7 +24,7 @@ public static function setUpBeforeClass() $driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX; } - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { self::$driver->report_mode = self::$previous_report_mode; diff --git a/tests/MysqliStmtAdapterTest.php b/tests/MysqliStmtAdapterTest.php index 1ff390a..d7cf36c 100644 --- a/tests/MysqliStmtAdapterTest.php +++ b/tests/MysqliStmtAdapterTest.php @@ -16,7 +16,7 @@ class MysqliStmtAdapterTest extends AbstractPDOStatementTest private static $previous_report_mode; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { self::$driver = $driver = new \mysqli_driver(); self::$previous_report_mode = $driver->report_mode; @@ -24,7 +24,7 @@ public static function setUpBeforeClass() $driver->report_mode = MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX; } - public static function tearDownAfterClass() + public static function tearDownAfterClass(): void { self::$driver->report_mode = self::$previous_report_mode; diff --git a/tests/NestedTransactionTest.php b/tests/NestedTransactionTest.php index b4d2ee2..07fc145 100644 --- a/tests/NestedTransactionTest.php +++ b/tests/NestedTransactionTest.php @@ -5,6 +5,7 @@ use Emonkak\Database\NestedTransaction; use Emonkak\Database\PDOInterface; use Emonkak\Database\SavepointInterface; +use PHPUnit\Framework\MockObject\Rule\InvokedAtIndex; use PHPUnit\Framework\TestCase; /** @@ -19,7 +20,12 @@ class NestedTransactionTest extends TestCase private $nestedTransaction; - public function setUp() + public static function at(int $index): InvokedAtIndex + { + return new InvokedAtIndex($index); + } + + public function setUp(): void { $this->pdo = $this->createMock(PDOInterface::class); $this->savepoint = $this->createMock(SavepointInterface::class); diff --git a/tests/PDOStatementIteratorTest.php b/tests/PDOStatementIteratorTest.php index 4eca453..9111a75 100644 --- a/tests/PDOStatementIteratorTest.php +++ b/tests/PDOStatementIteratorTest.php @@ -4,6 +4,7 @@ use Emonkak\Database\PDOStatementInterface; use Emonkak\Database\PDOStatementIterator; +use PHPUnit\Framework\MockObject\Rule\InvokedAtIndex; use PHPUnit\Framework\TestCase; /** @@ -15,12 +16,17 @@ class PDOStatementIteratorTest extends TestCase private $iterator; - public function setUp() + public function setUp(): void { $this->stmt = $this->createMock(PDOStatementInterface::class); $this->iterator = new PDOStatementIterator($this->stmt); } + public static function at(int $index): InvokedAtIndex + { + return new InvokedAtIndex($index); + } + public function test() { $this->stmt