Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/code-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
strategy:
matrix:
php:
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor
/.phpunit.cache/
/vendor/
/composer.lock
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion .runConfigurations/All tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<env name="INTEGRATION_USERNAME" value="root" />
</envs>
</CommandLine>
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" options="--verbose" />
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" />
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion .runConfigurations/All unit tests.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All unit tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" directory="$PROJECT_DIR$/tests" scope="XML" options="--verbose" />
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" directory="$PROJECT_DIR$/tests" scope="XML" />
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion .runConfigurations/Integration tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<env name="INTEGRATION_USERNAME" value="root" />
</envs>
</CommandLine>
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" options="--verbose --group=integration" />
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" options="--group=integration" />
<method v="2" />
</configuration>
</component>
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Add PHP v8 type declarations.
### Removed
- **BC break**: Removed support for PHP versions <= v8.0 as they are no longer
[actively supported](https://php.net/supported-versions.php) by the PHP project.

## [2.2.0] - 2024-10-22
### Added
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^10",
"symplify/easy-coding-standard": "^12"
},
"autoload": {
Expand Down
15 changes: 15 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,19 @@
[
'closure_fn_spacing' => 'none',
],
)

/*
* Rule from PER Coding Style 2.6:
* "If the list is split across multiple lines, then the last item MUST have a trailing comma."
*/
->withConfiguredRule(
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::class,
[
'elements' => [
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS,
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS,
\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS,
],
],
);
8 changes: 4 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
cacheDirectory=".phpunit.cache"
colors="true"
>
<php>
Expand All @@ -20,9 +20,9 @@
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function ping(): bool
{
try {
return $this->query('SELECT "1"')->fetchColumn() === '1';
} catch (\Exception $e) {
} catch (\Exception) {
return false;
}
}
Expand All @@ -177,7 +177,7 @@ public function ping(): bool
* Get the last inserted id. If the tablename is provided the id returned is
* the last insert id will be for that table.
*/
public function lastInsertId(string $tablename = null): string
public function lastInsertId(?string $tablename = null): string
{
// the lastInsertId is cached from the last insert, so no point in detected disconnection
return $this->getConnection()->lastInsertId($tablename);
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function create(Config $config): \PDO
$config->getDsn(),
$config->getUsername(),
$config->getPassword(),
$config->getOptions()
$config->getOptions(),
);
}
}
71 changes: 32 additions & 39 deletions src/Adapter/QuoteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@

class QuoteHandler
{
private bool $autoQuoteIdentifiers;

private \Closure $quoteFn;

/**
* @param \Closure{
* value: mixed,
* }:string $quoteFn
*/
public function __construct(\Closure $quoteFn, bool $autoQuoteIdentifiers = true)
{
$this->quoteFn = $quoteFn;
$this->autoQuoteIdentifiers = $autoQuoteIdentifiers;
public function __construct(
private readonly \Closure $quoteFn,
private readonly bool $autoQuoteIdentifiers = true,
) {
}

/**
* Quote a database value.
*
* @param mixed $value
* Quote a value for the database
*/
public function value($value): string
public function value(mixed $value): string
{
switch (true) {
case is_object($value):
Expand All @@ -43,7 +37,7 @@ public function value($value): string
case $value === null:
return 'NULL';
case is_array($value):
$value = array_map(function ($value) {
$value = array_map(function ($value): string {
if (is_array($value)) {
$value = 'Array';
}
Expand All @@ -56,52 +50,51 @@ public function value($value): string
}

/**
* Quote into the value for the database.
*
* @param mixed $value
* Quote a value to replace the `?` placeholder
*/
public function into(string $text, $value): string
public function into(string $text, mixed $value): string
{
return str_replace('?', $this->value($value), $text);
}

/**
* Quote a column identifier and alias.
*
* @param string|string[] $ident
* Quote a column identifier and alias
*/
public function columnAs($ident, string $alias, bool $auto = false): string
{
public function columnAs(
string|array|object $ident,
string $alias,
bool $auto = false,
): string {
return $this->quoteIdentifierAs($ident, $alias, $auto);
}

/**
* Quote a table identifier and alias.
*
* @param string|string[] $ident
* Quote a table identifier and alias
*/
public function tableAs($ident, string $alias, bool $auto = false): string
{
public function tableAs(
string|array|object $ident,
string $alias,
bool $auto = false,
): string {
return $this->quoteIdentifierAs($ident, $alias, $auto);
}

/**
* Quotes an identifier
*
* @param string|string[] $ident
* Quote an identifier
*/
public function identifier($ident, bool $auto = false): string
{
public function identifier(
string|array|object $ident,
bool $auto = false,
): string {
return $this->quoteIdentifierAs($ident, null, $auto);
}

/**
* Quote an identifier and an optional alias.
*
* @param string|array|object $ident
*/
private function quoteIdentifierAs($ident, string $alias = null, bool $auto = false, string $as = ' AS '): string
{
private function quoteIdentifierAs(
string|array|object $ident,
?string $alias = null,
bool $auto = false,
string $as = ' AS ',
): string {
if (is_object($ident) && method_exists($ident, 'assemble')) {
$quoted = '(' . $ident->assemble() . ')';
} elseif (is_object($ident)) {
Expand Down
2 changes: 1 addition & 1 deletion src/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function ping(): bool;
* Get the last inserted id. If the tablename is provided the id returned is
* the last insert id will be for that table.
*/
public function lastInsertId(string $tablename = null): string;
public function lastInsertId(?string $tablename = null): string;

public function prepare(string $statement): \PDOStatement;

Expand Down
16 changes: 6 additions & 10 deletions src/Exception/InvalidQueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@

class InvalidQueryException extends RuntimeException implements Exception
{
private string $query;

private array $bind;

public static function isInvalidSyntax(\PDOException $exception): bool
{
return stripos($exception->getMessage(), 'You have an error in your SQL syntax') !== false;
}

public function __construct(string $query, array $bind = [], \PDOException $previous = null)
{
$this->query = $query;
$this->bind = $bind;

public function __construct(
private readonly string $query,
private readonly array $bind = [],
?\PDOException $previous = null,
) {
$message = 'You have an error in your SQL syntax.';
$code = 0;
if ($previous !== null) {
$message = $previous->getMessage();
$code = (int)$previous->getCode();
}
$message .= ' SQL: ' . $query . ' Bind: ' . var_export($bind, true);
$message .= ' SQL: ' . $this->query . ' Bind: ' . var_export($this->bind, true);

parent::__construct($message, $code, $previous);
}
Expand Down
11 changes: 6 additions & 5 deletions src/Exception/UnknownDatabaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class UnknownDatabaseException extends RuntimeException implements Exception
*/
public const ER_BAD_DB_ERROR_2 = 42000;

private string $name;

public static function createFromUnknownDatabase(string $database, \PDOException $exception): self
{
return new static($database, "Unknown database '{$database}'.", self::ER_BAD_DB_ERROR_1, $exception);
Expand All @@ -40,9 +38,12 @@ public static function isUnknownDatabase(\PDOException $exception): bool
);
}

public function __construct(string $database, string $message, int $code = 0, \PDOException $previous = null)
{
$this->name = $database;
public function __construct(
private readonly string $name,
string $message,
int $code = 0,
?\PDOException $previous = null,
) {
parent::__construct($message, $code, $previous);
}

Expand Down
8 changes: 3 additions & 5 deletions src/SqlFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
*/
class SqlFragment
{
private string $value;

public function __construct(string $value)
{
$this->value = $value;
public function __construct(
private readonly string $value,
) {
}

public function __toString(): string
Expand Down
Loading