Skip to content

Commit c45fc62

Browse files
committed
added custom types class; corrections after review
1 parent 72ca57f commit c45fc62

File tree

5 files changed

+91
-51
lines changed

5 files changed

+91
-51
lines changed

src/Connection.php

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,10 @@
2525
* @method string quote(mixed $string, int $parameterType = PDO::PARAM_STR)
2626
* @method mixed setAttribute(int $attribute, mixed $value)
2727
*
28-
* @phpstan-type dsnArgs array{
29-
* dsn: string,
30-
* username?: string,
31-
* password?: string,
32-
* options?: array<int, mixed>
33-
* }
34-
*
35-
* @phpstan-type logEntryType array{
36-
* start: float,
37-
* finish: ?float,
38-
* duration: ?float,
39-
* performed: ?bool,
40-
* statement: ?string,
41-
* values: array<array-key, mixed>,
42-
* trace: ?string,
43-
* connection?: string
44-
* }
28+
* @phpstan-import-type dsn_args_array from PdoCustomTypes
29+
* @phpstan-import-type log_entry_array from PdoCustomTypes
30+
* @phpstan-import-type fetch_assoc_array from PdoCustomTypes
31+
* @phpstan-import-type fetch_string_mixed_array from PdoCustomTypes
4532
*/
4633
class Connection
4734
{
@@ -51,7 +38,7 @@ static public function new(mixed ...$args) : Connection
5138
return new static($args[0]);
5239
}
5340

54-
/** @var dsnArgs $args */
41+
/** @var dsn_args_array $args */
5542
return new static(new PDO(...$args));
5643
}
5744

@@ -75,7 +62,7 @@ static public function factory(mixed ...$args) : callable
7562

7663
public function __construct(protected PDO $pdo)
7764
{
78-
$this->persistent = (bool)$this->pdo->getAttribute(PDO::ATTR_PERSISTENT);
65+
$this->persistent = (bool) $this->pdo->getAttribute(PDO::ATTR_PERSISTENT);
7966
}
8067

8168
public function __call(
@@ -153,7 +140,7 @@ public function prepare(
153140
$sth = PersistentLoggedStatement::new(
154141
$sth,
155142
function (array $entry) : void {
156-
/** @var logEntryType $entry */
143+
/** @var log_entry_array $entry */
157144
$this->addLogEntry($entry);
158145
},
159146
$this->newLogEntry()
@@ -178,6 +165,13 @@ public function perform(
178165
return $sth;
179166
}
180167

168+
/**
169+
* @param PDOStatement $sth
170+
* @param int|string $name
171+
* @param mixed $args
172+
*
173+
* @return void
174+
*/
181175
protected function performBind(
182176
PDOStatement $sth,
183177
mixed $name,
@@ -190,7 +184,6 @@ protected function performBind(
190184
}
191185

192186
if (! is_array($args)) {
193-
/** @var int|string $name */
194187
$sth->bindValue($name, $args);
195188
return;
196189
}
@@ -201,7 +194,6 @@ protected function performBind(
201194
$args[0] = $args[0] ? '1' : '0';
202195
}
203196

204-
/** @var int|string $name */
205197
$sth->bindValue($name, ...$args);
206198
}
207199

@@ -286,10 +278,10 @@ public function fetchObject(
286278
/**
287279
* @template T of object
288280
* *
289-
* @param string $statement
290-
* @param array $values
291-
* @param class-string<T> $class
292-
* @param array<mixed> ...$args
281+
* @param string $statement
282+
* @param array $values
283+
* @param class-string<T> $class
284+
* @param callable|int|string ...$args
293285
*
294286
* @return array|false
295287
*/
@@ -301,17 +293,22 @@ public function fetchObjects(
301293
) : array|false
302294
{
303295
$sth = $this->perform($statement, $values);
304-
/** @var array<array-key, callable|int|string> $args */
305296
return $sth->fetchAll(PDO::FETCH_CLASS, $class, ...$args);
306297
}
307298

299+
/**
300+
* @param string $statement
301+
* @param array $values
302+
*
303+
* @return fetch_assoc_array|false
304+
*/
308305
public function fetchOne(
309306
string $statement,
310307
array $values = []
311308
) : array|false
312309
{
313310
$sth = $this->perform($statement, $values);
314-
/** @var array<array-key, mixed> $result */
311+
/** @var fetch_assoc_array $result */
315312
$result = $sth->fetch(PDO::FETCH_ASSOC);
316313

317314
return $result;
@@ -358,7 +355,7 @@ public function yieldUnique(
358355
$sth = $this->perform($statement, $values);
359356

360357
while ($row = $sth->fetch(PDO::FETCH_UNIQUE)) {
361-
/** @var array<string, mixed> $row */
358+
/** @var fetch_string_mixed_array $row */
362359
$key = array_shift($row);
363360
yield $key => $row;
364361
}
@@ -437,7 +434,7 @@ public function logQueries(bool $logQueries = true) : void
437434
LoggedStatement::CLASS,
438435
[
439436
function (array $entry) : void {
440-
/** @var logEntryType $entry */
437+
/** @var log_entry_array $entry */
441438
$this->addLogEntry($entry);
442439
},
443440
$this->newLogEntry()
@@ -458,7 +455,7 @@ public function setQueryLogger(callable $queryLogger) : void
458455
/**
459456
* @param string|null $statement
460457
*
461-
* @return logEntryType
458+
* @return log_entry_array
462459
*/
463460
protected function newLogEntry(?string $statement = null) : array
464461
{
@@ -474,7 +471,7 @@ protected function newLogEntry(?string $statement = null) : array
474471
}
475472

476473
/**
477-
* @param logEntryType $entry
474+
* @param log_entry_array $entry
478475
*
479476
* @return void
480477
*/

src/ConnectionLocator.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111
namespace Atlas\Pdo;
1212

1313
/**
14-
* @phpstan-type connectionStore array{
15-
* DEFAULT: ?Connection,
16-
* READ: array<string, ?Connection>,
17-
* WRITE: array<string, ?Connection>
18-
* }
19-
* @phpstan-import-type logEntryType from Connection
14+
* @phpstan-import-type connection_store_array from PdoCustomTypes
15+
* @phpstan-import-type log_entry_array from PdoCustomTypes
2016
*/
2117
class ConnectionLocator
2218
{
@@ -49,7 +45,7 @@ static public function new(mixed $arg, mixed ...$args) : static
4945
}
5046

5147
/**
52-
* @var connectionStore
48+
* @var connection_store_array
5349
*/
5450
protected array $instances = [
5551
self::DEFAULT => null,
@@ -66,7 +62,7 @@ static public function new(mixed $arg, mixed ...$args) : static
6662
protected bool $logQueries = false;
6763

6864
/**
69-
* @var logEntryType[]
65+
* @var log_entry_array[]
7066
*/
7167
protected array $queries = [];
7268

@@ -205,7 +201,7 @@ protected function newConnection(
205201
$connection = $factory();
206202

207203
$queryLogger = function (array $entry) use ($label) : void {
208-
/** @var logEntryType $entry */
204+
/** @var log_entry_array $entry */
209205
$entry = ['connection' => $label] + $entry;
210206
$this->addLogEntry($entry);
211207
};
@@ -258,7 +254,7 @@ public function logQueries(bool $logQueries = true) : void
258254
}
259255

260256
/**
261-
* @return logEntryType[]
257+
* @return log_entry_array[]
262258
*/
263259
public function getQueries() : array
264260
{
@@ -271,7 +267,7 @@ public function setQueryLogger(callable $queryLogger) : void
271267
}
272268

273269
/**
274-
* @param logEntryType $entry
270+
* @param log_entry_array $entry
275271
*
276272
* @return void
277273
*/

src/LoggedStatement.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use PDOStatement;
1515

1616
/**
17-
* @phpstan-import-type logEntryType from Connection
17+
* @phpstan-import-type log_entry_array from PdoCustomTypes
1818
*/
1919
class LoggedStatement extends PDOStatement
2020
{
2121
/**
22-
* @param callable $queryLogger
23-
* @param logEntryType $logEntry
22+
* @param callable $queryLogger
23+
* @param log_entry_array $logEntry
2424
*/
2525
protected function __construct(
2626
protected mixed $queryLogger,

src/PdoCustomTypes.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of Atlas for PHP.
5+
*
6+
* @license https://opensource.org/licenses/MIT MIT
7+
*
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace Atlas\Pdo;
12+
13+
/**
14+
* @phpstan-type connection_entry array<string, ?Connection>
15+
*
16+
* @phpstan-type connection_store_array array{
17+
* DEFAULT: ?Connection,
18+
* READ: connection_entry,
19+
* WRITE: connection_entry
20+
* }
21+
*
22+
* @phpstan-type dsn_args_array array{
23+
* dsn: string,
24+
* username?: string,
25+
* password?: string,
26+
* options?: array<int, mixed>
27+
* }
28+
*
29+
* @phpstan-type log_entry_array array{
30+
* start: float,
31+
* finish: ?float,
32+
* duration: ?float,
33+
* performed: ?bool,
34+
* statement: ?string,
35+
* values: array<array-key, mixed>,
36+
* trace: ?string,
37+
* connection?: string
38+
* }
39+
*
40+
* @phpstan-type fetch_assoc_array mixed[]
41+
*
42+
* @phpstan-type fetch_string_mixed_array array<string, mixed>
43+
*
44+
*/
45+
final class PdoCustomTypes
46+
{
47+
}

src/PersistentLoggedStatement.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
use ReturnTypeWillChange;
1717

1818
/**
19-
* @phpstan-import-type logEntryType from Connection
19+
* @phpstan-import-type log_entry_array from PdoCustomTypes
2020
*/
2121
class PersistentLoggedStatement extends PDOStatement
2222
{
2323
/**
24-
* @param PDOStatement $parent
25-
* @param callable $queryLogger
26-
* @param logEntryType $logEntry
24+
* @param PDOStatement $parent
25+
* @param callable $queryLogger
26+
* @param log_entry_array $logEntry
2727
*
2828
* @return static
2929
*/
@@ -46,7 +46,7 @@ static public function new(
4646
private mixed /* callable */ $queryLogger;
4747

4848
/**
49-
* @var logEntryType
49+
* @var log_entry_array
5050
*/
5151
private array $logEntry;
5252

0 commit comments

Comments
 (0)