From 50af08e54fb59744aec3fd481e995cab92831dd2 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 1 Aug 2024 23:17:11 +0000 Subject: [PATCH] Use compact use statements where possible and removed RuntimeException extensions. --- VERSION | 2 +- src/Bencode/Bencode.php | 4 +- src/Data/BeginTransactionFailedException.php | 11 ----- src/Data/CommitFailedException.php | 11 ----- src/Data/ConnectionFailedException.php | 11 ----- src/Data/DataException.php | 13 ------ src/Data/DbTools.php | 13 +++--- src/Data/IDbTransactions.php | 12 +++--- src/Data/MariaDB/MariaDBBackend.php | 9 ++-- src/Data/MariaDB/MariaDBConnection.php | 45 ++++++++------------ src/Data/MariaDB/MariaDBConnectionInfo.php | 8 +--- src/Data/MariaDB/MariaDBParameter.php | 5 +-- src/Data/MariaDB/MariaDBResult.php | 8 ++-- src/Data/MariaDB/MariaDBResultLib.php | 8 ++-- src/Data/MariaDB/MariaDBResultNative.php | 6 +-- src/Data/MariaDB/MariaDBStatement.php | 10 ++--- src/Data/Migration/DbMigrationManager.php | 4 +- src/Data/NullDb/NullDbBackend.php | 6 +-- src/Data/NullDb/NullDbConnection.php | 6 +-- src/Data/NullDb/NullDbResult.php | 5 +-- src/Data/NullDb/NullDbStatement.php | 6 +-- src/Data/QueryExecuteException.php | 11 ----- src/Data/ReleaseSavePointFailedException.php | 11 ----- src/Data/RollbackFailedException.php | 11 ----- src/Data/SQLite/SQLiteBackend.php | 6 +-- src/Data/SQLite/SQLiteConnection.php | 37 ++++++---------- src/Data/SQLite/SQLiteResult.php | 8 ++-- src/Data/SQLite/SQLiteStatement.php | 16 +++---- src/Data/SavePointFailedException.php | 11 ----- src/Data/TransactionException.php | 11 ----- src/Http/Content/BencodedContent.php | 6 +-- src/Http/Content/JsonContent.php | 3 +- src/Http/Content/StreamContent.php | 3 +- src/Http/ErrorHandling/IErrorHandler.php | 3 +- src/Http/HttpMessage.php | 7 +-- src/Http/HttpMessageBuilder.php | 4 +- src/Http/HttpRequest.php | 5 +-- src/Http/HttpResponseBuilder.php | 4 +- src/Http/HttpUploadedFile.php | 6 +-- src/Http/Routing/RouterTrait.php | 5 ++- src/Http/Routing/ScopedRouter.php | 9 +++- src/IO/FileStream.php | 6 ++- src/IO/IOException.php | 13 ------ src/IO/NetworkStream.php | 10 ++--- src/IO/ProcessStream.php | 8 ++-- src/IO/Stream.php | 3 +- tests/CacheToolsTest.php | 5 +-- tests/ColourTest.php | 8 +--- tests/DbToolsTest.php | 8 ++-- tests/IPAddressRangeTest.php | 8 ++-- tests/IndexTest.php | 5 +-- 51 files changed, 138 insertions(+), 316 deletions(-) delete mode 100644 src/Data/BeginTransactionFailedException.php delete mode 100644 src/Data/CommitFailedException.php delete mode 100644 src/Data/ConnectionFailedException.php delete mode 100644 src/Data/DataException.php delete mode 100644 src/Data/QueryExecuteException.php delete mode 100644 src/Data/ReleaseSavePointFailedException.php delete mode 100644 src/Data/RollbackFailedException.php delete mode 100644 src/Data/SavePointFailedException.php delete mode 100644 src/Data/TransactionException.php delete mode 100644 src/IO/IOException.php diff --git a/VERSION b/VERSION index 5babcac..15c0d0a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2408.12215 +0.2408.12316 diff --git a/src/Bencode/Bencode.php b/src/Bencode/Bencode.php index d2d2e58..b2e9812 100644 --- a/src/Bencode/Bencode.php +++ b/src/Bencode/Bencode.php @@ -7,9 +7,7 @@ namespace Index\Bencode; use InvalidArgumentException; use RuntimeException; -use Index\IO\GenericStream; -use Index\IO\Stream; -use Index\IO\TempFileStream; +use Index\IO\{GenericStream,Stream,TempFileStream}; /** * Provides a Bencode serialiser. diff --git a/src/Data/BeginTransactionFailedException.php b/src/Data/BeginTransactionFailedException.php deleted file mode 100644 index 7caa967..0000000 --- a/src/Data/BeginTransactionFailedException.php +++ /dev/null @@ -1,11 +0,0 @@ -isAvailable()) - throw new DataException('Requested database backend is not available, likely due to missing dependencies.'); + throw new RuntimeException('Requested database backend is not available, likely due to missing dependencies.'); $backends[$name] = $backend; } @@ -77,7 +78,7 @@ final class DbTools { * * @param string $dsn URL to create database connection from. * @throws InvalidArgumentException if $dsn is not a valid URL. - * @throws DataException if no database connection can be made using the URL. + * @throws RuntimeException if no database connection can be made using the URL. * @return IDbBackend Database backend instance. */ public static function backend(string $dsn): IDbBackend { @@ -91,7 +92,7 @@ final class DbTools { * * @param string $dsn URL to create database connection from. * @throws InvalidArgumentException if $dsn is not a valid URL. - * @throws DataException if no database connection can be made using the URL. + * @throws RuntimeException if no database connection can be made using the URL. * @return IDbConnectionInfo Database connection info. */ public static function parse(string $dsn): IDbConnectionInfo { @@ -106,7 +107,7 @@ final class DbTools { * * @param string $dsn URL to create database connection from. * @throws InvalidArgumentException if $dsn is not a valid URL. - * @throws DataException if no database connection can be made using the URL. + * @throws RuntimeException if no database connection can be made using the URL. * @return IDbConnection An active database connection. */ public static function create(string $dsn): IDbConnection { diff --git a/src/Data/IDbTransactions.php b/src/Data/IDbTransactions.php index 9f114ed..59ee66a 100644 --- a/src/Data/IDbTransactions.php +++ b/src/Data/IDbTransactions.php @@ -1,7 +1,7 @@ getMessage(), $ex->getCode(), $ex); + throw new RuntimeException($ex->getMessage(), $ex->getCode(), $ex); } } @@ -194,22 +185,22 @@ class MariaDBConnection implements IDbConnection, IDbTransactions { * @param string $userName New user name. * @param string $password New password. * @param string|null $database New default database. - * @throws DataException If the user switch action failed. + * @throws RuntimeException If the user switch action failed. */ public function switchUser(string $userName, string $password, string|null $database = null): void { if(!$this->connection->change_user($userName, $password, $database === null ? null : $database)) - throw new DataException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); } /** * Switches the default database for this connection. * * @param string $database New default database. - * @throws DataException If the database switch failed. + * @throws RuntimeException If the database switch failed. */ public function switchDatabase(string $database): void { if(!$this->connection->select_db($database)) - throw new DataException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); } /** @@ -314,27 +305,27 @@ class MariaDBConnection implements IDbConnection, IDbTransactions { public function beginTransaction(): void { if(!$this->connection->begin_transaction()) - throw new BeginTransactionFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function commit(): void { if(!$this->connection->commit()) - throw new CommitFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function rollback(?string $name = null): void { if(!$this->connection->rollback(0, $name)) - throw new RollbackFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function savePoint(string $name): void { if(!$this->connection->savepoint($name)) - throw new SavePointFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function releaseSavePoint(string $name): void { if(!$this->connection->release_savepoint($name)) - throw new ReleaseSavePointFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } /** @@ -363,10 +354,10 @@ class MariaDBConnection implements IDbConnection, IDbTransactions { try { $statement = $this->connection->prepare($query); } catch(mysqli_sql_exception $ex) { - throw new QueryExecuteException($ex->getMessage(), $ex->getCode(), $ex); + throw new RuntimeException($ex->getMessage(), $ex->getCode(), $ex); } if($statement === false) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); return new MariaDBStatement($statement); } @@ -377,10 +368,10 @@ class MariaDBConnection implements IDbConnection, IDbTransactions { try { $result = $this->connection->query($query, MYSQLI_STORE_RESULT); } catch(mysqli_sql_exception $ex) { - throw new QueryExecuteException($ex->getMessage(), $ex->getCode(), $ex); + throw new RuntimeException($ex->getMessage(), $ex->getCode(), $ex); } if($result === false) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); // Yes, this always uses Native, for some reason the stupid limitation in libmysql only applies to preparing return new MariaDBResultNative($result); } @@ -388,9 +379,9 @@ class MariaDBConnection implements IDbConnection, IDbTransactions { public function execute(string $query): int|string { try { if(!$this->connection->real_query($query)) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); } catch(mysqli_sql_exception $ex) { - throw new QueryExecuteException($ex->getMessage(), $ex->getCode(), $ex); + throw new RuntimeException($ex->getMessage(), $ex->getCode(), $ex); } return $this->connection->affected_rows; } diff --git a/src/Data/MariaDB/MariaDBConnectionInfo.php b/src/Data/MariaDB/MariaDBConnectionInfo.php index 169e9ec..75edd6c 100644 --- a/src/Data/MariaDB/MariaDBConnectionInfo.php +++ b/src/Data/MariaDB/MariaDBConnectionInfo.php @@ -1,17 +1,13 @@ store_result()) - throw new QueryExecuteException($statement->error, $statement->errno); + throw new RuntimeException($statement->error, $statement->errno); $metadata = $statement->result_metadata(); while($field = $metadata->fetch_field()) @@ -33,7 +33,7 @@ class MariaDBResultLib extends MariaDBResult { public function next(): bool { $result = $this->result->fetch(); if($result === false) - throw new QueryExecuteException($this->result->error, $this->result->errno); + throw new RuntimeException($this->result->error, $this->result->errno); if($result === null) return false; diff --git a/src/Data/MariaDB/MariaDBResultNative.php b/src/Data/MariaDB/MariaDBResultNative.php index 166b087..86cc51d 100644 --- a/src/Data/MariaDB/MariaDBResultNative.php +++ b/src/Data/MariaDB/MariaDBResultNative.php @@ -1,13 +1,13 @@ get_result(); if($_result === false) - throw new QueryExecuteException($result->error, $result->errno); + throw new RuntimeException($result->error, $result->errno); $result = $_result; } diff --git a/src/Data/MariaDB/MariaDBStatement.php b/src/Data/MariaDB/MariaDBStatement.php index 1bf981d..5fd5881 100644 --- a/src/Data/MariaDB/MariaDBStatement.php +++ b/src/Data/MariaDB/MariaDBStatement.php @@ -1,16 +1,14 @@ statement, 'bind_param'], $args); if(!$this->statement->execute()) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); } public function reset(): void { $this->params = []; if(!$this->statement->reset()) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); } public function close(): void { diff --git a/src/Data/Migration/DbMigrationManager.php b/src/Data/Migration/DbMigrationManager.php index eb6db4d..ead87d4 100644 --- a/src/Data/Migration/DbMigrationManager.php +++ b/src/Data/Migration/DbMigrationManager.php @@ -10,9 +10,7 @@ use InvalidArgumentException; use DateTimeImmutable; use DateTimeInterface; use Index\XDateTime; -use Index\Data\IDbConnection; -use Index\Data\IDbStatement; -use Index\Data\DbType; +use Index\Data\{IDbConnection,IDbStatement,DbType}; use Index\Data\SQLite\SQLiteConnection; /** diff --git a/src/Data/NullDb/NullDbBackend.php b/src/Data/NullDb/NullDbBackend.php index 596d1fd..eaf0b49 100644 --- a/src/Data/NullDb/NullDbBackend.php +++ b/src/Data/NullDb/NullDbBackend.php @@ -1,13 +1,11 @@ connection->openBlob($table, $column, $rowId); if(!is_resource($handle)) - throw new DataException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); return new GenericStream($this->connection->openBlob($table, $column, $rowId)); } @@ -497,20 +486,20 @@ class SQLiteConnection implements IDbConnection, IDbTransactions { public function prepare(string $query): IDbStatement { $statement = $this->connection->prepare($query); if($statement === false) - throw new QueryExecuteException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); return new SQLiteStatement($this, $statement); } public function query(string $query): IDbResult { $result = $this->connection->query($query); if($result === false) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); return new SQLiteResult($result); } public function execute(string $query): int|string { if(!$this->connection->exec($query)) - throw new QueryExecuteException($this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException($this->getLastErrorString(), $this->getLastErrorCode()); return $this->connection->changes(); } @@ -529,31 +518,31 @@ class SQLiteConnection implements IDbConnection, IDbTransactions { public function beginTransaction(): void { if(!$this->connection->exec('BEGIN;')) - throw new BeginTransactionFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function commit(): void { if(!$this->connection->exec('COMMIT;')) - throw new CommitFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); if(!$this->autoCommit) $this->beginTransaction(); } public function rollback(?string $name = null): void { if(!$this->connection->exec(empty($name) ? 'ROLLBACK;' : "ROLLBACK TO {$name};")) - throw new CommitFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); if(!$this->autoCommit) $this->beginTransaction(); } public function savePoint(string $name): void { if(!$this->connection->exec("SAVEPOINT {$name};")) - throw new SavePointFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function releaseSavePoint(string $name): void { if(!$this->connection->exec("RELEASE SAVEPOINT {$name};")) - throw new ReleaseSavePointFailedException((string)$this->getLastErrorString(), $this->getLastErrorCode()); + throw new RuntimeException((string)$this->getLastErrorString(), $this->getLastErrorCode()); } public function close(): void { diff --git a/src/Data/SQLite/SQLiteResult.php b/src/Data/SQLite/SQLiteResult.php index 3fba230..6d2da0f 100644 --- a/src/Data/SQLite/SQLiteResult.php +++ b/src/Data/SQLite/SQLiteResult.php @@ -1,16 +1,14 @@ statement->bindValue($ordinal, $value, $type)) - throw new QueryExecuteException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); + throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); } public function getResult(): IDbResult { @@ -84,16 +80,16 @@ class SQLiteStatement implements IDbStatement { public function execute(): void { $result = $this->statement->execute(); if($result === false) - throw new QueryExecuteException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); + throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); $this->result = $result; } public function reset(): void { $this->result = null; if(!$this->statement->clear()) - throw new QueryExecuteException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); + throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); if(!$this->statement->reset()) - throw new QueryExecuteException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); + throw new RuntimeException((string)$this->connection->getLastErrorString(), $this->connection->getLastErrorCode()); } public function close(): void {} diff --git a/src/Data/SavePointFailedException.php b/src/Data/SavePointFailedException.php deleted file mode 100644 index 5b6b630..0000000 --- a/src/Data/SavePointFailedException.php +++ /dev/null @@ -1,11 +0,0 @@ -add('GET', $path, $handler); diff --git a/src/Http/Routing/ScopedRouter.php b/src/Http/Routing/ScopedRouter.php index 068e003..96aa166 100644 --- a/src/Http/Routing/ScopedRouter.php +++ b/src/Http/Routing/ScopedRouter.php @@ -1,16 +1,23 @@ getParentRouter(); diff --git a/src/IO/FileStream.php b/src/IO/FileStream.php index 2d58729..a543684 100644 --- a/src/IO/FileStream.php +++ b/src/IO/FileStream.php @@ -6,6 +6,7 @@ namespace Index\IO; use ErrorException; +use RuntimeException; /** * Represents a Stream representing a file. @@ -115,6 +116,7 @@ class FileStream extends GenericStream { * @param string $path Path to the file. * @param string $mode Stream mode to use to open the file. * @param int $lock Locking method to use. + * @throws RuntimeException If the file could not be opened. */ public function __construct(string $path, string $mode, int $lock) { $this->lock = $lock; @@ -122,11 +124,11 @@ class FileStream extends GenericStream { try { $stream = fopen($path, $mode); } catch(ErrorException $ex) { - throw new IOException('An error occurred while trying to open a file.', $ex->getCode(), $ex); + throw new RuntimeException('An error occurred while trying to open a file.', $ex->getCode(), $ex); } if($stream === false) - throw new IOException('An unhandled error occurred while trying to open a file.'); + throw new RuntimeException('An unhandled error occurred while trying to open a file.'); parent::__construct($stream); diff --git a/src/IO/IOException.php b/src/IO/IOException.php deleted file mode 100644 index 812e9fb..0000000 --- a/src/IO/IOException.php +++ /dev/null @@ -1,13 +0,0 @@ -getCode(), $ex); + throw new RuntimeException('An error occurred while trying to connect.', $ex->getCode(), $ex); } if($stream === false) - throw new IOException('An unhandled error occurred while trying to connect.'); + throw new RuntimeException('An unhandled error occurred while trying to connect.'); parent::__construct($stream); } diff --git a/src/IO/ProcessStream.php b/src/IO/ProcessStream.php index c3595ff..f84f67b 100644 --- a/src/IO/ProcessStream.php +++ b/src/IO/ProcessStream.php @@ -5,6 +5,8 @@ namespace Index\IO; +use RuntimeException; + /** * Represents a stream to a running sub-process. */ @@ -16,7 +18,7 @@ class ProcessStream extends Stream { /** * @param string $command Command to run for this stream. * @param string $mode File mode to use. - * @throws IOException If we were unable to spawn the process. + * @throws RuntimeException If we were unable to spawn the process. */ public function __construct(string $command, string $mode) { $this->handle = popen($command, $mode); @@ -24,7 +26,7 @@ class ProcessStream extends Stream { $this->canWrite = strpos($mode, 'w') !== false; if(!is_resource($this->handle)) - throw new IOException('Failed to create process.'); + throw new RuntimeException('Failed to create process.'); } public function getPosition(): int { @@ -80,7 +82,7 @@ class ProcessStream extends Stream { } public function seek(int $offset, int $origin = self::START): bool { - throw new IOException('Cannot seek ProcessStream.'); + throw new RuntimeException('Cannot seek ProcessStream.'); } public function write(string $buffer, int $length = -1): void { diff --git a/src/IO/Stream.php b/src/IO/Stream.php index 942dc85..f39591a 100644 --- a/src/IO/Stream.php +++ b/src/IO/Stream.php @@ -5,6 +5,7 @@ namespace Index\IO; +use RuntimeException; use Stringable; use Index\ICloseable; @@ -135,7 +136,7 @@ abstract class Stream implements Stringable, ICloseable { * * @param int $offset Offset to apply to the cursor position. * @param int $origin Point from which to apply the offset. - * @throws IOException If the stream is not seekable. + * @throws RuntimeException If the stream is not seekable. * @return bool true if the cursor offset was applied successfully. */ abstract public function seek(int $offset, int $origin = self::START): bool; diff --git a/tests/CacheToolsTest.php b/tests/CacheToolsTest.php index cf0c4b5..de6a34b 100644 --- a/tests/CacheToolsTest.php +++ b/tests/CacheToolsTest.php @@ -1,13 +1,12 @@