Skip to content

Commit ca07f33

Browse files
authored
Merge pull request #14 from BaguettePHP/feature/callback-processor
Impl CallbackProcessor
2 parents d130f35 + 678c23e commit ca07f33

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

.phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ parameters:
1010
count: 2
1111
path: src/AbstractStaticQuery.php
1212

13+
-
14+
message: "#^Method Teto\\\\SQL\\\\Processor\\\\CallbackProcessor\\:\\:processQuery\\(\\) should return string but returns mixed\\.$#"
15+
count: 1
16+
path: src/Processor/CallbackProcessor.php
17+
18+
-
19+
message: "#^Property Teto\\\\SQL\\\\Processor\\\\CallbackProcessor\\:\\:\\$callback with generic interface Teto\\\\SQL\\\\PDOInterface does not specify its types\\: T$#"
20+
count: 1
21+
path: src/Processor/CallbackProcessor.php
22+
1323
-
1424
message: "#^Parameter \\#2 \\$callback of function preg_replace_callback expects callable\\(array\\<int\\|string, string\\>\\)\\: string, Closure\\(array\\)\\: int\\|string given\\.$#"
1525
count: 1

src/PDOInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* - Part of the variable name is substituted.
1717
*
1818
* @template T of \PDOStatement|PDOStatementInterface
19+
* @phpstan-type teto_pdo \PDO|PDOInterface
20+
* @phpstan-type teto_pdo_statement \PDOStatement|PDOStatementInterface
1921
*/
2022
interface PDOInterface
2123
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Teto\SQL\Processor;
4+
5+
use Closure;
6+
use Teto\SQL\ProcessorInterface;
7+
8+
/**
9+
* Define a processor using Closure
10+
*
11+
* @phpstan-import-type teto_pdo from \Teto\SQL\PDOInterface
12+
*/
13+
class CallbackProcessor implements ProcessorInterface
14+
{
15+
/**
16+
* @var Closure
17+
* @phpstan-var Closure(teto_pdo, string, array<non-empty-string,mixed>, array<non-empty-string,mixed>): string
18+
*/
19+
protected $callback;
20+
21+
public function __construct(Closure $callback)
22+
{
23+
$this->callback = $callback;
24+
}
25+
26+
public function processQuery($pdo, $sql, array $params, array &$bind_values)
27+
{
28+
return call_user_func_array($this->callback, [$pdo, $sql, $params, &$bind_values]);
29+
}
30+
}

src/StaticQueryExecuteTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function executeAndReturnInsertId($pdo, $sql, array $params, $name
5959
$stmt = static::build($pdo, $sql, $params);
6060
$stmt->execute();
6161

62-
$id = $pdo->lastInsertId($name);
62+
$id = ($name === null) ? $pdo->lastInsertId() : $pdo->lastInsertId($name);
6363
assert($id !== false);
6464

6565
return $id;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Teto\SQL\Processor;
4+
5+
use Teto\SQL\DummyPDO;
6+
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
7+
8+
class CallbackProcessorTest extends TestCase
9+
{
10+
/**
11+
* @return void
12+
*/
13+
public function test()
14+
{
15+
$pdo = new DummyPDO();
16+
17+
$bind_values = [];
18+
19+
$called = false;
20+
$subject = new CallbackProcessor(function ($pdo, $sql, array $params, array &$bind_values) use (&$called) {
21+
assert($params === ['foo' => 'bar', 'buz' => 'buz']);
22+
$called = true;
23+
$bind_values = $params;
24+
$bind_values[] = 'Bound!';
25+
return 'Closure called!';
26+
});
27+
28+
$params = ['foo' => 'bar', 'buz' => 'buz'];
29+
$actual = $subject->processQuery($pdo, 'before query', $params, $bind_values);
30+
31+
$this->assertTrue($called);
32+
$this->assertEquals($bind_values, ['foo' => 'bar', 'buz' => 'buz', 'Bound!']);
33+
$this->assertSame($actual, 'Closure called!');
34+
}
35+
}

0 commit comments

Comments
 (0)