diff --git a/Descriptor/Descriptor.php b/Descriptor/Descriptor.php index 2834cd0aa..2ecc59e4e 100644 --- a/Descriptor/Descriptor.php +++ b/Descriptor/Descriptor.php @@ -69,36 +69,26 @@ protected function write(string $content, bool $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 07aef2a31..ccfbd6d36 100644 --- a/Descriptor/TextDescriptor.php +++ b/Descriptor/TextDescriptor.php @@ -296,7 +296,7 @@ private function formatDefaultValue($default): string } /** - * @param (Command|string)[] $commands + * @param array $commands */ private function getColumnWidth(array $commands): int { 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/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/QuestionHelper.php b/Helper/QuestionHelper.php index 5bf8186b8..72880f6a6 100644 --- a/Helper/QuestionHelper.php +++ b/Helper/QuestionHelper.php @@ -99,7 +99,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 */ 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(); 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; diff --git a/Output/Output.php b/Output/Output.php index ed13d58fc..1b01472cd 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); } 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 */