Skip to content

Commit d228d0c

Browse files
[Console] add union types
1 parent f4d1e4f commit d228d0c

29 files changed

+103
-176
lines changed

src/Symfony/Component/Console/Command/Command.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,9 @@ public function mergeApplicationDefinition(bool $mergeArgs = true)
371371
/**
372372
* Sets an array of argument and option instances.
373373
*
374-
* @param array|InputDefinition $definition An array of argument and option instances or a definition instance
375-
*
376374
* @return $this
377375
*/
378-
public function setDefinition($definition)
376+
public function setDefinition(array|InputDefinition $definition)
379377
{
380378
if ($definition instanceof InputDefinition) {
381379
$this->definition = $definition;
@@ -420,14 +418,14 @@ public function getNativeDefinition()
420418
/**
421419
* Adds an argument.
422420
*
423-
* @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
424-
* @param string|string[]|null $default The default value (for InputArgument::OPTIONAL mode only)
421+
* @param $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
422+
* @param $default The default value (for InputArgument::OPTIONAL mode only)
425423
*
426424
* @throws InvalidArgumentException When argument mode is not valid
427425
*
428426
* @return $this
429427
*/
430-
public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
428+
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null)
431429
{
432430
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
433431
if (null !== $this->fullDefinition) {
@@ -440,15 +438,15 @@ public function addArgument(string $name, int $mode = null, string $description
440438
/**
441439
* Adds an option.
442440
*
443-
* @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
444-
* @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
445-
* @param string|string[]|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
441+
* @param $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
442+
* @param $mode The option mode: One of the InputOption::VALUE_* constants
443+
* @param $default The default value (must be null for InputOption::VALUE_NONE)
446444
*
447445
* @throws InvalidArgumentException If option mode is invalid or incompatible
448446
*
449447
* @return $this
450448
*/
451-
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
449+
public function addOption(string $name, string|array|null $shortcut = null, int $mode = null, string $description = '', mixed $default = null)
452450
{
453451
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
454452
if (null !== $this->fullDefinition) {

src/Symfony/Component/Console/Command/LazyCommand.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function mergeApplicationDefinition(bool $mergeArgs = true): void
9090
/**
9191
* @return $this
9292
*/
93-
public function setDefinition($definition): self
93+
public function setDefinition(array|InputDefinition $definition): self
9494
{
9595
$this->getCommand()->setDefinition($definition);
9696

@@ -110,7 +110,7 @@ public function getNativeDefinition(): InputDefinition
110110
/**
111111
* @return $this
112112
*/
113-
public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self
113+
public function addArgument(string $name, int $mode = null, string $description = '', mixed $default = null): self
114114
{
115115
$this->getCommand()->addArgument($name, $mode, $description, $default);
116116

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

src/Symfony/Component/Console/Descriptor/Descriptor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ abstract class Descriptor implements DescriptorInterface
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function describe(OutputInterface $output, $object, array $options = [])
37+
public function describe(OutputInterface $output, object $object, array $options = [])
3838
{
3939
$this->output = $output;
4040

src/Symfony/Component/Console/Descriptor/DescriptorInterface.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ interface DescriptorInterface
2222
{
2323
/**
2424
* Describes an object if supported.
25-
*
26-
* @param object $object
2725
*/
28-
public function describe(OutputInterface $output, $object, array $options = []);
26+
public function describe(OutputInterface $output, object $object, array $options = []);
2927
}

src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MarkdownDescriptor extends Descriptor
3131
/**
3232
* {@inheritdoc}
3333
*/
34-
public function describe(OutputInterface $output, $object, array $options = [])
34+
public function describe(OutputInterface $output, object $object, array $options = [])
3535
{
3636
$decorated = $output->isDecorated();
3737
$output->setDecorated(false);

src/Symfony/Component/Console/Descriptor/TextDescriptor.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,8 @@ private function getCommandAliasesText(Command $command): string
273273

274274
/**
275275
* Formats input option/argument default value.
276-
*
277-
* @param mixed $default
278276
*/
279-
private function formatDefaultValue($default): string
277+
private function formatDefaultValue(mixed $default): string
280278
{
281279
if (\INF === $default) {
282280
return 'INF';

src/Symfony/Component/Console/Helper/Dumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(OutputInterface $output, CliDumper $dumper = null, C
5757
}
5858
}
5959

60-
public function __invoke($var): string
60+
public function __invoke(mixed $var): string
6161
{
6262
return ($this->handler)($var);
6363
}

src/Symfony/Component/Console/Helper/FormatterHelper.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ public function formatSection(string $section, string $message, string $style =
3333
/**
3434
* Formats a message as a block of text.
3535
*
36-
* @param string|array $messages The message to write in the block
37-
*
3836
* @return string The formatter message
3937
*/
40-
public function formatBlock($messages, string $style, bool $large = false)
38+
public function formatBlock(string|array $messages, string $style, bool $large = false)
4139
{
4240
if (!\is_array($messages)) {
4341
$messages = [$messages];

src/Symfony/Component/Console/Helper/Helper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static function substr(?string $string, int $from, int $length = null)
9393
return mb_substr($string, $from, $length, $encoding);
9494
}
9595

96-
public static function formatTime($secs)
96+
public static function formatTime(int|float $secs)
9797
{
9898
static $timeFormats = [
9999
[0, '< 1 sec'],

src/Symfony/Component/Console/Helper/ProcessHelper.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ class ProcessHelper extends Helper
3131
* @param array|Process $cmd An instance of Process or an array of the command and arguments
3232
* @param callable|null $callback A PHP callback to run whenever there is some
3333
* output available on STDOUT or STDERR
34-
*
35-
* @return Process The process that ran
3634
*/
37-
public function run(OutputInterface $output, $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
35+
public function run(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
3836
{
3937
if (!class_exists(Process::class)) {
4038
throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
@@ -50,10 +48,6 @@ public function run(OutputInterface $output, $cmd, string $error = null, callabl
5048
$cmd = [$cmd];
5149
}
5250

53-
if (!\is_array($cmd)) {
54-
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)));
55-
}
56-
5751
if (\is_string($cmd[0] ?? null)) {
5852
$process = new Process($cmd);
5953
$cmd = [];
@@ -96,13 +90,11 @@ public function run(OutputInterface $output, $cmd, string $error = null, callabl
9690
* @param callable|null $callback A PHP callback to run whenever there is some
9791
* output available on STDOUT or STDERR
9892
*
99-
* @return Process The process that ran
100-
*
10193
* @throws ProcessFailedException
10294
*
10395
* @see run()
10496
*/
105-
public function mustRun(OutputInterface $output, $cmd, string $error = null, callable $callback = null): Process
97+
public function mustRun(OutputInterface $output, string|Process $cmd, string $error = null, callable $callback = null): Process
10698
{
10799
$process = $this->run($output, $cmd, $error, $callback);
108100

src/Symfony/Component/Console/Helper/QuestionHelper.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,9 @@ private function setIOCodepage(): int
552552
/**
553553
* Sets console I/O to the specified code page and converts the user input.
554554
*
555-
* @param string|false $input
556-
*
557555
* @return string|false
558556
*/
559-
private function resetIOCodepage(int $cp, $input)
557+
private function resetIOCodepage(int $cp, string|false $input)
560558
{
561559
if (0 !== $cp) {
562560
sapi_windows_cp_set($cp);

src/Symfony/Component/Console/Helper/Table.php

+10-16
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ public static function getStyleDefinition(string $name)
133133
/**
134134
* Sets table style.
135135
*
136-
* @param TableStyle|string $name The style name or a TableStyle instance
137-
*
138136
* @return $this
139137
*/
140-
public function setStyle($name)
138+
public function setStyle(TableStyle|string $name)
141139
{
142140
$this->style = $this->resolveStyle($name);
143141

@@ -161,7 +159,7 @@ public function getStyle()
161159
*
162160
* @return $this
163161
*/
164-
public function setColumnStyle(int $columnIndex, $name)
162+
public function setColumnStyle(int $columnIndex, TableStyle|string $name)
165163
{
166164
$this->columnStyles[$columnIndex] = $this->resolveStyle($name);
167165

@@ -254,18 +252,14 @@ public function addRows(array $rows)
254252
return $this;
255253
}
256254

257-
public function addRow($row)
255+
public function addRow(TableSeparator|array $row)
258256
{
259257
if ($row instanceof TableSeparator) {
260258
$this->rows[] = $row;
261259

262260
return $this;
263261
}
264262

265-
if (!\is_array($row)) {
266-
throw new InvalidArgumentException('A row must be an array or a TableSeparator instance.');
267-
}
268-
269263
$this->rows[] = array_values($row);
270264

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

293-
public function setRow($column, array $row)
287+
public function setRow(int|string $column, array $row)
294288
{
295289
$this->rows[$column] = $row;
296290

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

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

601595
if (isset($unmergedRows[$rowKey])) {
602-
foreach ($unmergedRows[$rowKey] as $unmergedRow) {
603-
yield $this->fillCells($unmergedRow);
596+
foreach ($unmergedRows[$rowKey] as $row) {
597+
yield $row instanceof TableSeparator ? $row : $this->fillCells($row);
604598
}
605599
}
606600
}
@@ -681,7 +675,7 @@ private function fillNextRows(array $rows, int $line): array
681675
/**
682676
* fill cells for a row that contains colspan > 1.
683677
*/
684-
private function fillCells($row)
678+
private function fillCells(iterable $row)
685679
{
686680
$newRow = [];
687681

@@ -848,7 +842,7 @@ private static function initStyles(): array
848842
];
849843
}
850844

851-
private function resolveStyle($name): TableStyle
845+
private function resolveStyle(TableStyle|string $name): TableStyle
852846
{
853847
if ($name instanceof TableStyle) {
854848
return $name;

src/Symfony/Component/Console/Input/ArgvInput.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private function parseArgument(string $token)
192192
*
193193
* @throws RuntimeException When option given doesn't exist
194194
*/
195-
private function addShortOption(string $shortcut, $value)
195+
private function addShortOption(string $shortcut, mixed $value)
196196
{
197197
if (!$this->definition->hasShortcut($shortcut)) {
198198
throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
@@ -206,7 +206,7 @@ private function addShortOption(string $shortcut, $value)
206206
*
207207
* @throws RuntimeException When option given doesn't exist
208208
*/
209-
private function addLongOption(string $name, $value)
209+
private function addLongOption(string $name, mixed $value)
210210
{
211211
if (!$this->definition->hasOption($name)) {
212212
if (!$this->definition->hasNegation($name)) {
@@ -294,7 +294,7 @@ public function getFirstArgument()
294294
/**
295295
* {@inheritdoc}
296296
*/
297-
public function hasParameterOption($values, bool $onlyParams = false)
297+
public function hasParameterOption(string|array $values, bool $onlyParams = false)
298298
{
299299
$values = (array) $values;
300300

@@ -319,7 +319,7 @@ public function hasParameterOption($values, bool $onlyParams = false)
319319
/**
320320
* {@inheritdoc}
321321
*/
322-
public function getParameterOption($values, $default = false, bool $onlyParams = false)
322+
public function getParameterOption(string|array $values, mixed $default = false, bool $onlyParams = false)
323323
{
324324
$values = (array) $values;
325325
$tokens = $this->tokens;

src/Symfony/Component/Console/Input/ArrayInput.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getFirstArgument()
5353
/**
5454
* {@inheritdoc}
5555
*/
56-
public function hasParameterOption($values, bool $onlyParams = false)
56+
public function hasParameterOption(string|array $values, bool $onlyParams = false)
5757
{
5858
$values = (array) $values;
5959

@@ -77,7 +77,7 @@ public function hasParameterOption($values, bool $onlyParams = false)
7777
/**
7878
* {@inheritdoc}
7979
*/
80-
public function getParameterOption($values, $default = false, bool $onlyParams = false)
80+
public function getParameterOption(string|array $values, mixed $default = false, bool $onlyParams = false)
8181
{
8282
$values = (array) $values;
8383

@@ -146,7 +146,7 @@ protected function parse()
146146
*
147147
* @throws InvalidOptionException When option given doesn't exist
148148
*/
149-
private function addShortOption(string $shortcut, $value)
149+
private function addShortOption(string $shortcut, mixed $value)
150150
{
151151
if (!$this->definition->hasShortcut($shortcut)) {
152152
throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
@@ -161,7 +161,7 @@ private function addShortOption(string $shortcut, $value)
161161
* @throws InvalidOptionException When option given doesn't exist
162162
* @throws InvalidOptionException When a required value is missing
163163
*/
164-
private function addLongOption(string $name, $value)
164+
private function addLongOption(string $name, mixed $value)
165165
{
166166
if (!$this->definition->hasOption($name)) {
167167
if (!$this->definition->hasNegation($name)) {
@@ -192,12 +192,9 @@ private function addLongOption(string $name, $value)
192192
/**
193193
* Adds an argument value.
194194
*
195-
* @param string|int $name The argument name
196-
* @param mixed $value The value for the argument
197-
*
198195
* @throws InvalidArgumentException When argument given doesn't exist
199196
*/
200-
private function addArgument($name, $value)
197+
private function addArgument(string|int $name, mixed $value)
201198
{
202199
if (!$this->definition->hasArgument($name)) {
203200
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));

0 commit comments

Comments
 (0)