Skip to content

[Console] add union types #41912

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

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 8 additions & 10 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,9 @@ public function mergeApplicationDefinition(bool $mergeArgs = true)
/**
* Sets an array of argument and option instances.
*
* @param array|InputDefinition $definition An array of argument and option instances or a definition instance
*
* @return $this
*/
public function setDefinition($definition)
public function setDefinition(array|InputDefinition $definition)
{
if ($definition instanceof InputDefinition) {
$this->definition = $definition;
Expand Down Expand Up @@ -420,14 +418,14 @@ public function getNativeDefinition()
/**
* Adds an argument.
*
* @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
* @param string|string[]|null $default The default value (for InputArgument::OPTIONAL mode only)
* @param $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
* @param $default The default value (for InputArgument::OPTIONAL mode only)
*
* @throws InvalidArgumentException When argument mode is not valid
*
* @return $this
*/
public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the parameter annotation wrong here?

Suggested change
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null)
public function addArgument(string $name, int $mode = null, string $description = '', string|array|null $default = null)

Copy link
Member Author

@nicolas-grekas nicolas-grekas Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fail if we restrict here. We should at least allow int|float|bool. But I don't see the need to restrict when it was allowed before. We should avoid needless BC breaks IMHO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not make much sense to pass a default value that cannot be produced by invoking the command via the console. null would be the only reasonable exception here.

If we don't want to break BC, I'm fine with mixed but please let's keep the more precise type in the doc block then.

Copy link
Member Author

@nicolas-grekas nicolas-grekas Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the point of the annotation? If technically we don't care, we shouldn't bother our users IMHO.
I'm sure there are ppl out there that already found some creative use of that :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there are ppl out there that already found some creative use of that :)

That's precisely what scares me. 🙈

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be proud of what ppl do with Symfony :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some git blame + some code inspection. We use mixed in many places already. Having this restricted list is both wrong (as proved by tests) and not needed. I confirm this change.

PR ready.

{
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
if (null !== $this->fullDefinition) {
Expand All @@ -440,15 +438,15 @@ public function addArgument(string $name, int $mode = null, string $description
/**
* Adds an option.
*
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
* @param string|string[]|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
* @param $mode The option mode: One of the InputOption::VALUE_* constants
* @param $default The default value (must be null for InputOption::VALUE_NONE)
*
* @throws InvalidArgumentException If option mode is invalid or incompatible
*
* @return $this
*/
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null)
{
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
if (null !== $this->fullDefinition) {
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Command/LazyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function mergeApplicationDefinition(bool $mergeArgs = true): void
/**
* @return $this
*/
public function setDefinition($definition): self
public function setDefinition(array|InputDefinition $definition): self
{
$this->getCommand()->setDefinition($definition);

Expand All @@ -110,7 +110,7 @@ public function getNativeDefinition(): InputDefinition
/**
* @return $this
*/
public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null): self
{
$this->getCommand()->addArgument($name, $mode, $description, $default);

Expand All @@ -120,7 +120,7 @@ public function addArgument(string $name, int $mode = null, string $description
/**
* @return $this
*/
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self
public function addOption(string $name, string|array $shortcut = null, int $mode = null, string $description = '', mixed $default = null): self
{
$this->getCommand()->addOption($name, $shortcut, $mode, $description, $default);

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Descriptor/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class Descriptor implements DescriptorInterface
/**
* {@inheritdoc}
*/
public function describe(OutputInterface $output, $object, array $options = [])
public function describe(OutputInterface $output, object $object, array $options = [])
{
$this->output = $output;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MarkdownDescriptor extends Descriptor
/**
* {@inheritdoc}
*/
public function describe(OutputInterface $output, $object, array $options = [])
public function describe(OutputInterface $output, object $object, array $options = [])
{
$decorated = $output->isDecorated();
$output->setDecorated(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,8 @@ private function getCommandAliasesText(Command $command): string

/**
* Formats input option/argument default value.
*
* @param mixed $default
*/
private function formatDefaultValue($default): string
private function formatDefaultValue(mixed $default): string
{
if (\INF === $default) {
return 'INF';
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(OutputInterface $output, CliDumper $dumper = null, C
}
}

public function __invoke($var): string
public function __invoke(mixed $var): string
{
return ($this->handler)($var);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Console/Helper/FormatterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ public function formatSection(string $section, string $message, string $style =
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
*
* @return string The formatter message
*/
public function formatBlock($messages, string $style, bool $large = false)
public function formatBlock(string|array $messages, string $style, bool $large = false)
{
if (!\is_array($messages)) {
$messages = [$messages];
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function substr(?string $string, int $from, int $length = null)
return mb_substr($string, $from, $length, $encoding);
}

public static function formatTime($secs)
public static function formatTime(int|float $secs)
{
static $timeFormats = [
[0, '< 1 sec'],
Expand Down
12 changes: 2 additions & 10 deletions src/Symfony/Component/Console/Helper/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ class ProcessHelper extends Helper
* @param array|Process $cmd An instance of Process or an array of the command and arguments
* @param callable|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return Process The process that ran
*/
public function run(OutputInterface $output, $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
public function run(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
{
if (!class_exists(Process::class)) {
throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
Expand All @@ -50,10 +48,6 @@ public function run(OutputInterface $output, $cmd, string $error = null, callabl
$cmd = [$cmd];
}

if (!\is_array($cmd)) {
throw new \TypeError(sprintf('The "command" argument of "%s()" must be an array or a "%s" instance, "%s" given.', __METHOD__, Process::class, get_debug_type($cmd)));
}

if (\is_string($cmd[0] ?? null)) {
$process = new Process($cmd);
$cmd = [];
Expand Down Expand Up @@ -96,13 +90,11 @@ public function run(OutputInterface $output, $cmd, string $error = null, callabl
* @param callable|null $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return Process The process that ran
*
* @throws ProcessFailedException
*
* @see run()
*/
public function mustRun(OutputInterface $output, $cmd, string $error = null, callable $callback = null): Process
public function mustRun(OutputInterface $output, string|Process $cmd, string $error = null, callable $callback = null): Process
{
$process = $this->run($output, $cmd, $error, $callback);

Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,9 @@ private function setIOCodepage(): int
/**
* Sets console I/O to the specified code page and converts the user input.
*
* @param string|false $input
*
* @return string|false
*/
private function resetIOCodepage(int $cp, $input)
private function resetIOCodepage(int $cp, string|false $input)
{
if (0 !== $cp) {
sapi_windows_cp_set($cp);
Expand Down
26 changes: 10 additions & 16 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ public static function getStyleDefinition(string $name)
/**
* Sets table style.
*
* @param TableStyle|string $name The style name or a TableStyle instance
*
* @return $this
*/
public function setStyle($name)
public function setStyle(TableStyle|string $name)
{
$this->style = $this->resolveStyle($name);

Expand All @@ -161,7 +159,7 @@ public function getStyle()
*
* @return $this
*/
public function setColumnStyle(int $columnIndex, $name)
public function setColumnStyle(int $columnIndex, TableStyle|string $name)
{
$this->columnStyles[$columnIndex] = $this->resolveStyle($name);

Expand Down Expand Up @@ -254,18 +252,14 @@ public function addRows(array $rows)
return $this;
}

public function addRow($row)
public function addRow(TableSeparator|array $row)
{
if ($row instanceof TableSeparator) {
$this->rows[] = $row;

return $this;
}

if (!\is_array($row)) {
throw new InvalidArgumentException('A row must be an array or a TableSeparator instance.');
}

$this->rows[] = array_values($row);

return $this;
Expand All @@ -274,7 +268,7 @@ public function addRow($row)
/**
* Adds a row to the table, and re-renders the table.
*/
public function appendRow($row): self
public function appendRow(TableSeparator|array $row): self
{
if (!$this->output instanceof ConsoleSectionOutput) {
throw new RuntimeException(sprintf('Output should be an instance of "%s" when calling "%s".', ConsoleSectionOutput::class, __METHOD__));
Expand All @@ -290,7 +284,7 @@ public function appendRow($row): self
return $this;
}

public function setRow($column, array $row)
public function setRow(int|string $column, array $row)
{
$this->rows[$column] = $row;

Expand Down Expand Up @@ -596,11 +590,11 @@ private function buildTableRows(array $rows): TableRows

return new TableRows(function () use ($rows, $unmergedRows): \Traversable {
foreach ($rows as $rowKey => $row) {
yield $this->fillCells($row);
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);

if (isset($unmergedRows[$rowKey])) {
foreach ($unmergedRows[$rowKey] as $unmergedRow) {
yield $this->fillCells($unmergedRow);
foreach ($unmergedRows[$rowKey] as $row) {
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
}
}
}
Expand Down Expand Up @@ -681,7 +675,7 @@ private function fillNextRows(array $rows, int $line): array
/**
* fill cells for a row that contains colspan > 1.
*/
private function fillCells($row)
private function fillCells(iterable $row)
{
$newRow = [];

Expand Down Expand Up @@ -848,7 +842,7 @@ private static function initStyles(): array
];
}

private function resolveStyle($name): TableStyle
private function resolveStyle(TableStyle|string $name): TableStyle
{
if ($name instanceof TableStyle) {
return $name;
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private function parseArgument(string $token)
*
* @throws RuntimeException When option given doesn't exist
*/
private function addShortOption(string $shortcut, $value)
private function addShortOption(string $shortcut, mixed $value)
{
if (!$this->definition->hasShortcut($shortcut)) {
throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
Expand All @@ -206,7 +206,7 @@ private function addShortOption(string $shortcut, $value)
*
* @throws RuntimeException When option given doesn't exist
*/
private function addLongOption(string $name, $value)
private function addLongOption(string $name, mixed $value)
{
if (!$this->definition->hasOption($name)) {
if (!$this->definition->hasNegation($name)) {
Expand Down Expand Up @@ -294,7 +294,7 @@ public function getFirstArgument()
/**
* {@inheritdoc}
*/
public function hasParameterOption($values, bool $onlyParams = false)
public function hasParameterOption(string|array $values, bool $onlyParams = false)
{
$values = (array) $values;

Expand All @@ -319,7 +319,7 @@ public function hasParameterOption($values, bool $onlyParams = false)
/**
* {@inheritdoc}
*/
public function getParameterOption($values, $default = false, bool $onlyParams = false)
public function getParameterOption(string|array $values, mixed $default = false, bool $onlyParams = false)
{
$values = (array) $values;
$tokens = $this->tokens;
Expand Down
13 changes: 5 additions & 8 deletions src/Symfony/Component/Console/Input/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getFirstArgument()
/**
* {@inheritdoc}
*/
public function hasParameterOption($values, bool $onlyParams = false)
public function hasParameterOption(string|array $values, bool $onlyParams = false)
{
$values = (array) $values;

Expand All @@ -77,7 +77,7 @@ public function hasParameterOption($values, bool $onlyParams = false)
/**
* {@inheritdoc}
*/
public function getParameterOption($values, $default = false, bool $onlyParams = false)
public function getParameterOption(string|array $values, mixed $default = false, bool $onlyParams = false)
{
$values = (array) $values;

Expand Down Expand Up @@ -146,7 +146,7 @@ protected function parse()
*
* @throws InvalidOptionException When option given doesn't exist
*/
private function addShortOption(string $shortcut, $value)
private function addShortOption(string $shortcut, mixed $value)
{
if (!$this->definition->hasShortcut($shortcut)) {
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
Expand All @@ -161,7 +161,7 @@ private function addShortOption(string $shortcut, $value)
* @throws InvalidOptionException When option given doesn't exist
* @throws InvalidOptionException When a required value is missing
*/
private function addLongOption(string $name, $value)
private function addLongOption(string $name, mixed $value)
{
if (!$this->definition->hasOption($name)) {
if (!$this->definition->hasNegation($name)) {
Expand Down Expand Up @@ -192,12 +192,9 @@ private function addLongOption(string $name, $value)
/**
* Adds an argument value.
*
* @param string|int $name The argument name
* @param mixed $value The value for the argument
*
* @throws InvalidArgumentException When argument given doesn't exist
*/
private function addArgument($name, $value)
private function addArgument(string|int $name, mixed $value)
{
if (!$this->definition->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function getArgument(string $name)
/**
* {@inheritdoc}
*/
public function setArgument(string $name, $value)
public function setArgument(string $name, mixed $value)
{
if (!$this->definition->hasArgument($name)) {
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
Expand All @@ -128,7 +128,7 @@ public function setArgument(string $name, $value)
/**
* {@inheritdoc}
*/
public function hasArgument($name)
public function hasArgument(string|int $name)
{
return $this->definition->hasArgument($name);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public function getOption(string $name)
/**
* {@inheritdoc}
*/
public function setOption(string $name, $value)
public function setOption(string $name, mixed $value)
{
if ($this->definition->hasNegation($name)) {
$this->options[$this->definition->negationToName($name)] = !$value;
Expand Down
Loading