Skip to content

Commit ad0a80c

Browse files
butschsterroxblnfk
authored andcommitted
Fixes the problem with variadic arguments in column type methods (#45)
Co-authored-by: Aleksei Gagarin <[email protected]> # Conflicts: # composer.json
1 parent 6a11fa2 commit ad0a80c

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"require": {
77
"php": ">=8.0",
8-
"cycle/database": "^2.0",
8+
"cycle/database": "^2.3",
99
"spiral/core": "^2.7",
1010
"spiral/files": "^2.7",
1111
"spiral/tokenizer": "^2.7",

Diff for: src/Operation/Column/Column.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,27 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn
4343
//Type configuring
4444
if (method_exists($column, $this->type)) {
4545
$arguments = [];
46+
$variadic = false;
4647

4748
$method = new \ReflectionMethod($column, $this->type);
4849
foreach ($method->getParameters() as $parameter) {
4950
if ($this->hasOption($parameter->getName())) {
50-
$arguments[] = $this->getOption($parameter->getName());
51+
$arguments[$parameter->getName()] = $this->getOption($parameter->getName());
5152
} elseif (!$parameter->isOptional()) {
5253
throw new ColumnException(
5354
"Option '{$parameter->getName()}' are required to define column with type '{$this->type}'"
5455
);
55-
} else {
56-
$arguments[] = $parameter->getDefaultValue();
56+
} elseif ($parameter->isDefaultValueAvailable()) {
57+
$arguments[$parameter->getName()] = $parameter->getDefaultValue();
58+
} elseif ($parameter->isVariadic()) {
59+
$variadic = true;
5760
}
5861
}
5962

60-
call_user_func_array([$column, $this->type], $arguments);
63+
\call_user_func_array(
64+
[$column, $this->type],
65+
$variadic ? $arguments + $this->options + $column->getAttributes() : $arguments,
66+
);
6167
} else {
6268
$column->type($this->type);
6369
}

Diff for: tests/Migrations/AtomizerTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,18 @@ public function testCreateDatetimeNowColumn(): void
310310
$this->migrator->configure();
311311

312312
$schema = $this->schema('sample');
313-
$column = $schema->datetime('value');
313+
$column = $schema->datetime('value', size: 2, foo: 'bar');
314314
$column->defaultValue(new Fragment($column::DATETIME_NOW));
315315

316316
$this->atomize('migration1', [$schema]);
317317

318318
$this->migrator->run();
319319

320320
$this->assertTrue($this->db->hasTable('sample'));
321-
$this->assertSame((string)$column->getDefaultValue(), (string)$this->schema('sample')->column('value')->getDefaultValue());
321+
$this->assertSame(
322+
(string)$column->getDefaultValue(),
323+
(string)$this->schema('sample')->column('value')->getDefaultValue()
324+
);
322325

323326
$this->migrator->rollback();
324327
$this->assertFalse($this->db->hasTable('sample'));

0 commit comments

Comments
 (0)