Skip to content

Commit 7d8e7e5

Browse files
committed
Various PHP 8 related changes
1 parent a69c571 commit 7d8e7e5

File tree

8 files changed

+25
-35
lines changed

8 files changed

+25
-35
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
!/.travis.yml
99
!/.scrutinizer.yml
1010
!/composer.json
11+
!/INSTRUCTIONS.md
1112
!/phpstan.neon
1213
!/README.md
1314
!/rector.php

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 6
2+
level: 7
33
paths:
44
- src
55
- tests

src/Databases/MySQL.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(PDO $pdo, array $options = []) {
6565
'delete-options' => [],
6666
];
6767
$this->options = array_merge($defaultOptions, $options);
68-
$this->options['timezone'] = $this->options['timezone'] ?? date_default_timezone_get();
68+
$this->options['timezone'] ??= date_default_timezone_get();
6969
if(!($this->options['timezone'] instanceof DateTimeZone)) {
7070
$this->options['timezone'] = new DateTimeZone((string) $this->options['timezone']);
7171
}

src/Databases/MySQL/MySQLQuoter.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
<?php
22
namespace Kir\MySQL\Databases\MySQL;
33

4+
use DateTimeImmutable;
45
use DateTimeInterface;
56
use DateTimeZone;
67
use Kir\MySQL\Builder\DBExpr;
78
use Kir\MySQL\Builder\Select;
89
use PDO;
910

1011
class MySQLQuoter {
11-
/** @var PDO */
12-
private $pdo;
13-
/** @var DateTimeZone */
14-
private $timeZone;
15-
16-
public function __construct(PDO $pdo, DateTimeZone $timeZone) {
17-
$this->timeZone = $timeZone;
18-
$this->pdo = $pdo;
19-
}
12+
public function __construct(
13+
private PDO $pdo,
14+
private DateTimeZone $timeZone
15+
) {}
2016

2117
/**
2218
* @param null|scalar|array<int, null|scalar>|DBExpr|Select|DateTimeInterface $value
@@ -48,7 +44,7 @@ public function quote($value): string {
4844
}
4945

5046
if($value instanceof DateTimeInterface) {
51-
$value = date_create_immutable($value->format('c'))->setTimezone($this->timeZone)->format('Y-m-d H:i:s');
47+
$value = (new DateTimeImmutable($value->format('c')))->setTimezone($this->timeZone)->format('Y-m-d H:i:s');
5248
}
5349

5450
return $this->pdo->quote($value);

src/Databases/MySQL/MySQLRunnableSelect.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
*/
1818
class MySQLRunnableSelect extends MySQLSelect {
1919
/** @var array<string, mixed> */
20-
private $values = [];
21-
/** @var bool */
22-
private $preserveTypes;
23-
/** @var string */
24-
private $defaultClassName;
25-
/** @var int */
26-
private $foundRows = 0;
20+
private array $values = [];
21+
private bool $preserveTypes;
22+
private string $defaultClassName;
23+
private int $foundRows = 0;
2724

2825
/**
2926
* @param MySQL $db
@@ -245,19 +242,18 @@ private function createStatement(): QueryStatement {
245242
}
246243

247244
/**
248-
* @return Traversable<int, array<string, mixed>>
245+
* @return Traversable<int, array<string, bool|float|int|string|null>>
249246
*/
250247
public function getIterator(): Traversable {
251248
yield from $this->fetchRowsLazy();
252249
}
253250

254251
/**
255-
* @template TFnParamType of array<string, null|scalar>
256252
* @template TFnReturnType of array<string, null|scalar>
257-
* @param null|callable(TFnParamType): (TFnReturnType|DBIgnoreRow|null|void) $callback
253+
* @param null|(callable(array<string, null|scalar>): (TFnReturnType|DBIgnoreRow|null|void)) $callback
258254
* @param int $mode
259255
* @param mixed $arg0
260-
* @return ($callback is null ? array<int, ($mode is PDO::FETCH_NUM ? array<int, null|scalar> : array<string, null|scalar>)> : array<int, TFnReturnType|TFnParamType>)
256+
* @return ($callback is null ? array<int, ($mode is PDO::FETCH_NUM ? array<int, null|scalar> : array<string, null|scalar>)> : array<int, TFnReturnType>)
261257
*/
262258
private function fetchAll($callback = null, int $mode = 0, $arg0 = null) {
263259
return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0) {
@@ -283,7 +279,6 @@ private function fetchAll($callback = null, int $mode = 0, $arg0 = null) {
283279
return $resultData;
284280
});
285281
}
286-
return $data;
287282
});
288283
}
289284

src/Databases/MySQL/MySQLSelect.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ abstract class MySQLSelect extends Statement implements RunnableSelect {
3232
use UnionBuilder;
3333

3434
/** @var array<int|string, string> */
35-
private $fields = [];
36-
/** @var bool */
37-
private $calcFoundRows = false;
38-
/** @var bool */
39-
private $forUpdate = false;
40-
/** @var bool */
41-
private $distinct = false;
35+
private array $fields = [];
36+
private bool $calcFoundRows = false;
37+
private bool $forUpdate = false;
38+
private bool $distinct = false;
4239

4340
/**
4441
* @param bool $distinct

src/QueryLogger/ClosureQueryLogger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ClosureQueryLogger implements QueryLogger {
88
private $fn;
99

1010
/**
11-
* @param callable(string, float, string, Throwable|null): void $fn
11+
* @param callable(string, float, string, Throwable|null):void $fn
1212
*/
1313
public function __construct(callable $fn) {
1414
$this->fn = $fn;

tests/Databases/MySQLTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Kir\MySQL\Common\DBTestCase;
66
use Kir\MySQL\QueryLogger\ClosureQueryLogger;
77
use PDOException;
8+
use Throwable;
89
use RuntimeException;
910

1011
class MySQLTest extends DBTestCase {
@@ -182,7 +183,7 @@ public function testInfoLoggingFromQuery(): void {
182183
public function testErrorLoggingFromQuery(): void {
183184
$log = (object) ['queries' => []];
184185
$db = $this->getDB();
185-
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?PDOException $e) use ($log) {
186+
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?Throwable $e) use ($log) {
186187
$log->queries[] = ['query' => $query, 'durection' => $duration, 'exception' => $e];
187188
}));
188189
$query = 'SELECT COUNT(*) FROM test1_';
@@ -210,7 +211,7 @@ public function testInfoLoggingFromExec(): void {
210211
public function testErrorLoggingFromExec(): void {
211212
$log = (object) ['queries' => []];
212213
$db = $this->getDB();
213-
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?PDOException $e) use ($log) {
214+
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?Throwable $e) use ($log) {
214215
$log->queries[] = ['query' => $query, 'durection' => $duration, 'exception' => $e];
215216
}));
216217
$query = 'UPDATE x SET y=1';
@@ -238,7 +239,7 @@ public function testInfoLoggingFromGetTableFields(): void {
238239
public function testErrorLoggingFromGetTableFields(): void {
239240
$log = (object) ['queries' => []];
240241
$db = $this->getDB();
241-
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?PDOException $e) use ($log) {
242+
$db->getQueryLoggers()->add(new ClosureQueryLogger(function (string $query, float $duration, string $severity, ?Throwable $e) use ($log) {
242243
$log->queries[] = ['query' => $query, 'durection' => $duration, 'exception' => $e];
243244
}));
244245
$query = 'DESCRIBE test1_';

0 commit comments

Comments
 (0)