From 5ec52d672feae4f894532e3ffd6b8b3a3d7658fb Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sun, 19 Oct 2025 21:09:57 +1000 Subject: [PATCH 1/9] Update code to match the project style guide This change is a result of running phpcbf over the code, and performing some manual changes to ensure that the code passes the phpcs configuration. --- src/ConfigProvider.php | 10 +++++----- src/Container/AdapterFactory.php | 1 - src/Container/MetadataInterfaceFactory.php | 2 +- src/Container/PdoDriverFactory.php | 2 +- src/Container/PdoStatementFactory.php | 1 - src/Container/PlatformInterfaceFactory.php | 2 +- src/Driver/Pdo/Connection.php | 7 ++++--- src/Metadata/Source/SqliteMetadata.php | 2 +- src/Platform/Sqlite.php | 13 ++++++------- src/Sql/Platform/Ddl/AlterTableDecorator.php | 4 +++- src/Sql/Platform/Ddl/CreateTableDecorator.php | 5 ++--- .../integration/Container/TestAsset/SetupTrait.php | 4 ++-- .../Driver/Pdo/ConnectionIntegrationTest.php | 13 +------------ test/integration/Driver/Pdo/StatementTest.php | 6 +++--- test/unit/AdapterServiceFactoryTest.php | 10 ++++++---- test/unit/AdapterTest.php | 6 +++--- test/unit/ConfigProviderTest.php | 14 +++++++------- test/unit/Driver/Pdo/ConnectionTest.php | 2 +- .../unit/Driver/Pdo/ConnectionTransactionsTest.php | 2 +- test/unit/Driver/Pdo/PdoTest.php | 4 ++-- test/unit/Driver/Pdo/ResultTest.php | 5 ++--- test/unit/Driver/Pdo/StatementTest.php | 3 +-- test/unit/Platform/SqliteTest.php | 6 +++--- test/unit/Sql/Platform/SelectDecoratorTest.php | 1 - test/unit/Sql/Platform/SqliteTest.php | 2 +- 25 files changed, 57 insertions(+), 70 deletions(-) diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index ce40636..5a093e0 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -7,13 +7,13 @@ use Laminas\ServiceManager\Factory\InvokableFactory; use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\ConnectionInterface; -use PhpDb\Adapter\Driver\PdoConnectionInterface; use PhpDb\Adapter\Driver\DriverInterface; +use PhpDb\Adapter\Driver\Pdo\Result; +use PhpDb\Adapter\Driver\Pdo\Statement; +use PhpDb\Adapter\Driver\PdoConnectionInterface; use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Driver\Pdo\Result; -use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\Adapter\Profiler\Profiler; use PhpDb\Adapter\Profiler\ProfilerInterface; @@ -34,10 +34,10 @@ public function __invoke(): array public function getDependencies(): array { return [ - 'aliases' => [ + 'aliases' => [ MetadataInterface::class => Metadata\Source\SqliteMetadata::class, ], - 'factories' => [ + 'factories' => [ Metadata\Source\SqliteMetadata::class => Container\MetadataInterfaceFactory::class, ], 'delegators' => [ diff --git a/src/Container/AdapterFactory.php b/src/Container/AdapterFactory.php index e593fff..b396be9 100644 --- a/src/Container/AdapterFactory.php +++ b/src/Container/AdapterFactory.php @@ -81,4 +81,3 @@ public function __invoke(ContainerInterface $container): AdapterInterface ); } } - diff --git a/src/Container/MetadataInterfaceFactory.php b/src/Container/MetadataInterfaceFactory.php index 4d7194c..65bcef8 100644 --- a/src/Container/MetadataInterfaceFactory.php +++ b/src/Container/MetadataInterfaceFactory.php @@ -5,8 +5,8 @@ namespace PhpDb\Adapter\Sqlite\Container; use PhpDb\Adapter\AdapterInterface; -use PhpDb\Adapter\Sqlite\Metadata\Source\SqliteMetadata; use PhpDb\Adapter\SchemaAwareInterface; +use PhpDb\Adapter\Sqlite\Metadata\Source\SqliteMetadata; use PhpDb\Metadata\MetadataInterface; use Psr\Container\ContainerInterface; diff --git a/src/Container/PdoDriverFactory.php b/src/Container/PdoDriverFactory.php index 9e06103..33cd20b 100644 --- a/src/Container/PdoDriverFactory.php +++ b/src/Container/PdoDriverFactory.php @@ -10,8 +10,8 @@ use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Sqlite\Driver\Pdo\Feature\SqliteRowCounter; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; +use PhpDb\Adapter\Sqlite\Driver\Pdo\Feature\SqliteRowCounter; use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo as PdoDriver; use PhpDb\Container\AdapterManager; use Psr\Container\ContainerInterface; diff --git a/src/Container/PdoStatementFactory.php b/src/Container/PdoStatementFactory.php index 7b8c343..1b33117 100644 --- a/src/Container/PdoStatementFactory.php +++ b/src/Container/PdoStatementFactory.php @@ -6,7 +6,6 @@ use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\ParameterContainer; use Psr\Container\ContainerInterface; final class PdoStatementFactory diff --git a/src/Container/PlatformInterfaceFactory.php b/src/Container/PlatformInterfaceFactory.php index ba09cc5..32b8ebf 100644 --- a/src/Container/PlatformInterfaceFactory.php +++ b/src/Container/PlatformInterfaceFactory.php @@ -6,8 +6,8 @@ use PDO; use PhpDb\Adapter\Driver\PdoDriverInterface; -use PhpDb\Adapter\Sqlite\Platform\Sqlite; use PhpDb\Adapter\Platform\PlatformInterface; +use PhpDb\Adapter\Sqlite\Platform\Sqlite; use PhpDb\Container\AdapterManager; use Psr\Container\ContainerInterface; diff --git a/src/Driver/Pdo/Connection.php b/src/Driver/Pdo/Connection.php index d221b39..243e047 100644 --- a/src/Driver/Pdo/Connection.php +++ b/src/Driver/Pdo/Connection.php @@ -5,6 +5,7 @@ namespace PhpDb\Adapter\Sqlite\Driver\Pdo; use Override; +use PDO; use PDOException; use PhpDb\Adapter\Driver\ConnectionInterface; use PhpDb\Adapter\Driver\Pdo\AbstractPdoConnection; @@ -94,9 +95,9 @@ public function connect(): ConnectionInterface $this->dsn = $dsn; try { - $this->resource = new \PDO(dsn: $dsn, options: $options); - $this->resource->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - $this->driverName = strtolower($this->resource->getAttribute(\PDO::ATTR_DRIVER_NAME)); + $this->resource = new PDO(dsn: $dsn, options: $options); + $this->resource->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->driverName = strtolower($this->resource->getAttribute(PDO::ATTR_DRIVER_NAME)); } catch (PDOException $e) { $code = $e->getCode(); if (! is_int($code)) { diff --git a/src/Metadata/Source/SqliteMetadata.php b/src/Metadata/Source/SqliteMetadata.php index 0d09a4e..3ff9f60 100644 --- a/src/Metadata/Source/SqliteMetadata.php +++ b/src/Metadata/Source/SqliteMetadata.php @@ -171,7 +171,7 @@ protected function loadConstraintData(string $table, string $schema): void $id = $name = null; foreach ($foreignKeys as $fk) { if ($id !== $fk['id']) { - $id = $fk['id']; + $id = $fk['id']; // todo: decide on whether to continue to use _laminas_ $name = '_laminas_' . $table . '_FOREIGN_KEY_' . ($id + 1); $constraints[$name] = [ diff --git a/src/Platform/Sqlite.php b/src/Platform/Sqlite.php index 0071fb1..10e1a24 100644 --- a/src/Platform/Sqlite.php +++ b/src/Platform/Sqlite.php @@ -5,12 +5,11 @@ namespace PhpDb\Adapter\Sqlite\Platform; use Override; +use PDO; use PhpDb\Adapter\Driver\PdoDriverInterface; -use PhpDb\Adapter\Exception; use PhpDb\Adapter\Platform\AbstractPlatform; -use PhpDb\Sql\Platform\PlatformDecoratorInterface; -use PhpDb\Adapter\Sqlite\Driver\Pdo; use PhpDb\Adapter\Sqlite\Sql\Platform\Sqlite as SqlPlatformDecorator; +use PhpDb\Sql\Platform\PlatformDecoratorInterface; class Sqlite extends AbstractPlatform { @@ -18,7 +17,7 @@ class Sqlite extends AbstractPlatform /** @var string[] */ protected $quoteIdentifier = ['"', '"']; - /** @var \PDO */ + /** @var PDO */ protected $resource; /** @@ -27,7 +26,7 @@ class Sqlite extends AbstractPlatform protected $quoteIdentifierTo = '\''; public function __construct( - protected readonly PdoDriverInterface|\PDO $driver + protected readonly PdoDriverInterface|PDO $driver ) { } @@ -43,7 +42,7 @@ public function quoteValue(string $value): string $resource = $resource->getConnection()->getResource(); } - if ($resource instanceof \PDO) { + if ($resource instanceof PDO) { return $resource->quote($value); } @@ -62,7 +61,7 @@ public function quoteTrustedValue(int|float|string|bool $value): ?string $resource = $resource->getConnection()->getResource(); } - if ($resource instanceof \PDO) { + if ($resource instanceof PDO) { return $resource->quote($value); } diff --git a/src/Sql/Platform/Ddl/AlterTableDecorator.php b/src/Sql/Platform/Ddl/AlterTableDecorator.php index 1013bdd..568efea 100644 --- a/src/Sql/Platform/Ddl/AlterTableDecorator.php +++ b/src/Sql/Platform/Ddl/AlterTableDecorator.php @@ -4,6 +4,7 @@ use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\Sql\Ddl\AlterTable; +use PhpDb\Sql\Ddl\Column\ColumnInterface; use PhpDb\Sql\Platform\PlatformDecoratorInterface; use function count; @@ -147,7 +148,7 @@ protected function processChangeColumns(?PlatformInterface $adapterPlatform = nu { $sqls = []; - /** @var \PhpDb\Sql\Ddl\Column\ColumnInterface $column */ + /** @var ColumnInterface $column */ foreach ($this->changeColumns as $name => $column) { $sql = $this->processExpression($column, $adapterPlatform); $insertStart = $this->getSqlInsertOffsets($sql); @@ -217,6 +218,7 @@ private function normalizeColumnOption(string $name): string /** * phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod + * * @psalm-suppress UnusedReturnValue */ private function compareColumnOptions(string $columnA, string $columnB): int diff --git a/src/Sql/Platform/Ddl/CreateTableDecorator.php b/src/Sql/Platform/Ddl/CreateTableDecorator.php index ad1756f..8549803 100644 --- a/src/Sql/Platform/Ddl/CreateTableDecorator.php +++ b/src/Sql/Platform/Ddl/CreateTableDecorator.php @@ -18,9 +18,7 @@ final class CreateTableDecorator extends CreateTable implements PlatformDecoratorInterface { - /** - * @psalm-suppress PossiblyUnusedProperty - */ + /** @psalm-suppress PossiblyUnusedProperty */ protected CreateTable $subject; /** @var int[] */ @@ -158,6 +156,7 @@ private function normalizeColumnOption(string $name): string /** * phpcs:ignore SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod + * * @psalm-suppress UnusedReturnValue */ private function compareColumnOptions(string $columnA, string $columnB): int diff --git a/test/integration/Container/TestAsset/SetupTrait.php b/test/integration/Container/TestAsset/SetupTrait.php index 1acd229..7c02a3b 100644 --- a/test/integration/Container/TestAsset/SetupTrait.php +++ b/test/integration/Container/TestAsset/SetupTrait.php @@ -4,17 +4,17 @@ namespace PhpDbIntegrationTest\Adapter\Sqlite\Container\TestAsset; -use Override; use Laminas\ServiceManager\ServiceManager; use Laminas\Stdlib\ArrayUtils; +use Override; use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\DriverInterface; use PhpDb\Adapter\Sqlite\ConfigProvider; use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; use PhpDb\Container\AdapterManager; use PhpDb\Container\ConfigProvider as LaminasDbConfigProvider; -use Psr\Container\ContainerInterface; use PHPUnit\Framework\Attributes\RequiresPhpExtension; +use Psr\Container\ContainerInterface; /** * This trait provides a setup method for integration tests that require diff --git a/test/integration/Driver/Pdo/ConnectionIntegrationTest.php b/test/integration/Driver/Pdo/ConnectionIntegrationTest.php index 7acc4c3..088a668 100644 --- a/test/integration/Driver/Pdo/ConnectionIntegrationTest.php +++ b/test/integration/Driver/Pdo/ConnectionIntegrationTest.php @@ -36,17 +36,6 @@ public function testGetCurrentSchema(): void self::assertIsString($connection->getCurrentSchema()); } - // public function testSetResource(): void - // { - // $resource = $this->getAdapter()->getDriver()->getConnection()->getResource(); - // $connection = new Connection([]); - // self::assertSame($connection, $connection->setResource($resource)); - - // $connection->disconnect(); - // unset($connection); - // unset($resource); - // } - public function testGetResource(): void { $connection = $this->getAdapter()->getDriver()->getConnection(); @@ -129,7 +118,7 @@ public function testGetLastGeneratedValue(): never public function testConnectReturnsConnectionWhenResourceSet(): void { /** @var PDO $resource */ - $resource = $this->getAdapter()->getDriver()->getConnection()->getResource(); + $resource = $this->getAdapter()->getDriver()->getConnection()->getResource(); /** @var PdoConnectionInterface&Connection $connection */ $connection = $this->getAdapter()->getDriver()->getConnection(); self::assertInstanceOf(PdoConnectionInterface::class, $connection); diff --git a/test/integration/Driver/Pdo/StatementTest.php b/test/integration/Driver/Pdo/StatementTest.php index ab73c02..5290b41 100644 --- a/test/integration/Driver/Pdo/StatementTest.php +++ b/test/integration/Driver/Pdo/StatementTest.php @@ -6,9 +6,9 @@ use PDO; use PDOStatement; -use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Driver\Pdo\Result; +use PhpDb\Adapter\Driver\Pdo\Statement; +use PhpDb\Adapter\Driver\StatementInterface; use PhpDbIntegrationTest\Adapter\Sqlite\Container\TestAsset\SetupTrait; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; @@ -29,7 +29,7 @@ final class StatementTest extends TestCase public function testGetResource(): void { /** @var PDO $pdo */ - $pdo = $this->getAdapter()->getDriver()->getConnection()->getResource(); + $pdo = $this->getAdapter()->getDriver()->getConnection()->getResource(); /** @var StatementInterface&Statement $statement */ $statement = $this->getAdapter()->getDriver()->createStatement(); /** @var PDOStatement $stmt */ diff --git a/test/unit/AdapterServiceFactoryTest.php b/test/unit/AdapterServiceFactoryTest.php index 9b3e63f..87952db 100644 --- a/test/unit/AdapterServiceFactoryTest.php +++ b/test/unit/AdapterServiceFactoryTest.php @@ -2,20 +2,22 @@ namespace PhpDbTest\Adapter\Sqlite; +use Laminas\ServiceManager\ServiceLocatorInterface; +use Laminas\ServiceManager\ServiceManager; +use Override; use PhpDb\Adapter\Profiler\Profiler; use PhpDb\Adapter\Profiler\ProfilerInterface; use PhpDb\Adapter\Sqlite\Adapter; use PhpDb\Adapter\Sqlite\AdapterServiceFactory; use PhpDb\Adapter\Sqlite\ConfigProvider; -use Laminas\ServiceManager\ServiceLocatorInterface; -use Laminas\ServiceManager\ServiceManager; -use Override; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use function array_key_exists; use function extension_loaded; +use function is_array; #[CoversMethod(AdapterServiceFactory::class, '__invoke')] final class AdapterServiceFactoryTest extends TestCase @@ -24,7 +26,7 @@ final class AdapterServiceFactoryTest extends TestCase protected function createServiceManager(array $dbConfig): ServiceLocatorInterface { - $config = (new ConfigProvider())->getDependencies(); + $config = (new ConfigProvider())->getDependencies(); if (array_key_exists('services', $config) && is_array($config['services'])) { $config['services']['config'] = $dbConfig; } diff --git a/test/unit/AdapterTest.php b/test/unit/AdapterTest.php index 077b82c..be91f1c 100644 --- a/test/unit/AdapterTest.php +++ b/test/unit/AdapterTest.php @@ -3,6 +3,7 @@ namespace PhpDbTest\Adapter\Sqlite; use InvalidArgumentException; +use Override; use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\ConnectionInterface; use PhpDb\Adapter\Driver\DriverInterface; @@ -10,14 +11,13 @@ use PhpDb\Adapter\Driver\StatementInterface; use PhpDb\Adapter\ParameterContainer; use PhpDb\Adapter\Profiler; -use PhpDb\ResultSet\ResultSet; -use PhpDb\ResultSet\ResultSetInterface; use PhpDb\Adapter\Sqlite\Adapter; use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; use PhpDb\Adapter\Sqlite\Driver\Pdo\Statement; use PhpDb\Adapter\Sqlite\Platform\Sqlite as SqlitePlatform; +use PhpDb\ResultSet\ResultSet; +use PhpDb\ResultSet\ResultSetInterface; use PhpDbTest\Adapter\Sqlite\TestAsset\TemporaryResultSet; -use Override; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\TestDox; diff --git a/test/unit/ConfigProviderTest.php b/test/unit/ConfigProviderTest.php index a73109f..23e4386 100644 --- a/test/unit/ConfigProviderTest.php +++ b/test/unit/ConfigProviderTest.php @@ -4,15 +4,16 @@ namespace PhpDbTest\Adapter\Sqlite; +use Laminas\ServiceManager\Factory\InvokableFactory; use PhpDb\Adapter\AdapterInterface; -use PhpDb\Adapter\Driver\DriverInterface; use PhpDb\Adapter\Driver\ConnectionInterface; +use PhpDb\Adapter\Driver\DriverInterface; +use PhpDb\Adapter\Driver\Pdo\Result; +use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Driver\PdoConnectionInterface; use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Driver\StatementInterface; -use PhpDb\Adapter\Driver\Pdo\Result; -use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\Adapter\Profiler\Profiler; use PhpDb\Adapter\Profiler\ProfilerInterface; @@ -24,7 +25,6 @@ use PhpDb\Container\AdapterManager; use PhpDb\Metadata\MetadataInterface; use PhpDb\ResultSet; -use Laminas\ServiceManager\Factory\InvokableFactory; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; @@ -36,10 +36,10 @@ final class ConfigProviderTest extends TestCase { /** @var array> */ private array $config = [ - 'aliases' => [ + 'aliases' => [ MetadataInterface::class => Metadata\Source\SqliteMetadata::class, ], - 'factories' => [ + 'factories' => [ Metadata\Source\SqliteMetadata::class => Container\MetadataInterfaceFactory::class, ], 'delegators' => [ @@ -76,7 +76,7 @@ final class ConfigProviderTest extends TestCase Statement::class => Container\PdoStatementFactory::class, Platform\Sqlite::class => Container\PlatformInterfaceFactory::class, //Profiler::class => InvokableFactory::class, - ResultSet\ResultSet::class => InvokableFactory::class, + ResultSet\ResultSet::class => InvokableFactory::class, ], ]; diff --git a/test/unit/Driver/Pdo/ConnectionTest.php b/test/unit/Driver/Pdo/ConnectionTest.php index 4e135f5..3fd2866 100644 --- a/test/unit/Driver/Pdo/ConnectionTest.php +++ b/test/unit/Driver/Pdo/ConnectionTest.php @@ -5,9 +5,9 @@ namespace PhpDbTest\Adapter\Sqlite\Sqlite\Driver\Pdo; use Exception; +use Override; use PhpDb\Adapter\Exception\InvalidConnectionParametersException; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; -use Override; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Driver/Pdo/ConnectionTransactionsTest.php b/test/unit/Driver/Pdo/ConnectionTransactionsTest.php index 4c7ca1f..9980060 100644 --- a/test/unit/Driver/Pdo/ConnectionTransactionsTest.php +++ b/test/unit/Driver/Pdo/ConnectionTransactionsTest.php @@ -4,11 +4,11 @@ namespace PhpDbTest\Adapter\Sqlite\Driver\Pdo; +use Override; use PhpDb\Adapter\Driver\AbstractConnection; use PhpDb\Adapter\Exception\RuntimeException; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; use PhpDbTest\Adapter\Sqlite\TestAsset\ConnectionWrapper; -use Override; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Driver/Pdo/PdoTest.php b/test/unit/Driver/Pdo/PdoTest.php index 7fabbb4..3d32f2f 100644 --- a/test/unit/Driver/Pdo/PdoTest.php +++ b/test/unit/Driver/Pdo/PdoTest.php @@ -5,11 +5,11 @@ namespace PhpDbTest\Adapter\Sqlite\Sqlite\Driver\Pdo; use Override; -use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\Pdo\Result; -use PhpDb\Exception\RuntimeException; +use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; +use PhpDb\Exception\RuntimeException; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Driver/Pdo/ResultTest.php b/test/unit/Driver/Pdo/ResultTest.php index 263d3fb..bc0d1e6 100644 --- a/test/unit/Driver/Pdo/ResultTest.php +++ b/test/unit/Driver/Pdo/ResultTest.php @@ -4,16 +4,15 @@ namespace PhpDbTest\Adapter\Sqlite\Sqlite\Driver\Pdo; -use PhpDb\Adapter\Driver\Pdo\Result; -use PhpDb\Adapter\Exception\InvalidArgumentException; use PDO; use PDOStatement; +use PhpDb\Adapter\Driver\Pdo\Result; +use PhpDb\Adapter\Exception\InvalidArgumentException; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; use stdClass; -use function assert; use function uniqid; #[CoversMethod(Result::class, 'current')] diff --git a/test/unit/Driver/Pdo/StatementTest.php b/test/unit/Driver/Pdo/StatementTest.php index ccf0951..ca8a0b1 100644 --- a/test/unit/Driver/Pdo/StatementTest.php +++ b/test/unit/Driver/Pdo/StatementTest.php @@ -4,11 +4,10 @@ namespace PhpDbTest\Adapter\Sqlite\Sqlite\Driver\Pdo; +use Override; use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\ParameterContainer; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; -use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; -use Override; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; diff --git a/test/unit/Platform/SqliteTest.php b/test/unit/Platform/SqliteTest.php index 8053a11..a41e2fb 100644 --- a/test/unit/Platform/SqliteTest.php +++ b/test/unit/Platform/SqliteTest.php @@ -2,15 +2,13 @@ namespace PhpDbTest\Adapter\Sqlite\Sqlite\Platform; +use Override; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; -use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; use PhpDb\Adapter\Sqlite\Platform\Sqlite; -use Override; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; use function file_exists; -use function realpath; use function restore_error_handler; use function set_error_handler; use function touch; @@ -76,6 +74,7 @@ public function testQuoteValueRaisesNoticeWithoutPlatformSupport(): void $this->assertEquals(E_USER_NOTICE, $errno); $this->assertEquals( $errstr, + // phpcs:ignore Generic.Files.LineLength 'Attempting to quote a value in PhpDb\Adapter\Sqlite\Platform\Sqlite without extension/driver support can ' . 'introduce security vulnerabilities in a production environment' ); @@ -125,6 +124,7 @@ public function testQuoteValueList(): void $this->assertEquals(E_USER_NOTICE, $errno); $this->assertEquals( $errstr, + // phpcs:ignore Generic.Files.LineLength 'Attempting to quote a value in PhpDb\Adapter\Sqlite\Platform\Sqlite without extension/driver support can ' . 'introduce security vulnerabilities in a production environment' ); diff --git a/test/unit/Sql/Platform/SelectDecoratorTest.php b/test/unit/Sql/Platform/SelectDecoratorTest.php index b93cf03..a0fd91b 100644 --- a/test/unit/Sql/Platform/SelectDecoratorTest.php +++ b/test/unit/Sql/Platform/SelectDecoratorTest.php @@ -27,7 +27,6 @@ final class SelectDecoratorTest extends TestCase protected function setUp(): void { - $this->driver = $this->getMockBuilder(PdoDriverInterface::class) ->getMock(); diff --git a/test/unit/Sql/Platform/SqliteTest.php b/test/unit/Sql/Platform/SqliteTest.php index e51828f..61feae8 100644 --- a/test/unit/Sql/Platform/SqliteTest.php +++ b/test/unit/Sql/Platform/SqliteTest.php @@ -2,9 +2,9 @@ namespace PhpDbTest\Adapter\Sqlite\Sql\Platform; -use PhpDb\Sql\Select; use PhpDb\Adapter\Sqlite\Sql\Platform\SelectDecorator; use PhpDb\Adapter\Sqlite\Sql\Platform\Sqlite; +use PhpDb\Sql\Select; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\TestCase; From a4532e95f798a8648126ef426aae44c8028fbc0d Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sun, 19 Oct 2025 21:18:03 +1000 Subject: [PATCH 2/9] Only pass a string to strtolower As pointed out by Psalm, strtolower only accepts a string, but getAttribute returns mixed. So, in the most simplistic way I know, this change refactors to the code to ensure that the driver name is only passed from getAttribute to strtolower if it is a string. --- src/Driver/Pdo/Connection.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Driver/Pdo/Connection.php b/src/Driver/Pdo/Connection.php index 243e047..2c3fafd 100644 --- a/src/Driver/Pdo/Connection.php +++ b/src/Driver/Pdo/Connection.php @@ -13,6 +13,7 @@ use Webmozart\Assert\Assert; use function array_diff_key; +use function assert; use function is_int; use function is_string; use function str_starts_with; @@ -97,7 +98,9 @@ public function connect(): ConnectionInterface try { $this->resource = new PDO(dsn: $dsn, options: $options); $this->resource->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $this->driverName = strtolower($this->resource->getAttribute(PDO::ATTR_DRIVER_NAME)); + $driverName = $this->resource->getAttribute(PDO::ATTR_DRIVER_NAME); + assert(is_string($driverName)); + $this->driverName = strtolower($driverName); } catch (PDOException $e) { $code = $e->getCode(); if (! is_int($code)) { From 1ec4c36fc98a3545d1546c925213cc58be1da510 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sun, 19 Oct 2025 21:19:33 +1000 Subject: [PATCH 3/9] Annotate Module as being part of the public API This change has been made as Psalm can't detect usage of the class on its own, so thinks that it is dead code, when it likely isn't; at least not at the moment. --- src/Module.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Module.php b/src/Module.php index e304815..8aed970 100644 --- a/src/Module.php +++ b/src/Module.php @@ -4,6 +4,9 @@ namespace PhpDb\Adapter\Sqlite; +/** + * @psalm-api + */ final class Module { public function getConfig(): array From 7bdd5638b687d95458c64785b3b6c67aab941bbe Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sun, 19 Oct 2025 21:20:32 +1000 Subject: [PATCH 4/9] Properly annotate the structure of $config --- test/unit/ConfigProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/ConfigProviderTest.php b/test/unit/ConfigProviderTest.php index 23e4386..412a147 100644 --- a/test/unit/ConfigProviderTest.php +++ b/test/unit/ConfigProviderTest.php @@ -34,7 +34,7 @@ #[CoversMethod(ConfigProvider::class, 'getAdapterManagerConfig')] final class ConfigProviderTest extends TestCase { - /** @var array> */ + /** @var array>> */ private array $config = [ 'aliases' => [ MetadataInterface::class => Metadata\Source\SqliteMetadata::class, From 59c5c67ed4fadc7ddb06751a146fb2b57e8955b4 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sun, 19 Oct 2025 21:37:13 +1000 Subject: [PATCH 5/9] Pass all required arguments to initialisation of Pdo Pdo's constructor requires three mandatory arguments: a AbstractPdoConnection|PDO object, a StatementInterface&PdoDriverAwareInterface object, and a ResultInterface object. This change mocks the second and third arguments to ensure, which were missing. --- test/unit/Driver/Pdo/PdoTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/unit/Driver/Pdo/PdoTest.php b/test/unit/Driver/Pdo/PdoTest.php index 3d32f2f..97f7259 100644 --- a/test/unit/Driver/Pdo/PdoTest.php +++ b/test/unit/Driver/Pdo/PdoTest.php @@ -6,7 +6,10 @@ use Override; use PhpDb\Adapter\Driver\Pdo\Result; +use PhpDb\Adapter\Driver\PdoDriverAwareInterface; use PhpDb\Adapter\Driver\PdoDriverInterface; +use PhpDb\Adapter\Driver\ResultInterface; +use PhpDb\Adapter\Driver\StatementInterface; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo; use PhpDb\Exception\RuntimeException; @@ -29,7 +32,17 @@ protected function setUp(): void { $connection = new Connection(); - $this->pdo = new Pdo($connection); + /** @var StatementInterface&PdoDriverAwareInterface $statementPrototype */ + $statementPrototype = $this->createMockForIntersectionOfInterfaces([ + StatementInterface::class, + PdoDriverAwareInterface::class, + ]); + + $this->pdo = new Pdo( + $connection, + $statementPrototype, + $this->createMock(ResultInterface::class), + ); } public function testGetDatabasePlatformName(): void From af3c2ccb10e8f30c1060c8077ee4fd6cbe043d41 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Sun, 19 Oct 2025 21:39:15 +1000 Subject: [PATCH 6/9] Suppress a Psalm InvalidArgument failure I don't see this Psalm failure as an issue for the time being, so am ignoring it in this change, quite visibly so that it will be hard to miss, and attended to in the future. --- test/unit/Platform/SqliteTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/unit/Platform/SqliteTest.php b/test/unit/Platform/SqliteTest.php index a41e2fb..c5ca238 100644 --- a/test/unit/Platform/SqliteTest.php +++ b/test/unit/Platform/SqliteTest.php @@ -70,7 +70,11 @@ public function testGetQuoteValueSymbol(): void public function testQuoteValueRaisesNoticeWithoutPlatformSupport(): void { $raisedNotice = false; - set_error_handler(function ($errno, $errstr) use (&$raisedNotice) { + + /** + * @psalm-suppress InvalidArgument + */ + set_error_handler(function (int $errno, string $errstr) use (&$raisedNotice) { $this->assertEquals(E_USER_NOTICE, $errno); $this->assertEquals( $errstr, @@ -120,6 +124,10 @@ public function testQuoteTrustedValue(): void public function testQuoteValueList(): void { $raisedNotice = false; + + /** + * @psalm-suppress InvalidArgument + */ set_error_handler(function ($errno, $errstr) use (&$raisedNotice) { $this->assertEquals(E_USER_NOTICE, $errno); $this->assertEquals( From a117ffcf93c512ef1cdc7a33203bbe15c59cc3cb Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Thu, 30 Oct 2025 22:05:00 +1000 Subject: [PATCH 7/9] Update the Psalm baseline The change is required to reflect the changes in the package, or to properly reflect the package. --- psalm-baseline.xml | 110 +-------------------------------------------- 1 file changed, 2 insertions(+), 108 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 6a39fe2..cd4fe80 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -21,9 +21,7 @@ connectionParameters]]> - - resource->getAttribute(\PDO::ATTR_DRIVER_NAME)]]> - + @@ -80,9 +78,6 @@ - - - @@ -364,17 +359,9 @@ - getMockBuilder(Driver::class) - ->setConstructorArgs([ - $this->mockConnection, - $this->mockStatement, - ]) - ->getMock()]]> getMockBuilder(Statement::class)->getMock()]]> - - @@ -432,8 +419,6 @@ - - @@ -441,47 +426,6 @@ - - - [ - PlatformInterface::class => Platform\Sqlite::class, - ProfilerInterface::class => Profiler::class, - ], - 'factories' => [ - AdapterInterface::class => AdapterServiceFactory::class, - DriverInterface::class => Driver\Pdo\DriverFactory::class, - Platform\Sqlite::class => InvokableFactory::class, - Profiler::class => InvokableFactory::class, - ], - ]]]> - - - - - - - - - - pdo]]> - - - - - - pdo]]> - pdo]]> - pdo]]> - pdo]]> - pdo]]> - pdo]]> - - - - - - @@ -490,17 +434,6 @@ - - - - - - - - - - - @@ -509,47 +442,8 @@ - - - [ - PlatformInterface::class => Platform\Sqlite::class, - ProfilerInterface::class => Profiler::class, - ], - 'factories' => [ - AdapterInterface::class => AdapterServiceFactory::class, - DriverInterface::class => Driver\Pdo\DriverFactory::class, - Platform\Sqlite::class => InvokableFactory::class, - Profiler::class => InvokableFactory::class, - ], - ]]]> - - - - - - - - assertEquals(E_USER_NOTICE, $errno); - $this->assertEquals( - $errstr, - 'Attempting to quote a value in PhpDb\Adapter\Sqlite\Platform\Sqlite without extension/driver support can ' - . 'introduce security vulnerabilities in a production environment' - ); - $raisedNotice = true; - }]]> - assertEquals(E_USER_NOTICE, $errno); - $this->assertEquals( - $errstr, - 'Attempting to quote a value in PhpDb\Adapter\Sqlite\Platform\Sqlite without extension/driver support can ' - . 'introduce security vulnerabilities in a production environment' - ); - $raisedNotice = true; - }]]> - + From 49a38a50d485390a0d9095ce21f63b6c4178487c Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Thu, 30 Oct 2025 22:08:21 +1000 Subject: [PATCH 8/9] Update the code following static analysis review This change is a minor one, changing the code, improving it based on the results of the review. --- src/Driver/Pdo/Pdo.php | 2 +- src/Platform/Sqlite.php | 4 ++-- test/unit/AdapterServiceFactoryTest.php | 4 ++-- test/unit/AdapterTest.php | 1 + test/unit/ConfigProviderTest.php | 5 ++++- test/unit/Driver/Pdo/StatementTest.php | 11 ++++++++++- test/unit/Platform/SqliteTest.php | 3 +++ 7 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Driver/Pdo/Pdo.php b/src/Driver/Pdo/Pdo.php index ac7d1d1..e449816 100644 --- a/src/Driver/Pdo/Pdo.php +++ b/src/Driver/Pdo/Pdo.php @@ -13,7 +13,7 @@ use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Sqlite\Driver\DatabasePlatformNameTrait; -final class Pdo extends AbstractPdo implements DriverFeatureProviderInterface +class Pdo extends AbstractPdo implements DriverFeatureProviderInterface { use DatabasePlatformNameTrait; use DriverFeatureProviderTrait; diff --git a/src/Platform/Sqlite.php b/src/Platform/Sqlite.php index 10e1a24..f1afa4d 100644 --- a/src/Platform/Sqlite.php +++ b/src/Platform/Sqlite.php @@ -42,7 +42,7 @@ public function quoteValue(string $value): string $resource = $resource->getConnection()->getResource(); } - if ($resource instanceof PDO) { + if ($resource instanceof \PDO) { return $resource->quote($value); } @@ -61,7 +61,7 @@ public function quoteTrustedValue(int|float|string|bool $value): ?string $resource = $resource->getConnection()->getResource(); } - if ($resource instanceof PDO) { + if ($resource instanceof \PDO) { return $resource->quote($value); } diff --git a/test/unit/AdapterServiceFactoryTest.php b/test/unit/AdapterServiceFactoryTest.php index 87952db..9ca6eec 100644 --- a/test/unit/AdapterServiceFactoryTest.php +++ b/test/unit/AdapterServiceFactoryTest.php @@ -7,8 +7,8 @@ use Override; use PhpDb\Adapter\Profiler\Profiler; use PhpDb\Adapter\Profiler\ProfilerInterface; -use PhpDb\Adapter\Sqlite\Adapter; -use PhpDb\Adapter\Sqlite\AdapterServiceFactory; +use PhpDb\Adapter\Adapter; +use PhpDb\Adapter\AdapterServiceFactory; use PhpDb\Adapter\Sqlite\ConfigProvider; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; diff --git a/test/unit/AdapterTest.php b/test/unit/AdapterTest.php index be91f1c..3df8651 100644 --- a/test/unit/AdapterTest.php +++ b/test/unit/AdapterTest.php @@ -7,6 +7,7 @@ use PhpDb\Adapter\AdapterInterface; use PhpDb\Adapter\Driver\ConnectionInterface; use PhpDb\Adapter\Driver\DriverInterface; +use PhpDb\Adapter\Driver\PdoDriverInterface; use PhpDb\Adapter\Driver\ResultInterface; use PhpDb\Adapter\Driver\StatementInterface; use PhpDb\Adapter\ParameterContainer; diff --git a/test/unit/ConfigProviderTest.php b/test/unit/ConfigProviderTest.php index 412a147..9f1fffa 100644 --- a/test/unit/ConfigProviderTest.php +++ b/test/unit/ConfigProviderTest.php @@ -91,7 +91,10 @@ public function testProvidesExpectedDependencies(): ConfigProvider public function testProvidesExpectedAdapterManagerConfiguration(): void { $provider = new ConfigProvider(); - self::assertEquals($this->adapterManagerConfig, $provider->getAdapterManagerConfig()); + self::assertEquals( + $this->adapterManagerConfig, + $provider->getAdapterManagerConfig() + ); } #[Depends('testProvidesExpectedDependencies')] diff --git a/test/unit/Driver/Pdo/StatementTest.php b/test/unit/Driver/Pdo/StatementTest.php index ca8a0b1..51efe0d 100644 --- a/test/unit/Driver/Pdo/StatementTest.php +++ b/test/unit/Driver/Pdo/StatementTest.php @@ -7,9 +7,11 @@ use Override; use PhpDb\Adapter\Driver\Pdo\Statement; use PhpDb\Adapter\ParameterContainer; +use PhpDb\Adapter\Sqlite\Container\PdoDriverFactory; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; #[CoversMethod(Statement::class, 'setDriver')] #[CoversMethod(Statement::class, 'setParameterContainer')] @@ -26,7 +28,14 @@ final class StatementTest extends TestCase public function testSetDriver(): void { - self::assertEquals($this->statement, $this->statement->setDriver(new Driver(new Connection()))); + self::assertEquals( + $this->statement, + $this->statement->setDriver( + (new PdoDriverFactory())->__invoke( + $this->createMock(ContainerInterface::class) + ) + ) + ); } public function testSetParameterContainer(): void diff --git a/test/unit/Platform/SqliteTest.php b/test/unit/Platform/SqliteTest.php index c5ca238..5913be3 100644 --- a/test/unit/Platform/SqliteTest.php +++ b/test/unit/Platform/SqliteTest.php @@ -3,10 +3,13 @@ namespace PhpDbTest\Adapter\Sqlite\Sqlite\Platform; use Override; +use PDO; +use PhpDb\Adapter\Sqlite\Container\PdoDriverFactory; use PhpDb\Adapter\Sqlite\Driver\Pdo\Connection; use PhpDb\Adapter\Sqlite\Platform\Sqlite; use PHPUnit\Framework\Attributes\CoversMethod; use PHPUnit\Framework\TestCase; +use Psr\Container\ContainerInterface; use function file_exists; use function restore_error_handler; From 13c19ab2506077a6fbbd27a77658309555f482f5 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Thu, 30 Oct 2025 22:12:23 +1000 Subject: [PATCH 9/9] Minor code tightening This is a small change aimed at tightening up the test code. Removing items that are unnecessary, such as checking for the pdo_sqlite extension, and adding in required function arguments, to ensure that the code is as explicit as possible. --- test/unit/AdapterServiceFactoryTest.php | 4 ---- test/unit/AdapterTest.php | 2 +- test/unit/Platform/SqliteTest.php | 11 ++++++----- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/test/unit/AdapterServiceFactoryTest.php b/test/unit/AdapterServiceFactoryTest.php index 9ca6eec..4447585 100644 --- a/test/unit/AdapterServiceFactoryTest.php +++ b/test/unit/AdapterServiceFactoryTest.php @@ -37,10 +37,6 @@ protected function createServiceManager(array $dbConfig): ServiceLocatorInterfac #[Override] protected function setUp(): void { - if (! extension_loaded('pdo_sqlite')) { - $this->markTestSkipped('Adapter factory tests require pdo_sqlite'); - } - $this->factory = new AdapterServiceFactory(); } diff --git a/test/unit/AdapterTest.php b/test/unit/AdapterTest.php index 3df8651..749fef1 100644 --- a/test/unit/AdapterTest.php +++ b/test/unit/AdapterTest.php @@ -245,7 +245,7 @@ public function testMagicGet(): void protected function setUp(): void { $this->mockConnection = $this->createMock(ConnectionInterface::class); - $this->mockPlatform = new SqlitePlatform(); + $this->mockPlatform = new SqlitePlatform($this->createMock(PdoDriverInterface::class)); $this->mockStatement = $this->getMockBuilder(Statement::class)->getMock(); $this->mockDriver = $this->getMockBuilder(Pdo::class) ->setConstructorArgs([ diff --git a/test/unit/Platform/SqliteTest.php b/test/unit/Platform/SqliteTest.php index 5913be3..e8da3a0 100644 --- a/test/unit/Platform/SqliteTest.php +++ b/test/unit/Platform/SqliteTest.php @@ -40,7 +40,9 @@ final class SqliteTest extends TestCase #[Override] protected function setUp(): void { - $this->platform = new Sqlite(); + $this->platform = new Sqlite(new PDO( + dsn: "sqlite::memory:mydb.sqlite", + )); } public function testGetName(): void @@ -191,10 +193,9 @@ public function testCanCloseConnectionAfterQuoteValue(): void touch($filePath); } - $driver = new Driver(new Connection([ - 'driver' => 'Pdo_Sqlite', - 'database' => ':memory', - ])); + $driver = (new PdoDriverFactory())->__invoke( + $this->createMock(ContainerInterface::class) + ); $this->platform->setDriver($driver); $this->platform->quoteValue("some; random]/ value");