From 7f25ab0f443a7525c819b9856917fc5d7657f53c Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Tue, 5 Nov 2024 17:59:01 +0900 Subject: [PATCH 1/3] feat: better phpstan support part 1 --- phpstan.neon | 1 - src/Schema/Grammar.php | 25 ++++++++++--------------- src/Schema/IndexDefinition.php | 6 ++++++ src/Schema/InterleaveDefinition.php | 2 ++ 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 063a0d2..f8cd2e9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,7 +6,6 @@ parameters: - identifier: missingType.iterableValue - '#^Access to an undefined property Illuminate\\Support\\Fluent.*$#' - '#^Call to an undefined method Illuminate\\Support\\Fluent.*$#' - - '#^Access to an undefined property Colopl\\Spanner\\Schema\\IndexDefinition.*$#' - '#^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 diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index 86c7949..065aca8 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -354,6 +354,7 @@ protected function formatChangeStreamOptions(ChangeStreamDefinition $definition) protected function addInterleaveToTable(Blueprint $blueprint) { if (! is_null($command = $this->getCommandByName($blueprint, 'interleaveInParent'))) { + assert($command instanceof InterleaveDefinition); $schema = ", interleave in parent {$this->wrap($command->table)}"; if (! is_null($command->onDelete)) { $schema .= " on delete {$command->onDelete}"; @@ -402,19 +403,13 @@ public function compileUnique(Blueprint $blueprint, Fluent $command) */ public function compileIndex(Blueprint $blueprint, Fluent $command) { - $columnsAsString = null; - // if index is defined as assoc array, key is treated as column name and value as order // if index is defined as numeric array, then values are read as column names - $keys = array_keys($command->columns); - if (array_keys($keys) !== $keys) { - $columns = []; - foreach ($command->columns as $column => $order) { - $columns[] = $this->wrap($column).' '.$order; - } - $columnsAsString = implode(', ', $columns); - } else { - $columnsAsString = $this->columnize($command->columns); + $columns = []; + foreach ($command->columns as $column => $order) { + $columns[] = is_string($column) + ? $this->wrap($column) . ' ' . $order + : $this->wrap($order); } return sprintf('create %s%sindex %s on %s (%s)%s%s', @@ -422,7 +417,7 @@ public function compileIndex(Blueprint $blueprint, Fluent $command) empty($command->nullFiltered) ? '' :'null_filtered ', $this->wrap($command->index), $this->wrapTable($blueprint), - $columnsAsString, + implode(', ', $columns), $this->addStoringToIndex($command), $this->addInterleaveToIndex($command) ); @@ -457,7 +452,7 @@ protected function addStoringToIndex(Fluent $indexCommand): string /** * @param Blueprint $blueprint - * @param Fluent $command + * @param IndexDefinition $command * @return string * @see https://cloud.google.com/spanner/docs/data-definition-language?hl=en */ @@ -470,7 +465,7 @@ public function compileDropIndex(Blueprint $blueprint, Fluent $command) /** * @param Blueprint $blueprint - * @param Fluent $command + * @param IndexDefinition $command * @return string * @see https://cloud.google.com/spanner/docs/data-definition-language?hl=en */ @@ -483,7 +478,7 @@ public function compileDropUnique(Blueprint $blueprint, Fluent $command) * Compile a drop foreign key command. * * @param Blueprint $blueprint - * @param Fluent $command + * @param IndexDefinition $command * @return string */ public function compileDropForeign(Blueprint $blueprint, Fluent $command) diff --git a/src/Schema/IndexDefinition.php b/src/Schema/IndexDefinition.php index 09b2f8a..cdc3a72 100755 --- a/src/Schema/IndexDefinition.php +++ b/src/Schema/IndexDefinition.php @@ -22,6 +22,12 @@ use LogicException; /** + * @property string $indexType + * @property string $index + * @property list|array $columns + * @property string|null $interleaveIn + * @property string|null $nullFiltered + * @property list|null $storing * @method $this interleaveIn(string $table) * @method $this nullFiltered() * @method $this storing(string[] $columns) diff --git a/src/Schema/InterleaveDefinition.php b/src/Schema/InterleaveDefinition.php index dd0ecff..d7ddfae 100755 --- a/src/Schema/InterleaveDefinition.php +++ b/src/Schema/InterleaveDefinition.php @@ -21,6 +21,8 @@ use Illuminate\Support\Fluent; /** + * @property string $table + * @property string|null $onDelete * @method $this onDelete(string $action) Add an ON DELETE action * @extends Fluent */ From 8ecfceb84c8d1903a286b287b66d60e4e3d9c5ab Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Wed, 6 Nov 2024 14:47:35 +0900 Subject: [PATCH 2/3] fix type --- src/Schema/IndexDefinition.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/IndexDefinition.php b/src/Schema/IndexDefinition.php index cdc3a72..96416f4 100755 --- a/src/Schema/IndexDefinition.php +++ b/src/Schema/IndexDefinition.php @@ -24,9 +24,9 @@ /** * @property string $indexType * @property string $index - * @property list|array $columns + * @property array|list $columns * @property string|null $interleaveIn - * @property string|null $nullFiltered + * @property bool|null $nullFiltered * @property list|null $storing * @method $this interleaveIn(string $table) * @method $this nullFiltered() From 0e8c1f0a2eea5cba32745066d7e20ce9a75e7780 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Wed, 6 Nov 2024 14:48:01 +0900 Subject: [PATCH 3/3] feat: better phpstan support part2 # Conflicts: # phpstan.neon --- phpstan.neon | 2 - src/Schema/ColumnDefinition.php | 41 ++++++++++++ src/Schema/Grammar.php | 75 +++++++++++----------- src/Schema/RowDeletionPolicyDefinition.php | 33 ++++++++++ 4 files changed, 113 insertions(+), 38 deletions(-) create mode 100644 src/Schema/ColumnDefinition.php create mode 100644 src/Schema/RowDeletionPolicyDefinition.php diff --git a/phpstan.neon b/phpstan.neon index f8cd2e9..822d7bd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,8 +4,6 @@ parameters: - src ignoreErrors: - identifier: missingType.iterableValue - - '#^Access to an undefined property Illuminate\\Support\\Fluent.*$#' - - '#^Call to an undefined method Illuminate\\Support\\Fluent.*$#' - '#^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 diff --git a/src/Schema/ColumnDefinition.php b/src/Schema/ColumnDefinition.php new file mode 100644 index 0000000..94b1fc6 --- /dev/null +++ b/src/Schema/ColumnDefinition.php @@ -0,0 +1,41 @@ + $command + * @param Fluent&object{ column: ColumnDefinition } $command * @return list|string */ public function compileAdd(Blueprint $blueprint, Fluent $command) @@ -134,7 +134,7 @@ public function compileAdd(Blueprint $blueprint, Fluent $command) * Compile a change column command into a series of SQL statements. * * @param Blueprint $blueprint - * @param Fluent $command + * @param Fluent&object{ column: ColumnDefinition } $command * @param Connection $connection * @return list|string */ @@ -155,7 +155,7 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection * Compile a drop column command. * * @param Blueprint $blueprint - * @param Fluent $command + * @param Fluent&object{ columns: list } $command * @return string[] */ public function compileDropColumn(Blueprint $blueprint, Fluent $command) @@ -168,7 +168,7 @@ public function compileDropColumn(Blueprint $blueprint, Fluent $command) /** * @param Blueprint $blueprint - * @param Fluent $command + * @param RowDeletionPolicyDefinition $command * @return string */ public function compileAddRowDeletionPolicy(Blueprint $blueprint, Fluent $command) @@ -183,7 +183,7 @@ public function compileAddRowDeletionPolicy(Blueprint $blueprint, Fluent $comman /** * @param Blueprint $blueprint - * @param Fluent $command + * @param RowDeletionPolicyDefinition $command * @return string */ public function compileReplaceRowDeletionPolicy(Blueprint $blueprint, Fluent $command) @@ -198,7 +198,7 @@ public function compileReplaceRowDeletionPolicy(Blueprint $blueprint, Fluent $co /** * @param Blueprint $blueprint - * @param Fluent $command + * @param RowDeletionPolicyDefinition $command * @return string */ public function compileDropRowDeletionPolicy(Blueprint $blueprint, Fluent $command) @@ -372,6 +372,7 @@ protected function addInterleaveToTable(Blueprint $blueprint) protected function addRowDeletionPolicy(Blueprint $blueprint) { if (! is_null($command = $this->getCommandByName($blueprint, 'rowDeletionPolicy'))) { + /** @var RowDeletionPolicyDefinition $command */ if ($command->policy === 'olderThan') { return ', row deletion policy (older_than('.$command->column.', interval '.$command->days.' day))'; } @@ -497,6 +498,7 @@ public function compileDropForeign(Blueprint $blueprint, Fluent $command) protected function addPrimaryKeys(Blueprint $blueprint) { if (! is_null($primary = $this->getCommandByName($blueprint, 'primary'))) { + /** @var IndexDefinition $primary */ return "primary key ({$this->columnize($primary->columns)})"; } return ''; @@ -531,7 +533,7 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command) /** * Create the column definition for a string type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeString(Fluent $column) @@ -542,7 +544,7 @@ protected function typeString(Fluent $column) /** * Create the column definition for a char type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeChar(Fluent $column) @@ -553,7 +555,7 @@ protected function typeChar(Fluent $column) /** * Create the column definition for a text type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeText(Fluent $column) @@ -564,7 +566,7 @@ protected function typeText(Fluent $column) /** * Create the column definition for a medium text type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeMediumText(Fluent $column) @@ -575,7 +577,7 @@ protected function typeMediumText(Fluent $column) /** * Create the column definition for a long text type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeLongText(Fluent $column) @@ -586,7 +588,7 @@ protected function typeLongText(Fluent $column) /** * Create the column definition for a json type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeJson(Fluent $column) @@ -597,7 +599,7 @@ protected function typeJson(Fluent $column) /** * Create the column definition for a binary type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeBinary(Fluent $column) @@ -608,7 +610,7 @@ protected function typeBinary(Fluent $column) /** * Create the column definition for a big integer type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeBigInteger(Fluent $column) @@ -619,7 +621,7 @@ protected function typeBigInteger(Fluent $column) /** * Create the column definition for an integer type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeInteger(Fluent $column) @@ -630,7 +632,7 @@ protected function typeInteger(Fluent $column) /** * Create the column definition for a float type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeFloat(Fluent $column) @@ -641,7 +643,7 @@ protected function typeFloat(Fluent $column) /** * Create the column definition for a double type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeDouble(Fluent $column) @@ -652,7 +654,7 @@ protected function typeDouble(Fluent $column) /** * Create the column definition for a decimal type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeDecimal(Fluent $column) @@ -663,7 +665,7 @@ protected function typeDecimal(Fluent $column) /** * Create the column definition for a date type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeDate(Fluent $column) @@ -674,7 +676,7 @@ protected function typeDate(Fluent $column) /** * Create the column definition for a date-time type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeDateTime(Fluent $column) @@ -685,7 +687,7 @@ protected function typeDateTime(Fluent $column) /** * Create the column definition for a timestamp type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeTimestamp(Fluent $column) @@ -700,7 +702,7 @@ protected function typeTimestamp(Fluent $column) /** * Create the column definition for an uuid type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeUuid(Fluent $column) @@ -712,7 +714,7 @@ protected function typeUuid(Fluent $column) * Create the column definition for a ARRAY type. * https://cloud.google.com/spanner/docs/arrays * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeArray(Fluent $column) @@ -723,7 +725,7 @@ protected function typeArray(Fluent $column) /** * Create the column definition for a boolean type. * - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function typeBoolean(Fluent $column) @@ -733,7 +735,7 @@ protected function typeBoolean(Fluent $column) /** * @param Blueprint $blueprint - * @param Fluent $column + * @param IntColumnDefinition $column * @return string|null */ protected function modifyUseSequence(Blueprint $blueprint, Fluent $column): ?string @@ -747,8 +749,8 @@ protected function modifyUseSequence(Blueprint $blueprint, Fluent $column): ?str /** * Get the SQL for a nullable column modifier. * - * @param Blueprint $blueprint - * @param Fluent $column + * @param Blueprint $blueprint + * @param ColumnDefinition $column * @return string|null */ protected function modifyNullable(Blueprint $blueprint, Fluent $column) @@ -760,11 +762,12 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column) } /** - * @param Fluent $column + * @param ColumnDefinition $column * @return string */ protected function getArrayInnerType(Fluent $column): string { + assert($column->arrayType !== null); return $this->{'type'.ucfirst($column->arrayType)}($column); } @@ -772,7 +775,7 @@ protected function getArrayInnerType(Fluent $column): string * Get the SQL for a default column modifier. * * @param Blueprint $blueprint - * @param Fluent $column + * @param ColumnDefinition $column * @return string|null */ protected function modifyDefault(Blueprint $blueprint, Fluent $column) @@ -787,7 +790,7 @@ protected function modifyDefault(Blueprint $blueprint, Fluent $column) } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param string $type * @param mixed $value * @return int|float|string @@ -813,7 +816,7 @@ protected function formatDefaultValue(Fluent $column, string $type, mixed $value } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param mixed $value * @return string */ @@ -828,7 +831,7 @@ protected function formatArrayValue(Fluent $column, mixed $value): string } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param mixed $value * @return string */ @@ -839,7 +842,7 @@ protected function formatBoolValue(Fluent $column, mixed $value): string } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param mixed $value * @return string */ @@ -853,7 +856,7 @@ protected function formatDateValue(Fluent $column, mixed $value): string } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param mixed $value * @return string */ @@ -864,7 +867,7 @@ protected function formatFloatValue(Fluent $column, mixed $value): string } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param mixed $value * @return string */ @@ -875,7 +878,7 @@ protected function formatNumericValue(Fluent $column, mixed $value): string } /** - * @param Fluent $column + * @param ColumnDefinition $column * @param mixed $value * @return string */ diff --git a/src/Schema/RowDeletionPolicyDefinition.php b/src/Schema/RowDeletionPolicyDefinition.php new file mode 100644 index 0000000..1363d45 --- /dev/null +++ b/src/Schema/RowDeletionPolicyDefinition.php @@ -0,0 +1,33 @@ + + */ +class RowDeletionPolicyDefinition extends Fluent +{ +}