From 55b64b28b69d4b11592875f0d0a3296994cd4a48 Mon Sep 17 00:00:00 2001 From: Joey Smith Date: Sun, 11 Jan 2026 05:57:10 -0600 Subject: [PATCH] Proposal for a readonly adapter implementation Signed-off-by: Joey Smith Signed-off-by: Joey Smith --- src/Adapter/Adapter.php | 27 +++++++++++---------------- src/Adapter/AdapterInterface.php | 2 ++ test/unit/Adapter/AdapterTest.php | 15 ++++++++------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/Adapter/Adapter.php b/src/Adapter/Adapter.php index 6a19e00b..d3e68e47 100644 --- a/src/Adapter/Adapter.php +++ b/src/Adapter/Adapter.php @@ -14,30 +14,25 @@ use function is_string; use function strtolower; -class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface, SchemaAwareInterface +readonly class Adapter implements AdapterInterface, SchemaAwareInterface { - /** - * @throws Exception\InvalidArgumentException - */ - public function __construct( + final public function __construct( protected Driver\DriverInterface $driver, protected Platform\PlatformInterface $platform, protected ResultSet\ResultSetInterface $queryResultSetPrototype = new ResultSet\ResultSet(), protected ?Profiler\ProfilerInterface $profiler = null ) { - if ($profiler) { - $this->setProfiler($profiler); - } } - #[Override] - public function setProfiler(Profiler\ProfilerInterface $profiler): Profiler\ProfilerAwareInterface - { - $this->profiler = $profiler; - if ($this->driver instanceof Profiler\ProfilerAwareInterface) { - $this->driver->setProfiler($profiler); - } - return $this; + final public function withProfiler( + Profiler\ProfilerInterface $profiler + ): static { + return new static( + $this->driver, + $this->platform, + $this->queryResultSetPrototype, + $profiler, + ); } #[Override] diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 185aa110..bba62984 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -30,6 +30,8 @@ interface AdapterInterface public const VALUE_QUOTE_SEPARATOR = 'quoteSeparator'; + public function withProfiler(Profiler\ProfilerInterface $profiler): AdapterInterface; + public function getDriver(): Driver\DriverInterface; public function getPlatform(): Platform\PlatformInterface; diff --git a/test/unit/Adapter/AdapterTest.php b/test/unit/Adapter/AdapterTest.php index 5cb21b23..22e32955 100644 --- a/test/unit/Adapter/AdapterTest.php +++ b/test/unit/Adapter/AdapterTest.php @@ -14,6 +14,7 @@ use PhpDb\Adapter\ParameterContainer; use PhpDb\Adapter\Platform\PlatformInterface; use PhpDb\Adapter\Profiler; +use PhpDb\Adapter\SchemaAwareInterface; use PhpDb\ResultSet\ResultSet; use PhpDb\ResultSet\ResultSetInterface; use PhpDbTest\TestAsset\TemporaryResultSet; @@ -24,7 +25,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -#[CoversMethod(Adapter::class, 'setProfiler')] +#[CoversMethod(Adapter::class, 'withProfiler')] #[CoversMethod(Adapter::class, 'getProfiler')] #[CoversMethod(Adapter::class, 'createDriver')] #[CoversMethod(Adapter::class, 'createPlatform')] @@ -45,7 +46,7 @@ final class AdapterTest extends TestCase protected StatementInterface&MockObject $mockStatement; - protected Adapter $adapter; + protected AdapterInterface&SchemaAwareInterface&Adapter $adapter; /** * @throws Exception @@ -66,17 +67,17 @@ protected function setUp(): void $this->adapter = new Adapter($this->mockDriver, $this->mockPlatform); } - #[TestDox('unit test: Test setProfiler() will store profiler')] - public function testSetProfiler(): void + #[TestDox('unit test: Test withProfiler() will store profiler')] + public function testWithProfiler(): void { - $ret = $this->adapter->setProfiler(new Profiler\Profiler()); - self::assertSame($this->adapter, $ret); + $ret = $this->adapter->withProfiler(new Profiler\Profiler()); + self::assertNotSame($this->adapter, $ret); } #[TestDox('unit test: Test getProfiler() will store profiler')] public function testGetProfiler(): void { - $this->adapter->setProfiler($profiler = new Profiler\Profiler()); + $this->adapter = $this->adapter->withProfiler($profiler = new Profiler\Profiler()); self::assertSame($profiler, $this->adapter->getProfiler()); $adapter = new Adapter(