Skip to content

Commit

Permalink
Fixes the problem with variadic arguments in column type methods (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksei Gagarin <[email protected]>
  • Loading branch information
butschster and roxblnfk authored Jan 3, 2023
1 parent 7af465e commit 943f42c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"cycle/database": "^2.2",
"cycle/database": "^2.3",
"spiral/core": "^3.0",
"spiral/files": "^3.0",
"spiral/tokenizer": "^3.0",
Expand Down
14 changes: 10 additions & 4 deletions src/Operation/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,27 @@ protected function declareColumn(AbstractTable $schema): AbstractColumn
//Type configuring
if (method_exists($column, $this->type)) {
$arguments = [];
$variadic = false;

$method = new \ReflectionMethod($column, $this->type);
foreach ($method->getParameters() as $parameter) {
if ($this->hasOption($parameter->getName())) {
$arguments[] = $this->getOption($parameter->getName());
$arguments[$parameter->getName()] = $this->getOption($parameter->getName());
} elseif (!$parameter->isOptional()) {
throw new ColumnException(
"Option '{$parameter->getName()}' are required to define column with type '{$this->type}'"
);
} else {
$arguments[] = $parameter->getDefaultValue();
} elseif ($parameter->isDefaultValueAvailable()) {
$arguments[$parameter->getName()] = $parameter->getDefaultValue();
} elseif ($parameter->isVariadic()) {
$variadic = true;
}
}

call_user_func_array([$column, $this->type], $arguments);
\call_user_func_array(
[$column, $this->type],
$variadic ? $arguments + $this->options + $column->getAttributes() : $arguments,
);
} else {
$column->type($this->type);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Migrations/AtomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,18 @@ public function testCreateDatetimeNowColumn(): void
$this->migrator->configure();

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

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

$this->migrator->run();

$this->assertTrue($this->db->hasTable('sample'));
$this->assertSame((string)$column->getDefaultValue(), (string)$this->schema('sample')->column('value')->getDefaultValue());
$this->assertSame(
(string)$column->getDefaultValue(),
(string)$this->schema('sample')->column('value')->getDefaultValue()
);

$this->migrator->rollback();
$this->assertFalse($this->db->hasTable('sample'));
Expand Down

0 comments on commit 943f42c

Please sign in to comment.