Skip to content

Commit

Permalink
Support PHPStan 2.x (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeriyoshi authored Jan 8, 2025
1 parent fd45164 commit ee339af
Show file tree
Hide file tree
Showing 21 changed files with 181 additions and 131 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require-dev": {
"orchestra/testbench": "~9",
"phpunit/phpunit": "~11.0",
"phpstan/phpstan": "^1"
"phpstan/phpstan": "^2"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 1 addition & 38 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,41 +1,4 @@
parameters:
level: max
level: 9
paths:
- src
ignoreErrors:
- identifier: missingType.iterableValue
- '#^Return type \(void\) of method .*? should be compatible with return type \(.*?\) of method .*?$#'
- message: '#^Method Colopl\\Spanner\\Connection::runPartitionedDml\(\) should return int but returns mixed\.$#'
path: src/Concerns/ManagesPartitionedDml.php
- message: '#^Method Colopl\\Spanner\\Connection::selectWithOptions\(\) should return array<int, array> but returns mixed\.$#'
path: src/Connection.php
- 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\\(\\)$#"
count: 1
path: src/Connection.php
- message: '#^Generator expects value type array, mixed given.$#'
path: src/Connection.php
count: 1
- message: '#^Cannot cast mixed to int\.$#'
path: src/Eloquent/Model.php
- message: '#^Property Illuminate\\Database\\Schema\\Builder::\$resolver \(Closure\) in isset\(\) is not nullable\.$#'
path: src/Schema/Builder.php
- message: '#^Parameter \#1 \$connection of method Illuminate\\Database\\DatabaseTransactionsManager::.+\(\) expects string, string\|null given\.$#'
path: src/Concerns/ManagesTransactions.php
- message: '#^Property Colopl\\Spanner\\Session\\SessionInfo\:\:\$labels \(array\<string, string\>\) does not accept array\.$#'
path: src/Session/SessionInfo.php
count: 1
- 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\\>\\.$#"
count: 1
path: src/SpannerServiceProvider.php
- message: "#^Expression on left side of \\?\\? is not nullable\\.$#"
count: 1
path: src/Connection.php
- message: "#^Cannot access offset 'requestTag' on mixed\\.$#"
count: 1
path: src/Connection.php
- message: "#^Parameter \\#2 \\$length of method Illuminate\\\\Database\\\\Schema\\\\Blueprint\\:\\:string\\(\\) expects int\\|null, int\\|string\\|null given\\.$#"
count: 1
path: src/Schema/Blueprint.php
- message: "#^Parameter \\#1 \\$array of class Colopl\\\\Spanner\\\\Query\\\\Nested constructor expects array\\<int, mixed\\>\\|Illuminate\\\\Contracts\\\\Support\\\\Arrayable\\<int, mixed\\>, mixed given\\.$#"
count: 1
path: src/Query/Builder.php
14 changes: 8 additions & 6 deletions src/Concerns/ManagesMutations.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use Illuminate\Support\Arr;
use InvalidArgumentException;

/**
* @phpstan-type TDataSet list<array<string, array<array-key, mixed>>>|array<string, array<array-key, mixed>>
*/
trait ManagesMutations
{
/**
Expand All @@ -37,7 +40,7 @@ abstract protected function getDatabaseContext(): Database|Transaction;

/**
* @param string $table
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
* @param TDataSet $dataSet
* @return void
*/
public function insertUsingMutation(string $table, array $dataSet)
Expand All @@ -51,7 +54,7 @@ public function insertUsingMutation(string $table, array $dataSet)

/**
* @param string $table
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
* @param TDataSet $dataSet
* @return void
*/
public function updateUsingMutation(string $table, array $dataSet)
Expand All @@ -65,7 +68,7 @@ public function updateUsingMutation(string $table, array $dataSet)

/**
* @param string $table
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
* @param TDataSet $dataSet
* @return void
*/
public function insertOrUpdateUsingMutation(string $table, array $dataSet)
Expand Down Expand Up @@ -117,8 +120,8 @@ protected function withTransactionEvents(callable $mutationCall)
}

/**
* @param list<array<string, mixed>>|array<string, mixed> $dataSet
* @return list<array<string, mixed>>
* @param TDataSet $dataSet
* @return array<array-key, iterable<array-key, mixed>>
*/
protected function prepareForMutation(array $dataSet): array
{
Expand All @@ -130,7 +133,6 @@ protected function prepareForMutation(array $dataSet): array
$dataSet = [$dataSet];
}

/** @var list<array<string, mixed>> $dataSet */
foreach ($dataSet as $index => $values) {
foreach ($values as $name => $value) {
if ($value instanceof DateTimeInterface) {
Expand Down
1 change: 1 addition & 0 deletions src/Concerns/ManagesPartitionedDml.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ abstract public function getSpannerDatabase(): Database;
*/
public function runPartitionedDml($query, $bindings = [])
{
/** @var int */
return $this->run($query, $bindings, function ($query, $bindings) {
if ($this->pretending()) {
return 0;
Expand Down
11 changes: 10 additions & 1 deletion src/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trait ManagesTransactions
protected ?int $maxAttempts = null;

/**
* @var array|null $commitOptions
* @var array<string, mixed>|null $commitOptions
*/
protected ?array $commitOptions = null;

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

$this->transactionsManager?->begin(
/**
* TODO: throws error
* @phpstan-ignore argument.type
*/
$this->getName(), $this->transactions,
);

Expand Down Expand Up @@ -176,6 +180,10 @@ protected function performSpannerCommit(): void
}

$this->transactionsManager?->commit(
/**
* TODO: throws error
* @phpstan-ignore argument.type
*/
$this->getName(),
$levelBeingCommitted,
$this->transactions,
Expand Down Expand Up @@ -280,6 +288,7 @@ public function getCommitOptions(): array

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

Expand Down
61 changes: 41 additions & 20 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Google\Cloud\Spanner\Transaction;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\Grammar as BaseQueryGrammar;
use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -240,15 +241,23 @@ protected function getDefaultPostProcessor(): QueryProcessor
}

/**
* @inheritDoc OVERRIDDEN for return type change
* OVERRIDDEN for return type change
*
* {@inheritDoc}
*
* @param Closure|QueryBuilder|Expression|string $table
* @return QueryBuilder
*/
public function table($table, $as = null): QueryBuilder
{
return $this->query()->from($table, $as);
}

/**
* @inheritDoc OVERRIDDEN for return type change
* OVERRIDDEN for return type change
*
* {@inheritDoc}
*
* @return QueryBuilder
*/
public function query(): QueryBuilder
Expand All @@ -257,7 +266,9 @@ public function query(): QueryBuilder
}

/**
* @inheritDoc
* {@inheritDoc}
* @param array<array-key, mixed> $bindings
* @return array<array-key, mixed>
*/
public function select($query, $bindings = [], $useReadPdo = true): array
{
Expand All @@ -267,6 +278,9 @@ public function select($query, $bindings = [], $useReadPdo = true): array
/**
* {@inheritDoc}
* @return Generator<int, array<array-key, mixed>>
* @param array<array-key, mixed> $bindings
* @return Generator<int, array<array-key, mixed>>
* @phpstan-ignore method.childReturnType
*/
public function cursor($query, $bindings = [], $useReadPdo = true): Generator
{
Expand All @@ -281,6 +295,7 @@ public function cursor($query, $bindings = [], $useReadPdo = true): Generator
*/
public function selectWithOptions(string $query, array $bindings, array $options): array
{
/** @var array<int, array<array-key, mixed>> */
return $this->run($query, $bindings, function ($query, $bindings) use ($options): array {
return !$this->pretending()
? iterator_to_array($this->executeQuery($query, $bindings, $options))
Expand All @@ -304,28 +319,32 @@ public function cursorWithOptions(string $query, array $bindings, array $options
}

/**
* @inheritDoc
* {@inheritDoc}
* @param array<array-key, mixed> $bindings
*/
public function statement($query, $bindings = []): bool
{
// is SELECT query
if (0 === stripos(ltrim($query), 'select')) {
return $this->select($query, $bindings) !== null;
$this->select($query, $bindings);
return true;
}

// is DML query
if (0 === stripos(ltrim($query), 'insert') ||
0 === stripos(ltrim($query), 'update') ||
0 === stripos(ltrim($query), 'delete')) {
return $this->affectingStatement($query, $bindings) !== null;
$this->affectingStatement($query, $bindings);
return true;
}

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

/**
* @inheritDoc
* {@inheritDoc}
* @param array<array-key, mixed> $bindings
*/
public function affectingStatement($query, $bindings = []): int
{
Expand Down Expand Up @@ -378,8 +397,8 @@ public function getDatabaseName()

/**
* @internal
* @inheritDoc
* @return void
* {@inheritDoc}
* @return never
*/
public function setDatabaseName($database)
{
Expand All @@ -388,9 +407,8 @@ public function setDatabaseName($database)

/**
* @internal
* @inheritDoc
* @return void
* @internal
* {@inheritDoc}
* @return never
*/
public function getPdo()
{
Expand All @@ -399,9 +417,8 @@ public function getPdo()

/**
* @internal
* @inheritDoc
* @return void
* @internal
* {@inheritDoc}
* @return never
*/
public function getReadPdo()
{
Expand All @@ -419,7 +436,9 @@ public function getDoctrineConnection()
}

/**
* @inheritDoc
* {@inheritDoc}
* @param array<array-key, mixed> $bindings
* @return array<array-key, mixed>
*/
public function prepareBindings(array $bindings)
{
Expand Down Expand Up @@ -457,7 +476,7 @@ protected function prepareBinding(BaseQueryGrammar $grammar, mixed $value): mixe
}

/**
* @inheritDoc
* {@inheritDoc}
* @param scalar|list<mixed>|Nested|null $value
*/
public function escape($value, $binary = false)
Expand Down Expand Up @@ -508,7 +527,8 @@ protected function escapeString($value)
}

/**
* @inheritDoc
* {@inheritDoc}
* @param array<array-key, mixed> $bindings
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
{
Expand Down Expand Up @@ -596,6 +616,7 @@ protected function executeQuery(string $query, array $bindings, array $options):
$tag = $this->getRequestTag();
if ($tag !== null) {
$options['requestOptions'] ??= [];
assert(is_array($options['requestOptions']));
$options['requestOptions']['requestTag'] = $tag;
}

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

foreach ($snapshot->partitionQuery($query, $options) as $partition) {
foreach ($snapshot->executePartition($partition) as $row) {
/** @var array<array-key, mixed> $row */
yield $row;
}
}
Expand Down Expand Up @@ -675,7 +697,7 @@ protected function executeBatchDml(Transaction $transaction, string $query, arra
);
}

$rowCount = array_sum($result->rowCounts() ?? []);
$rowCount = array_sum($result->rowCounts());
$this->recordsHaveBeenModified($rowCount > 0);
return $rowCount;
}
Expand Down Expand Up @@ -742,5 +764,4 @@ protected function causedBySessionNotFound(Throwable $e): bool
return ($e instanceof NotFoundException)
&& str_contains($e->getMessage(), 'Session does not exist');
}

}
7 changes: 5 additions & 2 deletions src/Eloquent/Concerns/DoesNotAutoIncrement.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ public function getIncrementing()
}

/**
* @inheritDoc
* {@inheritDoc}
* @return never
*/
public function setIncrementing($value)
{
$this->markAsNotSupported('AUTO_INCREMENT');
}

/**
* @inheritDoc
* {@inheritDoc}
* @param array<array-key, mixed> $attributes
* @return never
*/
protected function insertAndSetId(Builder $query, $attributes): void
{
Expand Down
Loading

0 comments on commit ee339af

Please sign in to comment.