Skip to content

Commit ee339af

Browse files
authored
Support PHPStan 2.x (#245)
1 parent fd45164 commit ee339af

21 files changed

+181
-131
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require-dev": {
2121
"orchestra/testbench": "~9",
2222
"phpunit/phpunit": "~11.0",
23-
"phpstan/phpstan": "^1"
23+
"phpstan/phpstan": "^2"
2424
},
2525
"autoload": {
2626
"psr-4": {

phpstan.neon

+1-38
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,4 @@
11
parameters:
2-
level: max
2+
level: 9
33
paths:
44
- src
5-
ignoreErrors:
6-
- identifier: missingType.iterableValue
7-
- '#^Return type \(void\) of method .*? should be compatible with return type \(.*?\) of method .*?$#'
8-
- message: '#^Method Colopl\\Spanner\\Connection::runPartitionedDml\(\) should return int but returns mixed\.$#'
9-
path: src/Concerns/ManagesPartitionedDml.php
10-
- message: '#^Method Colopl\\Spanner\\Connection::selectWithOptions\(\) should return array<int, array> but returns mixed\.$#'
11-
path: src/Connection.php
12-
- message: "#^Return type \\(Generator\\<int, array, mixed, mixed\\>\\) of method Colopl\\\\Spanner\\\\Connection\\:\\:cursor\\(\\) should be compatible with return type \\(Generator\\<int, stdClass, mixed, mixed\\>\\) of method Illuminate\\\\Database\\\\Connection\\:\\:cursor\\(\\)$#"
13-
count: 1
14-
path: src/Connection.php
15-
- message: '#^Generator expects value type array, mixed given.$#'
16-
path: src/Connection.php
17-
count: 1
18-
- message: '#^Cannot cast mixed to int\.$#'
19-
path: src/Eloquent/Model.php
20-
- message: '#^Property Illuminate\\Database\\Schema\\Builder::\$resolver \(Closure\) in isset\(\) is not nullable\.$#'
21-
path: src/Schema/Builder.php
22-
- message: '#^Parameter \#1 \$connection of method Illuminate\\Database\\DatabaseTransactionsManager::.+\(\) expects string, string\|null given\.$#'
23-
path: src/Concerns/ManagesTransactions.php
24-
- message: '#^Property Colopl\\Spanner\\Session\\SessionInfo\:\:\$labels \(array\<string, string\>\) does not accept array\.$#'
25-
path: src/Session/SessionInfo.php
26-
count: 1
27-
- message: "#^Method Colopl\\\\Spanner\\\\SpannerServiceProvider\\:\\:parseConfig\\(\\) should return array\\{name\\: string, instance\\: string, database\\: string, prefix\\: string, cache_path\\: string|null, session_pool\\: array\\<string, mixed\\>\\} but returns non\\-empty\\-array\\<string, mixed\\>\\.$#"
28-
count: 1
29-
path: src/SpannerServiceProvider.php
30-
- message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
31-
count: 1
32-
path: src/Connection.php
33-
- message: "#^Cannot access offset 'requestTag' on mixed\\.$#"
34-
count: 1
35-
path: src/Connection.php
36-
- message: "#^Parameter \\#2 \\$length of method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:string\\(\\) expects int\\|null, int\\|string\\|null given\\.$#"
37-
count: 1
38-
path: src/Schema/Blueprint.php
39-
- message: "#^Parameter \\#1 \\$array of class Colopl\\\\Spanner\\\\Query\\\\Nested constructor expects array\\<int, mixed\\>\\|Illuminate\\\\Contracts\\\\Support\\\\Arrayable\\<int, mixed\\>, mixed given\\.$#"
40-
count: 1
41-
path: src/Query/Builder.php

src/Concerns/ManagesMutations.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
use Illuminate\Support\Arr;
2929
use InvalidArgumentException;
3030

31+
/**
32+
* @phpstan-type TDataSet list<array<string, array<array-key, mixed>>>|array<string, array<array-key, mixed>>
33+
*/
3134
trait ManagesMutations
3235
{
3336
/**
@@ -37,7 +40,7 @@ abstract protected function getDatabaseContext(): Database|Transaction;
3740

3841
/**
3942
* @param string $table
40-
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
43+
* @param TDataSet $dataSet
4144
* @return void
4245
*/
4346
public function insertUsingMutation(string $table, array $dataSet)
@@ -51,7 +54,7 @@ public function insertUsingMutation(string $table, array $dataSet)
5154

5255
/**
5356
* @param string $table
54-
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
57+
* @param TDataSet $dataSet
5558
* @return void
5659
*/
5760
public function updateUsingMutation(string $table, array $dataSet)
@@ -65,7 +68,7 @@ public function updateUsingMutation(string $table, array $dataSet)
6568

6669
/**
6770
* @param string $table
68-
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
71+
* @param TDataSet $dataSet
6972
* @return void
7073
*/
7174
public function insertOrUpdateUsingMutation(string $table, array $dataSet)
@@ -117,8 +120,8 @@ protected function withTransactionEvents(callable $mutationCall)
117120
}
118121

119122
/**
120-
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
121-
* @return list<array<string, mixed>>
123+
* @param TDataSet $dataSet
124+
* @return array<array-key, iterable<array-key, mixed>>
122125
*/
123126
protected function prepareForMutation(array $dataSet): array
124127
{
@@ -130,7 +133,6 @@ protected function prepareForMutation(array $dataSet): array
130133
$dataSet = [$dataSet];
131134
}
132135

133-
/** @var list<array<string, mixed>> $dataSet */
134136
foreach ($dataSet as $index => $values) {
135137
foreach ($values as $name => $value) {
136138
if ($value instanceof DateTimeInterface) {

src/Concerns/ManagesPartitionedDml.php

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ abstract public function getSpannerDatabase(): Database;
3535
*/
3636
public function runPartitionedDml($query, $bindings = [])
3737
{
38+
/** @var int */
3839
return $this->run($query, $bindings, function ($query, $bindings) {
3940
if ($this->pretending()) {
4041
return 0;

src/Concerns/ManagesTransactions.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ trait ManagesTransactions
4040
protected ?int $maxAttempts = null;
4141

4242
/**
43-
* @var array|null $commitOptions
43+
* @var array<string, mixed>|null $commitOptions
4444
*/
4545
protected ?array $commitOptions = null;
4646

@@ -84,6 +84,10 @@ public function transaction(Closure $callback, $attempts = -1)
8484
$this->transactions++;
8585

8686
$this->transactionsManager?->begin(
87+
/**
88+
* TODO: throws error
89+
* @phpstan-ignore argument.type
90+
*/
8791
$this->getName(), $this->transactions,
8892
);
8993

@@ -176,6 +180,10 @@ protected function performSpannerCommit(): void
176180
}
177181

178182
$this->transactionsManager?->commit(
183+
/**
184+
* TODO: throws error
185+
* @phpstan-ignore argument.type
186+
*/
179187
$this->getName(),
180188
$levelBeingCommitted,
181189
$this->transactions,
@@ -280,6 +288,7 @@ public function getCommitOptions(): array
280288

281289
$options = $this->getConfig('commit') ?? [];
282290
assert(is_array($options));
291+
/** @var array<string, mixed> $options */
283292
return $this->commitOptions = $options;
284293
}
285294

src/Connection.php

+41-20
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use Google\Cloud\Spanner\Transaction;
4040
use Illuminate\Contracts\Support\Arrayable;
4141
use Illuminate\Database\Connection as BaseConnection;
42+
use Illuminate\Database\Query\Expression;
4243
use Illuminate\Database\Query\Grammars\Grammar as BaseQueryGrammar;
4344
use Illuminate\Database\QueryException;
4445
use Illuminate\Support\Arr;
@@ -240,15 +241,23 @@ protected function getDefaultPostProcessor(): QueryProcessor
240241
}
241242

242243
/**
243-
* @inheritDoc OVERRIDDEN for return type change
244+
* OVERRIDDEN for return type change
245+
*
246+
* {@inheritDoc}
247+
*
248+
* @param Closure|QueryBuilder|Expression|string $table
249+
* @return QueryBuilder
244250
*/
245251
public function table($table, $as = null): QueryBuilder
246252
{
247253
return $this->query()->from($table, $as);
248254
}
249255

250256
/**
251-
* @inheritDoc OVERRIDDEN for return type change
257+
* OVERRIDDEN for return type change
258+
*
259+
* {@inheritDoc}
260+
*
252261
* @return QueryBuilder
253262
*/
254263
public function query(): QueryBuilder
@@ -257,7 +266,9 @@ public function query(): QueryBuilder
257266
}
258267

259268
/**
260-
* @inheritDoc
269+
* {@inheritDoc}
270+
* @param array<array-key, mixed> $bindings
271+
* @return array<array-key, mixed>
261272
*/
262273
public function select($query, $bindings = [], $useReadPdo = true): array
263274
{
@@ -267,6 +278,9 @@ public function select($query, $bindings = [], $useReadPdo = true): array
267278
/**
268279
* {@inheritDoc}
269280
* @return Generator<int, array<array-key, mixed>>
281+
* @param array<array-key, mixed> $bindings
282+
* @return Generator<int, array<array-key, mixed>>
283+
* @phpstan-ignore method.childReturnType
270284
*/
271285
public function cursor($query, $bindings = [], $useReadPdo = true): Generator
272286
{
@@ -281,6 +295,7 @@ public function cursor($query, $bindings = [], $useReadPdo = true): Generator
281295
*/
282296
public function selectWithOptions(string $query, array $bindings, array $options): array
283297
{
298+
/** @var array<int, array<array-key, mixed>> */
284299
return $this->run($query, $bindings, function ($query, $bindings) use ($options): array {
285300
return !$this->pretending()
286301
? iterator_to_array($this->executeQuery($query, $bindings, $options))
@@ -304,28 +319,32 @@ public function cursorWithOptions(string $query, array $bindings, array $options
304319
}
305320

306321
/**
307-
* @inheritDoc
322+
* {@inheritDoc}
323+
* @param array<array-key, mixed> $bindings
308324
*/
309325
public function statement($query, $bindings = []): bool
310326
{
311327
// is SELECT query
312328
if (0 === stripos(ltrim($query), 'select')) {
313-
return $this->select($query, $bindings) !== null;
329+
$this->select($query, $bindings);
330+
return true;
314331
}
315332

316333
// is DML query
317334
if (0 === stripos(ltrim($query), 'insert') ||
318335
0 === stripos(ltrim($query), 'update') ||
319336
0 === stripos(ltrim($query), 'delete')) {
320-
return $this->affectingStatement($query, $bindings) !== null;
337+
$this->affectingStatement($query, $bindings);
338+
return true;
321339
}
322340

323341
// is DDL Query
324342
return $this->runDdlBatch([$query]) !== null;
325343
}
326344

327345
/**
328-
* @inheritDoc
346+
* {@inheritDoc}
347+
* @param array<array-key, mixed> $bindings
329348
*/
330349
public function affectingStatement($query, $bindings = []): int
331350
{
@@ -378,8 +397,8 @@ public function getDatabaseName()
378397

379398
/**
380399
* @internal
381-
* @inheritDoc
382-
* @return void
400+
* {@inheritDoc}
401+
* @return never
383402
*/
384403
public function setDatabaseName($database)
385404
{
@@ -388,9 +407,8 @@ public function setDatabaseName($database)
388407

389408
/**
390409
* @internal
391-
* @inheritDoc
392-
* @return void
393-
* @internal
410+
* {@inheritDoc}
411+
* @return never
394412
*/
395413
public function getPdo()
396414
{
@@ -399,9 +417,8 @@ public function getPdo()
399417

400418
/**
401419
* @internal
402-
* @inheritDoc
403-
* @return void
404-
* @internal
420+
* {@inheritDoc}
421+
* @return never
405422
*/
406423
public function getReadPdo()
407424
{
@@ -419,7 +436,9 @@ public function getDoctrineConnection()
419436
}
420437

421438
/**
422-
* @inheritDoc
439+
* {@inheritDoc}
440+
* @param array<array-key, mixed> $bindings
441+
* @return array<array-key, mixed>
423442
*/
424443
public function prepareBindings(array $bindings)
425444
{
@@ -457,7 +476,7 @@ protected function prepareBinding(BaseQueryGrammar $grammar, mixed $value): mixe
457476
}
458477

459478
/**
460-
* @inheritDoc
479+
* {@inheritDoc}
461480
* @param scalar|list<mixed>|Nested|null $value
462481
*/
463482
public function escape($value, $binary = false)
@@ -508,7 +527,8 @@ protected function escapeString($value)
508527
}
509528

510529
/**
511-
* @inheritDoc
530+
* {@inheritDoc}
531+
* @param array<array-key, mixed> $bindings
512532
*/
513533
protected function runQueryCallback($query, $bindings, Closure $callback)
514534
{
@@ -596,6 +616,7 @@ protected function executeQuery(string $query, array $bindings, array $options):
596616
$tag = $this->getRequestTag();
597617
if ($tag !== null) {
598618
$options['requestOptions'] ??= [];
619+
assert(is_array($options['requestOptions']));
599620
$options['requestOptions']['requestTag'] = $tag;
600621
}
601622

@@ -623,6 +644,7 @@ protected function executePartitionedQuery(string $query, array $options): Gener
623644

624645
foreach ($snapshot->partitionQuery($query, $options) as $partition) {
625646
foreach ($snapshot->executePartition($partition) as $row) {
647+
/** @var array<array-key, mixed> $row */
626648
yield $row;
627649
}
628650
}
@@ -675,7 +697,7 @@ protected function executeBatchDml(Transaction $transaction, string $query, arra
675697
);
676698
}
677699

678-
$rowCount = array_sum($result->rowCounts() ?? []);
700+
$rowCount = array_sum($result->rowCounts());
679701
$this->recordsHaveBeenModified($rowCount > 0);
680702
return $rowCount;
681703
}
@@ -742,5 +764,4 @@ protected function causedBySessionNotFound(Throwable $e): bool
742764
return ($e instanceof NotFoundException)
743765
&& str_contains($e->getMessage(), 'Session does not exist');
744766
}
745-
746767
}

src/Eloquent/Concerns/DoesNotAutoIncrement.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ public function getIncrementing()
3737
}
3838

3939
/**
40-
* @inheritDoc
40+
* {@inheritDoc}
41+
* @return never
4142
*/
4243
public function setIncrementing($value)
4344
{
4445
$this->markAsNotSupported('AUTO_INCREMENT');
4546
}
4647

4748
/**
48-
* @inheritDoc
49+
* {@inheritDoc}
50+
* @param array<array-key, mixed> $attributes
51+
* @return never
4952
*/
5053
protected function insertAndSetId(Builder $query, $attributes): void
5154
{

0 commit comments

Comments
 (0)