diff --git a/CHANGELOG.md b/CHANGELOG.md index dcbc294f5..c7e82cd77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ - Enh #878: Realize `ColumnBuilder` class (@Tigrov) - Enh #881: Refactor `ColumnSchemaInterface` and `AbstractColumnSchema` (@Tigrov) - End #882: Move `ArrayColumnSchema` and `StructuredColumnSchema` classes from `db-pgsql` package (@Tigrov) +- Enh #885: Refactor `AbstractDsn` class (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/src/Connection/AbstractDsn.php b/src/Connection/AbstractDsn.php index f8895c097..8689a04f2 100644 --- a/src/Connection/AbstractDsn.php +++ b/src/Connection/AbstractDsn.php @@ -6,8 +6,6 @@ use Stringable; -use function implode; - /** * It's typically used to parse a DSN string, which is a string that has all the necessary information to connect * to a database, such as the database driver, hostname, database name, port, and options. @@ -17,37 +15,37 @@ abstract class AbstractDsn implements DsnInterface, Stringable { /** - * @psalm-param string[] $options + * @param string $driver The database driver name. + * @param string $host The database host name or IP address. + * @param string|null $databaseName The database name to connect to. + * @param string|null $port The database port. Null if isn't set. + * @param string[] $options The database connection options. Default value to an empty array. + * + * @psalm-param array $options */ public function __construct( - private string $driver, - private string $host, - private string|null $databaseName = null, - private string|null $port = null, - private array $options = [] + private readonly string $driver, + private readonly string $host, + private readonly string|null $databaseName = null, + private readonly string|null $port = null, + private readonly array $options = [] ) { } public function asString(): string { - $dsn = "$this->driver:" . "host=$this->host"; + $dsn = "$this->driver:host=$this->host"; if ($this->databaseName !== null && $this->databaseName !== '') { - $dsn .= ';' . "dbname=$this->databaseName"; + $dsn .= ";dbname=$this->databaseName"; } if ($this->port !== null) { - $dsn .= ';' . "port=$this->port"; + $dsn .= ";port=$this->port"; } - $parts = []; - foreach ($this->options as $key => $value) { - $parts[] = "$key=$value"; - } - - if (!empty($parts)) { - $dsn .= ';' . implode(';', $parts); + $dsn .= ";$key=$value"; } return $dsn; @@ -86,7 +84,9 @@ public function getHost(): string } /** - * @return array The database connection options. Default value to an empty array. + * @return string[] The database connection options. Default value to an empty array. + * + * @psalm-return array */ public function getOptions(): array {