Skip to content

Commit cb2a5f4

Browse files
committed
[Console] Backport minor refactorings
Signed-off-by: Alexander M. Turek <[email protected]>
1 parent c33080a commit cb2a5f4

15 files changed

+62
-65
lines changed

Command/LockableTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Console\Command;
1313

1414
use Symfony\Component\Console\Exception\LogicException;
15-
use Symfony\Component\Lock\Lock;
1615
use Symfony\Component\Lock\LockFactory;
16+
use Symfony\Component\Lock\LockInterface;
1717
use Symfony\Component\Lock\Store\FlockStore;
1818
use Symfony\Component\Lock\Store\SemaphoreStore;
1919

@@ -24,7 +24,7 @@
2424
*/
2525
trait LockableTrait
2626
{
27-
/** @var Lock */
27+
/** @var LockInterface|null */
2828
private $lock;
2929

3030
/**

Cursor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ final class Cursor
2121
private $output;
2222
private $input;
2323

24+
/**
25+
* @param resource|null $input
26+
*/
2427
public function __construct(OutputInterface $output, $input = null)
2528
{
2629
$this->output = $output;

Descriptor/ApplicationDescription.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ class ApplicationDescription
3434
private $namespaces;
3535

3636
/**
37-
* @var Command[]
37+
* @var array<string, Command>
3838
*/
3939
private $commands;
4040

4141
/**
42-
* @var Command[]
42+
* @var array<string, Command>
4343
*/
4444
private $aliases;
4545

Formatter/NullOutputFormatter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ public function format(?string $message): ?string
3131
*/
3232
public function getStyle(string $name): OutputFormatterStyleInterface
3333
{
34-
if ($this->style) {
35-
return $this->style;
36-
}
3734
// to comply with the interface we must return a OutputFormatterStyleInterface
38-
return $this->style = new NullOutputFormatterStyle();
35+
return $this->style ?? $this->style = new NullOutputFormatterStyle();
3936
}
4037

4138
/**

Helper/DebugFormatterHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class DebugFormatterHelper extends Helper
2222
{
23-
private $colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default'];
23+
private const COLORS = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'default'];
2424
private $started = [];
2525
private $count = -1;
2626

@@ -31,7 +31,7 @@ class DebugFormatterHelper extends Helper
3131
*/
3232
public function start(string $id, string $message, string $prefix = 'RUN')
3333
{
34-
$this->started[$id] = ['border' => ++$this->count % \count($this->colors)];
34+
$this->started[$id] = ['border' => ++$this->count % \count(self::COLORS)];
3535

3636
return sprintf("%s<bg=blue;fg=white> %s </> <fg=blue>%s</>\n", $this->getBorder($id), $prefix, $message);
3737
}
@@ -94,7 +94,7 @@ public function stop(string $id, string $message, bool $successful, string $pref
9494

9595
private function getBorder(string $id): string
9696
{
97-
return sprintf('<bg=%s> </>', $this->colors[$this->started[$id]['border']]);
97+
return sprintf('<bg=%s> </>', self::COLORS[$this->started[$id]['border']]);
9898
}
9999

100100
/**

Helper/ProgressBar.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ public static function setPlaceholderFormatterDefinition(string $name, callable
114114
* Gets the placeholder formatter for a given name.
115115
*
116116
* @param string $name The placeholder name (including the delimiter char like %)
117-
*
118-
* @return callable|null
119117
*/
120118
public static function getPlaceholderFormatterDefinition(string $name): ?callable
121119
{
@@ -147,8 +145,6 @@ public static function setFormatDefinition(string $name, string $format): void
147145
* Gets the format for a given name.
148146
*
149147
* @param string $name The format name
150-
*
151-
* @return string|null
152148
*/
153149
public static function getFormatDefinition(string $name): ?string
154150
{
@@ -244,11 +240,7 @@ public function setBarCharacter(string $char)
244240

245241
public function getBarCharacter(): string
246242
{
247-
if (null === $this->barChar) {
248-
return $this->max ? '=' : $this->emptyBarChar;
249-
}
250-
251-
return $this->barChar;
243+
return $this->barChar ?? ($this->max ? '=' : $this->emptyBarChar);
252244
}
253245

254246
public function setEmptyBarCharacter(string $char)

Helper/ProgressIndicator.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
*/
2121
class ProgressIndicator
2222
{
23+
private const FORMATS = [
24+
'normal' => ' %indicator% %message%',
25+
'normal_no_ansi' => ' %message%',
26+
27+
'verbose' => ' %indicator% %message% (%elapsed:6s%)',
28+
'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
29+
30+
'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
31+
'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
32+
];
33+
2334
private $output;
2435
private $startTime;
2536
private $format;
@@ -30,8 +41,10 @@ class ProgressIndicator
3041
private $indicatorUpdateTime;
3142
private $started = false;
3243

44+
/**
45+
* @var array<string, callable>
46+
*/
3347
private static $formatters;
34-
private static $formats;
3548

3649
/**
3750
* @param int $indicatorChangeInterval Change interval in milliseconds
@@ -138,11 +151,7 @@ public function finish(string $message)
138151
*/
139152
public static function getFormatDefinition(string $name)
140153
{
141-
if (!self::$formats) {
142-
self::$formats = self::initFormats();
143-
}
144-
145-
return self::$formats[$name] ?? null;
154+
return self::FORMATS[$name] ?? null;
146155
}
147156

148157
/**
@@ -237,18 +246,4 @@ private static function initPlaceholderFormatters(): array
237246
},
238247
];
239248
}
240-
241-
private static function initFormats(): array
242-
{
243-
return [
244-
'normal' => ' %indicator% %message%',
245-
'normal_no_ansi' => ' %message%',
246-
247-
'verbose' => ' %indicator% %message% (%elapsed:6s%)',
248-
'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
249-
250-
'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
251-
'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
252-
];
253-
}
254249
}

Helper/QuestionHelper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
*/
3434
class QuestionHelper extends Helper
3535
{
36+
/**
37+
* @var resource|null
38+
*/
3639
private $inputStream;
37-
private static $shell;
40+
3841
private static $stty = true;
3942
private static $stdinIsInteractive;
4043

Helper/Table.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class Table
8585
private $columnWidths = [];
8686
private $columnMaxWidths = [];
8787

88+
/**
89+
* @var array<string, TableStyle>|null
90+
*/
8891
private static $styles;
8992

9093
private $rendered = false;
@@ -803,6 +806,9 @@ private function cleanup()
803806
$this->numberOfColumns = null;
804807
}
805808

809+
/**
810+
* @return array<string, TableStyle>
811+
*/
806812
private static function initStyles(): array
807813
{
808814
$borderless = new TableStyle();

Helper/TableCellStyle.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@ class TableCellStyle
2020
{
2121
public const DEFAULT_ALIGN = 'left';
2222

23-
private $options = [
24-
'fg' => 'default',
25-
'bg' => 'default',
26-
'options' => null,
27-
'align' => self::DEFAULT_ALIGN,
28-
'cellFormat' => null,
29-
];
30-
31-
private $tagOptions = [
23+
private const TAG_OPTIONS = [
3224
'fg',
3325
'bg',
3426
'options',
3527
];
3628

37-
private $alignMap = [
29+
private const ALIGN_MAP = [
3830
'left' => \STR_PAD_RIGHT,
3931
'center' => \STR_PAD_BOTH,
4032
'right' => \STR_PAD_LEFT,
4133
];
4234

35+
private $options = [
36+
'fg' => 'default',
37+
'bg' => 'default',
38+
'options' => null,
39+
'align' => self::DEFAULT_ALIGN,
40+
'cellFormat' => null,
41+
];
42+
4343
public function __construct(array $options = [])
4444
{
4545
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
4646
throw new InvalidArgumentException(sprintf('The TableCellStyle does not support the following options: \'%s\'.', implode('\', \'', $diff)));
4747
}
4848

49-
if (isset($options['align']) && !\array_key_exists($options['align'], $this->alignMap)) {
50-
throw new InvalidArgumentException(sprintf('Wrong align value. Value must be following: \'%s\'.', implode('\', \'', array_keys($this->alignMap))));
49+
if (isset($options['align']) && !\array_key_exists($options['align'], self::ALIGN_MAP)) {
50+
throw new InvalidArgumentException(sprintf('Wrong align value. Value must be following: \'%s\'.', implode('\', \'', array_keys(self::ALIGN_MAP))));
5151
}
5252

5353
$this->options = array_merge($this->options, $options);
@@ -68,15 +68,18 @@ public function getTagOptions()
6868
return array_filter(
6969
$this->getOptions(),
7070
function ($key) {
71-
return \in_array($key, $this->tagOptions) && isset($this->options[$key]);
71+
return \in_array($key, self::TAG_OPTIONS) && isset($this->options[$key]);
7272
},
7373
\ARRAY_FILTER_USE_KEY
7474
);
7575
}
7676

77+
/**
78+
* @return int
79+
*/
7780
public function getPadByAlign()
7881
{
79-
return $this->alignMap[$this->getOptions()['align']];
82+
return self::ALIGN_MAP[$this->getOptions()['align']];
8083
}
8184

8285
public function getCellFormat(): ?string

Helper/TableRows.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ class TableRows implements \IteratorAggregate
1818
{
1919
private $generator;
2020

21-
public function __construct(callable $generator)
21+
public function __construct(\Closure $generator)
2222
{
2323
$this->generator = $generator;
2424
}
2525

2626
public function getIterator(): \Traversable
2727
{
28-
$g = $this->generator;
29-
30-
return $g();
28+
return ($this->generator)();
3129
}
3230
}

Output/Output.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ abstract class Output implements OutputInterface
3333
private $formatter;
3434

3535
/**
36-
* @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
36+
* @param int|null $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
3737
* @param bool $decorated Whether to decorate messages
3838
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
3939
*/
4040
public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null)
4141
{
42-
$this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
42+
$this->verbosity = $verbosity ?? self::VERBOSITY_NORMAL;
4343
$this->formatter = $formatter ?? new OutputFormatter();
4444
$this->formatter->setDecorated($decorated);
4545
}

Tester/ApplicationTester.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class ApplicationTester
2929
use TesterTrait;
3030

3131
private $application;
32-
private $input;
33-
private $statusCode;
3432

3533
public function __construct(Application $application)
3634
{

Tester/CommandTester.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class CommandTester
2525
use TesterTrait;
2626

2727
private $command;
28-
private $input;
29-
private $statusCode;
3028

3129
public function __construct(Command $command)
3230
{

Tester/TesterTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ trait TesterTrait
2727
private $output;
2828
private $inputs = [];
2929
private $captureStreamsIndependently = false;
30+
/** @var InputInterface */
31+
private $input;
32+
/** @var int */
33+
private $statusCode;
3034

3135
/**
3236
* Gets the display returned by the last execution of the command or application.

0 commit comments

Comments
 (0)