Skip to content

fix: new phpstan errors #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ public function processSelect(Builder $query, $results): array
return $results;
}

/**
* @inheritDoc
*/
public function processTables($results)
{
return array_map(function ($result) {
$result = (object) $result;

return [
'name' => $result->name,
'schema' => $result->schema === '' ? $result->schema : null,
'schema_qualified_name' => $result->schema !== '' ? $result->schema.'.'.$result->name : $result->name,
'size' => isset($result->size) ? (int) $result->size : null,
'comment' => null,
'collation' => null,
'engine' => null,
'parent' => $result->parent,
];
}, $results);
}

/**
* @template TValue of mixed
* @param TValue $value
Expand All @@ -100,41 +121,27 @@ protected function processColumn(mixed $value): mixed
}

/**
* Process the results of a columns query.
*
* {@inheritDoc}
* @param array<array-key, array<array-key, mixed>> $results
* @return array<array-key, array{
* name: string,
* type_name: string,
* type: string,
* collation: null,
* nullable: bool,
* default: scalar,
* auto_increment: false,
* comment: null
* }>
* @param list<array{COLUMN_NAME: string, SPANNER_TYPE: string, IS_NULLABLE: string, COLUMN_DEFAULT: mixed}> $results
*/
public function processColumns($results)
{
return array_map(static function (array $result) {
return [
'name' => $result['COLUMN_NAME'],
'type_name' => preg_replace("/\([^)]+\)/", "", $result['SPANNER_TYPE']),
'type' => $result['SPANNER_TYPE'],
'collation' => null,
'type_name' => (string) preg_replace("/\([^)]+\)/", "", $result['SPANNER_TYPE']),
'nullable' => $result['IS_NULLABLE'] !== 'NO',
'default' => $result['COLUMN_DEFAULT'],
'auto_increment' => false,
'generation' => null,
'comment' => null,
];
}, $results);
}

/**
* {@inheritDoc}
* @param array{ index_name: string }&array<string, mixed> $results
* @return array<array-key, string>
* @inheritDoc
*/
public function processIndexes($results)
{
Expand All @@ -144,9 +151,7 @@ public function processIndexes($results)
}

/**
* {@inheritDoc}
* @param array{key_name: string}&array<string, mixed> $results
* @return array<array-key, string>
* @inheritDoc
*/
public function processForeignKeys($results)
{
Expand Down
17 changes: 8 additions & 9 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Closure;
use Colopl\Spanner\Connection;
use Colopl\Spanner\Query\Processor;
use Illuminate\Database\Schema\Builder as BaseBuilder;

/**
Expand All @@ -41,17 +42,15 @@ class Builder extends BaseBuilder
public static $defaultMorphKeyType = 'uuid';

/**
* @param null $schema
* @inheritDoc Adds a parent key, for tracking interleaving
*
* @return list<array{ name: string, type: string, parent: string }>
* @return list<array{name: string, schema: string|null, schema_qualified_name: string, size: int|null, comment: string|null, collation: string|null, engine: string|null, parent: string}>
*/
public function getTables($schema = null)
{
/** @var list<array{ name: string, type: string, parent: string }> */
return $this->connection->select(
$this->grammar->compileTables(null),
);
$results = $this->connection->select($this->grammar->compileTables($schema));
assert(array_is_list($results));
/** @phpstan-ignore return.type */
return $this->connection->getPostProcessor()->processTables($results);
}

/**
Expand All @@ -73,7 +72,7 @@ public function dropIndex($table, $name)
*/
public function dropIndexIfExist($table, $name)
{
if (in_array($name, $this->getIndexes($table), true)) {
if (in_array($name, $this->getIndexListing($table), true)) {
$blueprint = $this->createBlueprint($table);
$blueprint->dropIndex($name);
$this->build($blueprint);
Expand Down Expand Up @@ -145,7 +144,7 @@ public function dropAllTables()
$queries = [];
foreach ($sortedTables as $tableData) {
$tableName = $tableData['name'];
$indexes = $this->getIndexes($tableName);
$indexes = $this->getIndexListing($tableName);
$blueprint = $this->createBlueprint($tableName);
foreach ($indexes as $index) {
if ($index === 'PRIMARY_KEY') {
Expand Down
9 changes: 8 additions & 1 deletion src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ class Grammar extends BaseGrammar
*/
public function compileTables($schema)
{
return 'select `table_name` as name, `table_type` as type, `parent_table_name` as parent from information_schema.tables where table_schema = \'\' and table_type = \'BASE TABLE\'';
if ($schema === null || (is_array($schema) && count($schema) === 0)) {
$schema = '';
}

return
'select `table_name` as name, `table_type` as type, `parent_table_name` as parent `table_schema` as `schema` ' .
'from information_schema.tables where table_type = \'BASE TABLE\'' .
'and table_schema in (' . $this->quoteString($schema) . ')';
}

/**
Expand Down
Loading