Skip to content

Commit 05d2238

Browse files
committed
Create PHP-specific versions of FakePdo
1 parent d9e8122 commit 05d2238

29 files changed

+200
-130
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ PHP MySQL Engine works by providing a subclass of [PDO](https://www.php.net/manu
6060
You can instantiate the subclass as you would `PDO`, and use dependency injection or similar to provide that instance to your application code.
6161

6262
```php
63-
$pdo = new \Vimeo\MysqlEngine\FakePdo($dsn, $user, $password);
63+
// use a class specific to your cuurrent PHP version (APIs changed in major versions)
64+
$pdo = new \Vimeo\MysqlEngine\Php8\FakePdo($dsn, $user, $password);
6465
// currently supported attributes
6566
$pdo->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_LOWER);
6667
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);

psalm-baseline.xml

+21-21
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,7 @@
2121
<code>$value</code>
2222
</MixedAssignment>
2323
</file>
24-
<file src="src/FakePdo.php">
25-
<MissingParamType occurrences="4">
26-
<code>$key</code>
27-
<code>$options</code>
28-
<code>$seqname</code>
29-
<code>$value</code>
30-
</MissingParamType>
31-
<MixedArgument occurrences="2">
32-
<code>$key</code>
33-
<code>$seqname</code>
34-
</MixedArgument>
35-
<PossiblyNullArgument occurrences="1">
36-
<code>$host</code>
37-
</PossiblyNullArgument>
38-
</file>
39-
<file src="src/FakePdoTrait.php">
24+
<file src="src/FakePdoStatementTrait.php">
4025
<ArgumentTypeCoercion occurrences="1">
4126
<code>$class</code>
4227
</ArgumentTypeCoercion>
@@ -76,12 +61,26 @@
7661
<code>$ctor_args</code>
7762
<code>$fetch_argument</code>
7863
<code>$fetch_argument</code>
79-
<code>$this-&gt;conn-&gt;databaseName</code>
80-
<code>$this-&gt;conn-&gt;databaseName</code>
81-
<code>$this-&gt;conn-&gt;databaseName</code>
64+
<code>$this-&gt;conn-&gt;getDatabaseName()</code>
65+
<code>$this-&gt;conn-&gt;getDatabaseName()</code>
66+
<code>$this-&gt;conn-&gt;getDatabaseName()</code>
8267
<code>$this-&gt;fetchConstructorArgs</code>
8368
</PossiblyNullArgument>
8469
</file>
70+
<file src="src/FakePdoTrait.php">
71+
<MissingParamType occurrences="3">
72+
<code>$key</code>
73+
<code>$seqname</code>
74+
<code>$value</code>
75+
</MissingParamType>
76+
<MixedArgument occurrences="2">
77+
<code>$key</code>
78+
<code>$seqname</code>
79+
</MixedArgument>
80+
<PossiblyNullArgument occurrences="1">
81+
<code>$host</code>
82+
</PossiblyNullArgument>
83+
</file>
8584
<file src="src/Parser/CreateTableParser.php">
8685
<ArgumentTypeCoercion occurrences="1">
8786
<code>$these_tokens</code>
@@ -232,8 +231,9 @@
232231
<code>$stmt-&gt;values</code>
233232
<code>$stmt-&gt;values</code>
234233
</MixedArgumentTypeCoercion>
235-
<PossiblyNullArgument occurrences="4">
236-
<code>$conn-&gt;databaseName</code>
234+
<PossiblyNullArgument occurrences="5">
235+
<code>$conn-&gt;getDatabaseName()</code>
236+
<code>$conn-&gt;getDatabaseName()</code>
237237
<code>$stmt-&gt;decimals</code>
238238
<code>$stmt-&gt;length</code>
239239
<code>$stmt-&gt;length</code>

src/DataIntegrity.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ final class DataIntegrity
77
* @return mixed
88
*/
99
protected static function getDefaultValueForColumn(
10-
FakePdo $conn,
10+
FakePdoInterface $conn,
1111
string $php_type,
1212
bool $nullable,
1313
?string $default,
@@ -57,7 +57,7 @@ protected static function getDefaultValueForColumn(
5757
* @return array<string, mixed>
5858
*/
5959
public static function ensureColumnsPresent(
60-
FakePdo $conn,
60+
FakePdoInterface $conn,
6161
array $row,
6262
Schema\TableDefinition $table_definition
6363
) {
@@ -119,7 +119,7 @@ public static function ensureColumnsPresent(
119119
* @return array<string, mixed>
120120
*/
121121
public static function coerceToSchema(
122-
FakePdo $conn,
122+
FakePdoInterface $conn,
123123
array $row,
124124
Schema\TableDefinition $table_definition
125125
) {

src/FakePdoInterface.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Vimeo\MysqlEngine;
3+
4+
interface FakePdoInterface
5+
{
6+
public function getServer(): Server;
7+
8+
public function setLastInsertId(string $last_insert_id): void;
9+
10+
public function getDatabaseName(): ?string;
11+
12+
public function shouldStringifyResult(): bool;
13+
14+
public function shouldLowercaseResultKeys(): bool;
15+
16+
/**
17+
* @param string $seqname
18+
*/
19+
public function lastInsertId($seqname = null) : string;
20+
}

src/FakePdoStatementTrait.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ trait FakePdoStatementTrait
3636
private $fetchConstructorArgs = null;
3737

3838
/**
39-
* @var FakePdo
39+
* @var FakePdoInterface
4040
*/
4141
private $conn;
4242

@@ -55,7 +55,7 @@ trait FakePdoStatementTrait
5555
*/
5656
private $boundValues = [];
5757

58-
public function __construct(FakePdo $conn, string $sql, ?\PDO $real)
58+
public function __construct(FakePdoInterface $conn, string $sql, ?\PDO $real)
5959
{
6060
$this->sql = $sql;
6161
$this->conn = $conn;
@@ -158,7 +158,7 @@ public function universalExecute(?array $params = null)
158158
$real_result = $this->realStatement->fetchAll(\PDO::FETCH_ASSOC);
159159

160160
if ($fake_result) {
161-
if ($this->conn->stringifyResult) {
161+
if ($this->conn->shouldStringifyResult()) {
162162
$fake_result = array_map(
163163
function ($row) {
164164
return self::stringify($row);
@@ -167,7 +167,7 @@ function ($row) {
167167
);
168168
}
169169

170-
if ($this->conn->lowercaseResultKeys) {
170+
if ($this->conn->shouldLowercaseResultKeys()) {
171171
$fake_result = array_map(
172172
function ($row) {
173173
return self::lowercaseKeys($row);
@@ -214,23 +214,23 @@ function ($row) {
214214

215215
case Query\TruncateQuery::class:
216216
$this->conn->getServer()->resetTable(
217-
$this->conn->databaseName,
217+
$this->conn->getDatabaseName(),
218218
$parsed_query->table
219219
);
220220

221221
break;
222222

223223
case Query\DropTableQuery::class:
224224
$this->conn->getServer()->dropTable(
225-
$this->conn->databaseName,
225+
$this->conn->getDatabaseName(),
226226
$parsed_query->table
227227
);
228228

229229
break;
230230

231231
case Query\ShowTablesQuery::class:
232232
if ($this->conn->getServer()->getTable(
233-
$this->conn->databaseName,
233+
$this->conn->getDatabaseName(),
234234
$parsed_query->pattern
235235
)) {
236236
$this->result = [[$parsed_query->sql => $parsed_query->pattern]];
@@ -302,11 +302,11 @@ public function fetch(
302302
return false;
303303
}
304304

305-
if ($this->conn->stringifyResult) {
305+
if ($this->conn->shouldStringifyResult()) {
306306
$row = self::stringify($row);
307307
}
308308

309-
if ($this->conn->lowercaseResultKeys) {
309+
if ($this->conn->shouldLowercaseResultKeys()) {
310310
$row = self::lowercaseKeys($row);
311311
}
312312

@@ -355,11 +355,11 @@ public function universalFetchAll(int $fetch_style = -123, ...$args) : array
355355
if ($fetch_style === \PDO::FETCH_ASSOC) {
356356
return array_map(
357357
function ($row) {
358-
if ($this->conn->stringifyResult) {
358+
if ($this->conn->shouldStringifyResult()) {
359359
$row = self::stringify($row);
360360
}
361361

362-
if ($this->conn->lowercaseResultKeys) {
362+
if ($this->conn->shouldLowercaseResultKeys()) {
363363
$row = self::lowercaseKeys($row);
364364
}
365365

@@ -374,7 +374,7 @@ function ($row) {
374374
if ($fetch_style === \PDO::FETCH_NUM) {
375375
return array_map(
376376
function ($row) {
377-
if ($this->conn->stringifyResult) {
377+
if ($this->conn->shouldStringifyResult()) {
378378
$row = self::stringify($row);
379379
}
380380

@@ -387,11 +387,11 @@ function ($row) {
387387
if ($fetch_style === \PDO::FETCH_BOTH) {
388388
return array_map(
389389
function ($row) {
390-
if ($this->conn->stringifyResult) {
390+
if ($this->conn->shouldStringifyResult()) {
391391
$row = self::stringify($row);
392392
}
393393

394-
if ($this->conn->lowercaseResultKeys) {
394+
if ($this->conn->shouldLowercaseResultKeys()) {
395395
$row = self::lowercaseKeys($row);
396396
}
397397

@@ -405,7 +405,7 @@ function ($row) {
405405
return \array_column(
406406
array_map(
407407
function ($row) {
408-
if ($this->conn->stringifyResult) {
408+
if ($this->conn->shouldStringifyResult()) {
409409
$row = self::stringify($row);
410410
}
411411

@@ -424,11 +424,11 @@ function ($row) {
424424

425425
return array_map(
426426
function ($row) use ($fetch_argument, $ctor_args) {
427-
if ($this->conn->stringifyResult) {
427+
if ($this->conn->shouldStringifyResult()) {
428428
$row = self::stringify($row);
429429
}
430430

431-
if ($this->conn->lowercaseResultKeys) {
431+
if ($this->conn->shouldLowercaseResultKeys()) {
432432
$row = self::lowercaseKeys($row);
433433
}
434434

src/FakePdo.php renamed to src/FakePdoTrait.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Vimeo\MysqlEngine;
33

4-
class FakePdo extends \PDO
4+
trait FakePdoTrait
55
{
66
/**
77
* @var Server
@@ -70,16 +70,24 @@ public function getServer() : Server
7070
return $this->server;
7171
}
7272

73-
/**
74-
* @param string $statement
75-
*/
76-
public function prepare($statement, $options = null)
73+
public function getDatabaseName() : ?string
7774
{
78-
if (\PHP_MAJOR_VERSION === 8) {
79-
return new Php8\FakePdoStatement($this, $statement, $this->real);
80-
}
75+
return $this->databaseName;
76+
}
8177

82-
return new Php7\FakePdoStatement($this, $statement, $this->real);
78+
public function shouldStringifyResult(): bool
79+
{
80+
return $this->stringifyResult;
81+
}
82+
83+
public function shouldLowercaseResultKeys(): bool
84+
{
85+
return $this->lowercaseResultKeys;
86+
}
87+
88+
public function setLastInsertId(string $last_insert_id) : void
89+
{
90+
$this->lastInsertId = $last_insert_id;
8391
}
8492

8593
public function lastInsertId($seqname = null) : string

src/Php7/FakePdo.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Vimeo\MysqlEngine\Php7;
3+
4+
class FakePdo extends \PDO implements \Vimeo\MysqlEngine\FakePdoInterface
5+
{
6+
use \Vimeo\MysqlEngine\FakePdoTrait;
7+
8+
/**
9+
* @param string $statement
10+
* @param ?array $options
11+
*/
12+
public function prepare($statement, $options = null)
13+
{
14+
return new FakePdoStatement($this, $statement, $this->real);
15+
}
16+
}

src/Php8/FakePdo.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Vimeo\MysqlEngine\Php8;
3+
4+
class FakePdo extends \PDO implements \Vimeo\MysqlEngine\FakePdoInterface
5+
{
6+
use \Vimeo\MysqlEngine\FakePdoTrait;
7+
8+
/**
9+
* @param string $statement
10+
* @param ?array $options
11+
*/
12+
public function prepare($statement, $options = null)
13+
{
14+
return new FakePdoStatement($this, $statement, $this->real);
15+
}
16+
}

src/Processor/CreateProcessor.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class CreateProcessor
1010
{
1111
public static function process(
12-
\Vimeo\MysqlEngine\FakePdo $conn,
12+
\Vimeo\MysqlEngine\FakePdoInterface $conn,
1313
Query\CreateQuery $stmt
1414
) : void {
1515
$definition_columns = [];
@@ -78,7 +78,7 @@ function ($col) {
7878

7979
$definition = new TableDefinition(
8080
$stmt->name,
81-
$conn->databaseName,
81+
$conn->getDatabaseName(),
8282
$definition_columns,
8383
$default_character_set,
8484
$default_collation,
@@ -88,7 +88,7 @@ function ($col) {
8888
);
8989

9090
$conn->getServer()->addTableDefinition(
91-
$conn->databaseName,
91+
$conn->getDatabaseName(),
9292
$stmt->name,
9393
$definition
9494
);

src/Processor/DeleteProcessor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class DeleteProcessor extends Processor
88
/**
99
* @return int
1010
*/
11-
public static function process(\Vimeo\MysqlEngine\FakePdo $conn, Scope $scope, DeleteQuery $stmt)
11+
public static function process(\Vimeo\MysqlEngine\FakePdoInterface $conn, Scope $scope, DeleteQuery $stmt)
1212
{
1313
($__tmp1__ = $stmt->fromClause) !== null ? $__tmp1__ : (function () {
1414
throw new \TypeError('Failed assertion');
@@ -57,7 +57,7 @@ public static function process(\Vimeo\MysqlEngine\FakePdo $conn, Scope $scope, D
5757
* @return int
5858
*/
5959
protected static function applyDelete(
60-
\Vimeo\MysqlEngine\FakePdo $conn,
60+
\Vimeo\MysqlEngine\FakePdoInterface $conn,
6161
string $database,
6262
string $table_name,
6363
array $filtered_rows,

src/Processor/Expression/BetweenOperatorEvaluator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class BetweenOperatorEvaluator
1212
* @param array<string, mixed> $row
1313
*/
1414
public static function evaluate(
15-
\Vimeo\MysqlEngine\FakePdo $conn,
15+
\Vimeo\MysqlEngine\FakePdoInterface $conn,
1616
Scope $scope,
1717
BetweenOperatorExpression $expr,
1818
array $row,

0 commit comments

Comments
 (0)