Skip to content

Commit 2492690

Browse files
committed
Introduced type checks for SqlPlatform for code safety
Signed-off-by: Simon Mundy <simon.mundy@peptolab.com>
1 parent 4572d73 commit 2492690

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/Sql/Platform/AbstractPlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getSqlString(?PlatformInterface $adapterPlatform = null): string
8383
if (! $this->subject instanceof SqlInterface) {
8484
throw new Exception\RuntimeException(
8585
'The subject does not appear to implement PhpDb\Sql\SqlInterface, thus calling '
86-
. 'prepareStatement() has no effect'
86+
. 'getSqlString() has no effect'
8787
);
8888
}
8989

src/Sql/Platform/PlatformDecoratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpDb\Sql\PreparableSqlInterface;
88
use PhpDb\Sql\SqlInterface;
99

10-
interface PlatformDecoratorInterface extends PreparableSqlInterface, SqlInterface
10+
interface PlatformDecoratorInterface
1111
{
1212
public function setSubject(
1313
SqlInterface|PreparableSqlInterface|null $subject

src/Sql/Sql.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,17 @@ public function prepareStatementForSqlObject(
109109
?StatementInterface $statement = null,
110110
?AdapterInterface $adapter = null
111111
): StatementInterface {
112+
if (! $this->sqlPlatform instanceof PreparableSqlInterface) {
113+
throw new Exception\RuntimeException(
114+
'The subject does not implement PreparableSqlInterface'
115+
);
116+
}
117+
112118
$adapter ??= $this->adapter;
113119
$statement ??= $adapter->getDriver()->createStatement();
114120

115-
$this->sqlPlatform->setSubject($sqlObject)->prepareStatement($adapter, $statement);
121+
$this->sqlPlatform->setSubject($sqlObject);
122+
$this->sqlPlatform->prepareStatement($adapter, $statement);
116123

117124
return $statement;
118125
}
@@ -122,11 +129,16 @@ public function prepareStatementForSqlObject(
122129
*/
123130
public function buildSqlString(SqlInterface $sqlObject, ?AdapterInterface $adapter = null): string
124131
{
125-
return $this
126-
->sqlPlatform
127-
->setSubject($sqlObject)
128-
->getSqlString(
129-
$adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform()
132+
if (! $this->sqlPlatform instanceof SqlInterface) {
133+
throw new Exception\RuntimeException(
134+
'The subject does not implement SqlInterface'
130135
);
136+
}
137+
138+
$this->sqlPlatform->setSubject($sqlObject);
139+
140+
return $this->sqlPlatform->getSqlString(
141+
$adapter instanceof AdapterInterface ? $adapter->getPlatform() : $this->adapter->getPlatform()
142+
);
131143
}
132144
}

test/unit/RowGateway/AbstractRowGatewayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use PhpDb\Adapter\Driver\DriverInterface;
1111
use PhpDb\Adapter\Driver\ResultInterface;
1212
use PhpDb\Adapter\Driver\StatementInterface;
13-
use PhpDb\Adapter\Platform\PlatformInterface;
1413
use PhpDb\RowGateway\AbstractRowGateway;
1514
use PhpDb\RowGateway\Exception\InvalidArgumentException;
1615
use PhpDb\RowGateway\Exception\RuntimeException;
1716
use PhpDb\RowGateway\Feature\FeatureSet;
1817
use PhpDb\RowGateway\RowGateway;
1918
use PhpDb\Sql\Select;
2019
use PhpDb\Sql\Sql;
20+
use PhpDbTest\TestAsset\TrustingSql92Platform;
2121
use PHPUnit\Framework\Attributes\CoversMethod;
2222
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
2323
use PHPUnit\Framework\Attributes\RequiresPhp;
@@ -77,7 +77,7 @@ protected function setUp(): void
7777
->setConstructorArgs(
7878
[
7979
$mockDriver,
80-
$this->getMockBuilder(PlatformInterface::class)->getMock(),
80+
new TrustingSql92Platform(),
8181
]
8282
)->getMock();
8383

0 commit comments

Comments
 (0)