Skip to content

Commit 8ae3342

Browse files
committed
PHP 8.4 specific changes
1 parent 7d8e7e5 commit 8ae3342

File tree

7 files changed

+28
-22
lines changed

7 files changed

+28
-22
lines changed

src/Builder/Expr/OptionalDBFilterMap.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class OptionalDBFilterMap {
55
/** @var array<string, mixed> */
6-
private $map;
6+
private array $map;
77

88
/**
99
* @param array<string, mixed> $map
@@ -33,7 +33,7 @@ protected function getMap(): array {
3333
* @param null|callable(mixed): bool $validator
3434
* @return DBExprFilter
3535
*/
36-
public function __invoke(string $expression, $keyPath, callable $validator = null): DBExprFilter {
36+
public function __invoke(string $expression, $keyPath, ?callable $validator = null): DBExprFilter {
3737
return new DBExprFilter($expression, $this->map, $keyPath, $validator);
3838
}
3939
}

src/Builder/Insert.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function addOrUpdateExpr(string $expr, ...$args) {
133133
* @param array<int, string>|null $excludeFields
134134
* @return $this
135135
*/
136-
public function addAll(array $data, array $mask = null, array $excludeFields = null) {
136+
public function addAll(array $data, ?array $mask = null, ?array $excludeFields = null) {
137137
$this->addAllTo($data, $mask, $excludeFields, function ($field, $value) {
138138
$this->add($field, $value);
139139
});
@@ -146,7 +146,7 @@ public function addAll(array $data, array $mask = null, array $excludeFields = n
146146
* @param array<int, string>|null $excludeFields
147147
* @return $this
148148
*/
149-
public function updateAll(array $data, array $mask = null, array $excludeFields = null) {
149+
public function updateAll(array $data, ?array $mask = null, ?array $excludeFields = null) {
150150
$this->addAllTo($data, $mask, $excludeFields, function ($field, $value) {
151151
if($field !== $this->keyField) {
152152
$this->update($field, $value);
@@ -161,7 +161,7 @@ public function updateAll(array $data, array $mask = null, array $excludeFields
161161
* @param array<int, string>|null $excludeFields
162162
* @return $this
163163
*/
164-
public function addOrUpdateAll(array $data, array $mask = null, array $excludeFields = null) {
164+
public function addOrUpdateAll(array $data, ?array $mask = null, ?array $excludeFields = null) {
165165
$this->addAll($data, $mask, $excludeFields);
166166
$this->updateAll($data, $mask, $excludeFields);
167167
return $this;
@@ -177,10 +177,10 @@ public function from(Select $select) {
177177
}
178178

179179
/**
180-
* @param array<int, array<string, mixed>>|Traversable<int, array<string, mixed>> $rows
180+
* @param iterable<int, array<string, mixed>>|Traversable<int, array<string, mixed>> $rows
181181
* @return int[] Insert IDs
182182
*/
183-
abstract public function insertRows($rows);
183+
abstract public function insertRows(iterable $rows);
184184

185185
/**
186186
* @param array<string, mixed> $params
@@ -226,7 +226,7 @@ public function __toString(): string {
226226
/**
227227
* @param array<string|int, mixed> $fields
228228
* @param string $field
229-
* @param null|bool|int|float|string $value
229+
* @param null|bool|int|float|string|DateTimeInterface $value
230230
* @return array<string|int, mixed>
231231
*/
232232
private function addTo(array $fields, string $field, $value): array {

src/Builder/RunnableInsert.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class RunnableInsert extends Insert implements DDLPreparable {
1616
/**
1717
* @inheritDoc
1818
*/
19-
public function insertRows($rows) {
19+
public function insertRows(iterable $rows) {
2020
if (!is_iterable($rows)) {
2121
throw new BadMethodCallException('Expected $rows to by an iterable');
2222
}

src/Builder/Update.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class Update extends InsertUpdateStatement {
2121
use OffsetBuilder;
2222

2323
/** @var mixed[] */
24-
private $fields = [];
24+
private array $fields = [];
2525

2626
/**
2727
* @param string $alias
@@ -77,7 +77,7 @@ public function setExpr(string $expr, ...$args): self {
7777
* @param array<int, string>|null $allowedFields
7878
* @return $this
7979
*/
80-
public function setAll(array $data, array $allowedFields = null): self {
80+
public function setAll(array $data, ?array $allowedFields = null): self {
8181
if($allowedFields !== null) {
8282
foreach($data as $fieldName => $value) {
8383
if(in_array($fieldName, $allowedFields)) {

src/Database.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function exec(
5050

5151
/**
5252
* @param string|null $name
53-
* @return string
53+
* @return string|null
5454
*/
55-
public function getLastInsertId(?string $name = null): string;
55+
public function getLastInsertId(?string $name = null): ?string;
5656

5757
/**
5858
* @param string $table

src/Databases/MySQL.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Kir\MySQL\Tools\VirtualTables;
1919
use PDO;
2020
use PDOException;
21+
use PDOStatement;
2122
use RuntimeException;
2223
use Stringable;
2324
use Throwable;
@@ -149,10 +150,14 @@ public function exec(
149150

150151
/**
151152
* @param string|null $name
152-
* @return string
153+
* @return string|null
153154
*/
154-
public function getLastInsertId(?string $name = null): string {
155-
return $this->pdo->lastInsertId();
155+
public function getLastInsertId(?string $name = null): ?string {
156+
$result = $this->pdo->lastInsertId();
157+
if($result === false) {
158+
return null;
159+
}
160+
return $result;
156161
}
157162

158163
/**
@@ -177,7 +182,9 @@ public function getTableFields(string $table): array {
177182
return $this->tableFields[$fqTable];
178183
} finally {
179184
try {
180-
$stmt->closeCursor();
185+
if($stmt instanceof PDOStatement) {
186+
$stmt->closeCursor();
187+
}
181188
} catch (Throwable $e) {}
182189
}
183190
})
@@ -213,7 +220,7 @@ public function quoteField(string $field): string {
213220
* @param array<string|int, string>|null $fields
214221
* @return MySQLRunnableSelect
215222
*/
216-
public function select(array $fields = null): Builder\RunnableSelect {
223+
public function select(?array $fields = null): Builder\RunnableSelect {
217224
$select = array_key_exists('select-factory', $this->options)
218225
? call_user_func($this->options['select-factory'], $this, $this->options['select-options'])
219226
: new MySQL\MySQLRunnableSelect($this, $this->options['select-options']);
@@ -227,7 +234,7 @@ public function select(array $fields = null): Builder\RunnableSelect {
227234
* @param null|array<string|int, string> $fields
228235
* @return Builder\RunnableInsert
229236
*/
230-
public function insert(array $fields = null): Builder\RunnableInsert {
237+
public function insert(?array $fields = null): Builder\RunnableInsert {
231238
$insert = array_key_exists('insert-factory', $this->options)
232239
? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options'])
233240
: new Builder\RunnableInsert($this, $this->options['insert-options']);
@@ -241,7 +248,7 @@ public function insert(array $fields = null): Builder\RunnableInsert {
241248
* @param array<string|int, string>|null $fields
242249
* @return Builder\RunnableUpdate
243250
*/
244-
public function update(array $fields = null): Builder\RunnableUpdate {
251+
public function update(?array $fields = null): Builder\RunnableUpdate {
245252
$update = array_key_exists('update-factory', $this->options)
246253
? call_user_func($this->options['update-factory'], $this, $this->options['update-options'])
247254
: new Builder\RunnableUpdate($this, $this->options['update-options']);
@@ -331,7 +338,6 @@ public function transaction(callable $callback) {
331338
$this->transactionCommit();
332339
return $result;
333340
} catch (Throwable $e) {
334-
// @phpstan-ignore-next-line; If condition is always false as it is not.
335341
if($this->pdo->inTransaction()) {
336342
$this->transactionRollback();
337343
}

tests/Builder/InsertTest/TestInsert.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function create(): TestInsert {
1616
/**
1717
* @inheritDoc
1818
*/
19-
public function insertRows($rows) {
19+
public function insertRows(iterable $rows) {
2020
return [];
2121
}
2222

0 commit comments

Comments
 (0)