Skip to content
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

Fix #20247: Support for variadic console controller action methods #20247

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 20 additions & 8 deletions framework/console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
$method = new \ReflectionMethod($action, 'run');
}

$paramKeys = array_keys($params);
$args = [];
$missing = [];
$actionParams = [];
Expand All @@ -212,16 +213,27 @@
}

if ($key !== null) {
if (PHP_VERSION_ID >= 80000) {
$isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array';
if ($param->isVariadic()) {
for ($j = array_search($key, $paramKeys); $j < count($paramKeys); $j++) {
$jKey = $paramKeys[$j];
if ($j !== $key && !is_int($j)) {
break;

Check warning on line 220 in framework/console/Controller.php

View check run for this annotation

Codecov / codecov/patch

framework/console/Controller.php#L220

Added line #L220 was not covered by tests
}
$args[] = $actionParams[$key][] = $params[$jKey];
unset($params[$jKey]);
}
} else {
$isArray = $param->isArray();
}
if ($isArray) {
$params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]);
if (PHP_VERSION_ID >= 80000) {
$isArray = ($type = $param->getType()) instanceof \ReflectionNamedType && $type->getName() === 'array';

Check warning on line 227 in framework/console/Controller.php

View check run for this annotation

Codecov / codecov/patch

framework/console/Controller.php#L227

Added line #L227 was not covered by tests
} else {
$isArray = $param->isArray();
}
if ($isArray) {
$params[$key] = $params[$key] === '' ? [] : preg_split('/\s*,\s*/', $params[$key]);
}
$args[] = $actionParams[$key] = $params[$key];
unset($params[$key]);
}
$args[] = $actionParams[$key] = $params[$key];
unset($params[$key]);
} elseif (
PHP_VERSION_ID >= 70100
&& ($type = $param->getType()) !== null
Expand Down
6 changes: 6 additions & 0 deletions tests/framework/console/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public function testBindActionParams()
$this->assertEquals('from params', $fromParam);
$this->assertEquals('notdefault', $other);

$params = ['a', 'b', 'c1', 'c2', 'c3'];
[$a, $b, $c] = $controller->run('variadic', $params);
$this->assertEquals('a', $a);
$this->assertEquals('b', $b);
$this->assertEquals(['c1', 'c2', 'c3'], $c);

$params = ['avaliable'];
$message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]);
$this->expectException('yii\console\Exception');
Expand Down
5 changes: 5 additions & 0 deletions tests/framework/console/FakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ public function actionResponse($status = 0)
$response->exitStatus = (int) $status;
return $response;
}

public function actionVariadic($foo, $bar, ...$baz)
{
return [$foo, $bar, $baz];
}
}
Loading