Skip to content

Commit 958cd15

Browse files
committed
Provide constructor defaults
Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent eece5b4 commit 958cd15

5 files changed

Lines changed: 56 additions & 13 deletions

File tree

phpstan-baseline.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: '#^Callable PhpDb\\Sqlite\\Container\\PdoStatementFactory invoked with 1 parameter, 2\-3 required\.$#'
5+
identifier: arguments.count
6+
count: 1
7+
path: test/integration/Container/PdoStatementFactoryTest.php
8+
9+
-
10+
message: '#^Callable PhpDb\\Sqlite\\Container\\PdoStatementFactory invoked with 1 parameter, 2\-3 required\.$#'
11+
identifier: arguments.count
12+
count: 3
13+
path: test/unit/Container/PdoStatementFactoryTest.php

src/Container/PdoStatementFactory.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@
1010

1111
final class PdoStatementFactory
1212
{
13-
public function __invoke(ContainerInterface $container): StatementInterface&Statement
14-
{
15-
/** @var array $config */
16-
$config = $container->get('config');
17-
18-
/** @var array $dbConfig */
19-
$dbConfig = $config['db'] ?? [];
20-
21-
/** @var array $options */
22-
$options = $dbConfig['options'] ?? [];
23-
13+
public function __invoke(
14+
ContainerInterface $container,
15+
string $requestedName,
16+
?array $options = null
17+
): StatementInterface&Statement {
2418
return new Statement(options: $options);
2519
}
2620
}

src/Pdo/Connection.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use function array_diff_key;
1616
use function assert;
17+
use function is_array;
1718
use function is_int;
1819
use function is_string;
1920
use function str_starts_with;
@@ -23,6 +24,16 @@ class Connection extends AbstractPdoConnection
2324
{
2425
public final const CURRENT_SCHEMA = 'main';
2526

27+
public function __construct(
28+
PDO|array $connectionParameters
29+
) {
30+
if (is_array($connectionParameters)) {
31+
$this->setConnectionParameters($connectionParameters);
32+
} elseif ($connectionParameters instanceof PDO) {
33+
$this->setResource($connectionParameters);
34+
}
35+
}
36+
2637
/**
2738
* {@inheritDoc}
2839
*/

src/Pdo/Driver.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,46 @@
55
namespace PhpDb\Sqlite\Pdo;
66

77
use Override;
8+
use PDO;
89
use PDOStatement;
910
use PhpDb\Adapter\Driver\Feature\DriverFeatureProviderInterface;
1011
use PhpDb\Adapter\Driver\Feature\DriverFeatureProviderTrait;
1112
use PhpDb\Adapter\Driver\Pdo\AbstractPdo;
1213
use PhpDb\Adapter\Driver\Pdo\Result;
1314
use PhpDb\Adapter\Driver\Pdo\Statement;
15+
use PhpDb\Adapter\Driver\PdoConnectionInterface;
16+
use PhpDb\Adapter\Driver\PdoDriverAwareInterface;
1417
use PhpDb\Adapter\Driver\ResultInterface;
18+
use PhpDb\Adapter\Driver\StatementInterface;
1519
use PhpDb\Sqlite\DatabasePlatformNameTrait;
1620

1721
class Driver extends AbstractPdo implements DriverFeatureProviderInterface
1822
{
1923
use DatabasePlatformNameTrait;
2024
use DriverFeatureProviderTrait;
2125

26+
public function __construct(
27+
(PdoConnectionInterface&PdoDriverAwareInterface)|PDO $connection,
28+
(StatementInterface&PdoDriverAwareInterface)|Statement $statementPrototype = new Statement(),
29+
ResultInterface $resultPrototype = new Result(),
30+
array $features = [],
31+
) {
32+
$this->connection = $connection;
33+
$this->statementPrototype = $statementPrototype;
34+
$this->resultPrototype = $resultPrototype;
35+
36+
if (! $this->connection instanceof PDO) {
37+
$this->connection->setDriver($this);
38+
}
39+
40+
$this->statementPrototype->setDriver($this);
41+
42+
// $features is not constructor promoted because $this->features is defined in the trait
43+
if ($features !== []) {
44+
$this->addFeatures($features);
45+
}
46+
}
47+
2248
/**
2349
* @param PDOStatement|resource $resource
2450
*/

test/unit/Container/PdoStatementFactoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpDbTest\Sqlite\Container;
66

7+
use PhpDb\Adapter\AdapterInterface;
78
use PhpDb\Adapter\Driver\Pdo\Statement;
89
use PhpDb\Sqlite\Container\PdoStatementFactory;
910
use PHPUnit\Framework\Attributes\CoversClass;
@@ -19,7 +20,7 @@ public function testInvokeReturnsStatement(): void
1920
$containerMock->method('get')
2021
->with('config')
2122
->willReturn([
22-
'db' => [
23+
AdapterInterface::class => [
2324
'options' => [],
2425
],
2526
]);
@@ -36,7 +37,7 @@ public function testInvokeWithoutOptionsConfig(): void
3637
$containerMock->method('get')
3738
->with('config')
3839
->willReturn([
39-
'db' => [],
40+
AdapterInterface::class => [],
4041
]);
4142

4243
$factory = new PdoStatementFactory();

0 commit comments

Comments
 (0)