From 1b95e23435ab025895d9ff4c2d867aef06aaeab7 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Wed, 7 Apr 2021 17:14:41 +0200 Subject: [PATCH 1/5] [CS] Replace easy occurences of ?: with ?? --- Formatter/OutputFormatterStyleStack.php | 2 +- Output/Output.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Formatter/OutputFormatterStyleStack.php b/Formatter/OutputFormatterStyleStack.php index 33f7d5222..fc48dc0e1 100644 --- a/Formatter/OutputFormatterStyleStack.php +++ b/Formatter/OutputFormatterStyleStack.php @@ -28,7 +28,7 @@ class OutputFormatterStyleStack implements ResetInterface public function __construct(OutputFormatterStyleInterface $emptyStyle = null) { - $this->emptyStyle = $emptyStyle ?: new OutputFormatterStyle(); + $this->emptyStyle = $emptyStyle ?? new OutputFormatterStyle(); $this->reset(); } diff --git a/Output/Output.php b/Output/Output.php index 9dd765113..857248133 100644 --- a/Output/Output.php +++ b/Output/Output.php @@ -40,7 +40,7 @@ abstract class Output implements OutputInterface public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null) { $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity; - $this->formatter = $formatter ?: new OutputFormatter(); + $this->formatter = $formatter ?? new OutputFormatter(); $this->formatter->setDecorated($decorated); } From d53ddf9bafb578242cc3a2c53d894f210739e2c4 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Wed, 7 Apr 2021 18:12:22 +0200 Subject: [PATCH 2/5] [PHPDoc] Fix some union type cases --- Descriptor/Descriptor.php | 10 ---------- Descriptor/TextDescriptor.php | 2 +- Helper/QuestionHelper.php | 2 +- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/Descriptor/Descriptor.php b/Descriptor/Descriptor.php index d25a708e4..9c3878d1e 100644 --- a/Descriptor/Descriptor.php +++ b/Descriptor/Descriptor.php @@ -72,36 +72,26 @@ protected function write($content, $decorated = false) /** * Describes an InputArgument instance. - * - * @return string|mixed */ abstract protected function describeInputArgument(InputArgument $argument, array $options = []); /** * Describes an InputOption instance. - * - * @return string|mixed */ abstract protected function describeInputOption(InputOption $option, array $options = []); /** * Describes an InputDefinition instance. - * - * @return string|mixed */ abstract protected function describeInputDefinition(InputDefinition $definition, array $options = []); /** * Describes a Command instance. - * - * @return string|mixed */ abstract protected function describeCommand(Command $command, array $options = []); /** * Describes an Application instance. - * - * @return string|mixed */ abstract protected function describeApplication(Application $application, array $options = []); } diff --git a/Descriptor/TextDescriptor.php b/Descriptor/TextDescriptor.php index 1db3686be..220bd2e68 100644 --- a/Descriptor/TextDescriptor.php +++ b/Descriptor/TextDescriptor.php @@ -298,7 +298,7 @@ private function formatDefaultValue($default): string } /** - * @param (Command|string)[] $commands + * @param array $commands */ private function getColumnWidth(array $commands): int { diff --git a/Helper/QuestionHelper.php b/Helper/QuestionHelper.php index 6dde580cf..f1a3c4b5d 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -97,7 +97,7 @@ public static function disableStty() /** * Asks the question to the user. * - * @return bool|mixed|string|null + * @return mixed * * @throws RuntimeException In case the fallback is deactivated and the response cannot be hidden */ From f0671efd0e144681fd74ac1208ca0b5da8591b8a Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sat, 3 Apr 2021 15:14:33 +0200 Subject: [PATCH 3/5] [Console] Add Helper::strwidth() and Helper::strlength() --- Helper/Helper.php | 40 +++++++++++++++++++++++++++++++++------- Helper/ProgressBar.php | 2 +- Helper/Table.php | 2 +- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Helper/Helper.php b/Helper/Helper.php index acec994db..e4f9ca907 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -45,6 +45,17 @@ public function getHelperSet() * @return int The length of the string */ public static function strlen(?string $string) + { + return self::width($string); + } + + /** + * Returns the width of a string, using mb_strwidth if it is available. + * The width is how many characters positions the string will use. + * + * @internal in Symfony 5.2 + */ + public static function width(?string $string): int { $string ?? $string = ''; @@ -59,6 +70,27 @@ public static function strlen(?string $string) return mb_strwidth($string, $encoding); } + /** + * Returns the length of a string, using mb_strlen if it is available. + * The length is related to how many bytes the string will use. + * + * @internal in Symfony 5.2 + */ + public static function length(?string $string): int + { + $string ?? $string = ''; + + if (preg_match('//u', $string)) { + return (new UnicodeString($string))->length(); + } + + if (false === $encoding = mb_detect_encoding($string, null, true)) { + return \strlen($string); + } + + return mb_strlen($string, $encoding); + } + /** * Returns the subset of a string, using mb_substr if it is available. * @@ -123,13 +155,7 @@ public static function formatMemory(int $memory) public static function strlenWithoutDecoration(OutputFormatterInterface $formatter, ?string $string) { - $string = self::removeDecoration($formatter, $string); - - if (preg_match('//u', $string)) { - return (new UnicodeString($string))->width(true); - } - - return self::strlen($string); + return self::width(self::removeDecoration($formatter, $string)); } public static function removeDecoration(OutputFormatterInterface $formatter, ?string $string) diff --git a/Helper/ProgressBar.php b/Helper/ProgressBar.php index 61c471424..fb9036925 100644 --- a/Helper/ProgressBar.php +++ b/Helper/ProgressBar.php @@ -513,7 +513,7 @@ private static function initPlaceholderFormatters(): array $completeBars = $bar->getBarOffset(); $display = str_repeat($bar->getBarCharacter(), $completeBars); if ($completeBars < $bar->getBarWidth()) { - $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter()); + $emptyBars = $bar->getBarWidth() - $completeBars - Helper::length(Helper::removeDecoration($output->getFormatter(), $bar->getProgressCharacter())); $display .= $bar->getProgressCharacter().str_repeat($bar->getEmptyBarCharacter(), $emptyBars); } diff --git a/Helper/Table.php b/Helper/Table.php index 041145403..ee969bcff 100644 --- a/Helper/Table.php +++ b/Helper/Table.php @@ -511,7 +511,7 @@ private function renderCell(array $row, int $column, string $cellFormat): string return sprintf($style->getBorderFormat(), str_repeat($style->getBorderChars()[2], $width)); } - $width += Helper::strlen($cell) - Helper::strlenWithoutDecoration($this->output->getFormatter(), $cell); + $width += Helper::length($cell) - Helper::length(Helper::removeDecoration($this->output->getFormatter(), $cell)); $content = sprintf($style->getCellRowContentFormat(), $cell); $padType = $style->getPadType(); From 334155854b1edf4a291b84ac61e92e7d9eac5795 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Wed, 31 Mar 2021 16:54:00 -0400 Subject: [PATCH 4/5] Add test. --- Tests/Helper/ProgressBarTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Tests/Helper/ProgressBarTest.php b/Tests/Helper/ProgressBarTest.php index 8baf5a6d8..71b6c7850 100644 --- a/Tests/Helper/ProgressBarTest.php +++ b/Tests/Helper/ProgressBarTest.php @@ -899,6 +899,21 @@ public function testSetFormat() ); } + public function testUnicode() + { + $bar = new ProgressBar($output = $this->getOutputStream(), 10, 0); + ProgressBar::setFormatDefinition('test', '%current%/%max% [%bar%] %percent:3s%% %message% Fruitcake marzipan toffee. Cupcake gummi bears tart dessert ice cream chupa chups cupcake chocolate bar sesame snaps. Croissant halvah cookie jujubes powder macaroon. Fruitcake bear claw bonbon jelly beans oat cake pie muffin Fruitcake marzipan toffee.'); + $bar->setFormat('test'); + $bar->setProgressCharacter('💧'); + $bar->start(); + rewind($output->getStream()); + $this->assertStringContainsString( + ' 0/10 [💧] 0%', + stream_get_contents($output->getStream()) + ); + $bar->finish(); + } + /** * @dataProvider provideFormat */ From 36bbd079b69b94bcc9c9c9e1e37ca3b1e7971625 Mon Sep 17 00:00:00 2001 From: Markus Staab <47448731+clxmstaab@users.noreply.github.com> Date: Fri, 16 Apr 2021 09:18:02 +0200 Subject: [PATCH 5/5] [Console] : added phpdocs to InputOption constants --- Input/InputOption.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Input/InputOption.php b/Input/InputOption.php index a8e956db5..5e48f88b8 100644 --- a/Input/InputOption.php +++ b/Input/InputOption.php @@ -21,9 +21,24 @@ */ class InputOption { + /** + * Do not accept input for the option (e.g. --yell). This is the default behavior of options. + */ public const VALUE_NONE = 1; + + /** + * A value must be passed when the option is used (e.g. --iterations=5 or -i5). + */ public const VALUE_REQUIRED = 2; + + /** + * The option may or may not have a value (e.g. --yell or --yell=loud). + */ public const VALUE_OPTIONAL = 4; + + /** + * The option accepts multiple values (e.g. --dir=/foo --dir=/bar). + */ public const VALUE_IS_ARRAY = 8; private $name;