From 8ada55c07c44a399e48bba7f5f9289ebb934b3dd Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Tue, 16 Mar 2021 22:19:58 -0400 Subject: [PATCH 1/5] Correctly clear lines for multi-line progress bar messages. --- .../Component/Console/Helper/ProgressBar.php | 11 ++++++-- .../Console/Tests/Helper/ProgressBarTest.php | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/ProgressBar.php b/src/Symfony/Component/Console/Helper/ProgressBar.php index 824c0f2963..5049c7dae0 100644 --- a/src/Symfony/Component/Console/Helper/ProgressBar.php +++ b/src/Symfony/Component/Console/Helper/ProgressBar.php @@ -441,8 +441,15 @@ final class ProgressBar if ($this->overwrite) { if (null !== $this->previousMessage) { if ($this->output instanceof ConsoleSectionOutput) { - $lines = floor(Helper::strlenWithoutDecoration($this->output->getFormatter(), $message) / $this->terminal->getWidth()) + $this->formatLineCount + 1; - $this->output->clear($lines); + $messageLines = explode("\n", $message); + $lineCount = \count($messageLines); + foreach ($messageLines as $messageLine) { + $messageLineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $messageLine); + if ($messageLineLength > $this->terminal->getWidth()) { + $lineCount += floor($messageLineLength / $this->terminal->getWidth()); + } + } + $this->output->clear($lineCount); } else { // Erase previous lines if ($this->formatLineCount > 0) { diff --git a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php index aabadef2cd..2d32e1c3d5 100644 --- a/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php @@ -397,6 +397,34 @@ class ProgressBarTest extends TestCase ); } + public function testOverwriteWithSectionOutputWithNewlinesInMessage() + { + $sections = []; + $stream = $this->getOutputStream(true); + $output = new ConsoleSectionOutput($stream->getStream(), $sections, $stream->getVerbosity(), $stream->isDecorated(), new OutputFormatter()); + + 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 = new ProgressBar($output, 50, 0); + $bar->setFormat('test'); + $bar->start(); + $bar->display(); + $bar->setMessage("Twas brillig, and the slithy toves. Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe.\nBeware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!"); + $bar->advance(); + $bar->setMessage("He took his vorpal sword in hand; Long time the manxome foe he sought— So rested he by the Tumtum tree And stood awhile in thought.\nAnd, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came!"); + $bar->advance(); + + rewind($output->getStream()); + $this->assertEquals( + ' 0/50 [>] 0% %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.'.\PHP_EOL. + "\x1b[6A\x1b[0J 1/50 [>] 2% Twas brillig, and the slithy toves. Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe. +Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch! 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.".\PHP_EOL. + "\x1b[6A\x1b[0J 2/50 [>] 4% He took his vorpal sword in hand; Long time the manxome foe he sought— So rested he by the Tumtum tree And stood awhile in thought. +And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came! 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.".\PHP_EOL, + stream_get_contents($output->getStream()) + ); + } + public function testMultipleSectionsWithCustomFormat() { $sections = []; From 1a11491f6e25bef21d318900a8e107f917c5defd Mon Sep 17 00:00:00 2001 From: Michael Nelson Date: Fri, 12 Mar 2021 00:49:32 -0500 Subject: [PATCH 2/5] [VarDumper] Adds support for ReflectionUnionType to VarDumper --- .../VarDumper/Caster/ReflectionCaster.php | 21 +++- .../Tests/Caster/ReflectionCasterTest.php | 103 +++++++++++++++++- .../Fixtures/ExtendsReflectionTypeFixture.php | 21 ++++ .../Fixtures/ReflectionNamedTypeFixture.php | 8 ++ .../Fixtures/ReflectionUnionTypeFixture.php | 8 ++ 5 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/VarDumper/Tests/Fixtures/ExtendsReflectionTypeFixture.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionNamedTypeFixture.php create mode 100644 src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionUnionTypeFixture.php diff --git a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php index 8d5f428e51..95c1dbf6ff 100644 --- a/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php @@ -96,11 +96,20 @@ class ReflectionCaster { $prefix = Caster::PREFIX_VIRTUAL; - $a += [ - $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c, - $prefix.'allowsNull' => $c->allowsNull(), - $prefix.'isBuiltin' => $c->isBuiltin(), - ]; + if ($c instanceof \ReflectionNamedType || \PHP_VERSION_ID < 80000) { + $a += [ + $prefix.'name' => $c instanceof \ReflectionNamedType ? $c->getName() : (string) $c, + $prefix.'allowsNull' => $c->allowsNull(), + $prefix.'isBuiltin' => $c->isBuiltin(), + ]; + } elseif ($c instanceof \ReflectionUnionType) { + $a[$prefix.'allowsNull'] = $c->allowsNull(); + self::addMap($a, $c, [ + 'types' => 'getTypes', + ]); + } else { + $a[$prefix.'allowsNull'] = $c->allowsNull(); + } return $a; } @@ -377,7 +386,7 @@ class ReflectionCaster } } - private static function addMap(array &$a, \Reflector $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL) + private static function addMap(array &$a, $c, array $map, string $prefix = Caster::PREFIX_VIRTUAL) { foreach ($map as $k => $m) { if (\PHP_VERSION_ID >= 80000 && 'isDisabled' === $k) { diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php index 9fc8dcaf0d..3f5497573a 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php @@ -14,8 +14,11 @@ namespace Symfony\Component\VarDumper\Tests\Caster; use PHPUnit\Framework\TestCase; use Symfony\Component\VarDumper\Caster\Caster; use Symfony\Component\VarDumper\Test\VarDumperTestTrait; +use Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture; use Symfony\Component\VarDumper\Tests\Fixtures\GeneratorDemo; use Symfony\Component\VarDumper\Tests\Fixtures\NotLoadableClass; +use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture; +use Symfony\Component\VarDumper\Tests\Fixtures\ReflectionUnionTypeFixture; /** * @author Nicolas Grekas @@ -75,7 +78,7 @@ Closure($x) { $b: & 123 } file: "%sReflectionCasterTest.php" - line: "68 to 68" + line: "71 to 71" } EOTXT , $var @@ -211,6 +214,104 @@ EOTXT ); } + /** + * @requires PHP 7.4 + */ + public function testReflectionPropertyScalar() + { + $var = new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a'); + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionProperty { + +name: "a" + +class: "Symfony\Component\VarDumper\Tests\Fixtures\ReflectionNamedTypeFixture" + modifiers: "public" +} +EOTXT + , $var + ); + } + + /** + * @requires PHP 7.4 + */ + public function testReflectionNamedType() + { + $var = (new \ReflectionProperty(ReflectionNamedTypeFixture::class, 'a'))->getType(); + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionNamedType { + name: "int" + allowsNull: false + isBuiltin: true +} +EOTXT + , $var + ); + } + + /** + * @requires PHP 8 + */ + public function testReflectionUnionType() + { + $var = (new \ReflectionProperty(ReflectionUnionTypeFixture::class, 'a'))->getType(); + $this->assertDumpMatchesFormat( + <<<'EOTXT' +ReflectionUnionType { + allowsNull: false + types: array:2 [ + 0 => ReflectionNamedType { + name: "string" + allowsNull: false + isBuiltin: true + } + 1 => ReflectionNamedType { + name: "int" + allowsNull: false + isBuiltin: true + } + ] +} +EOTXT + , $var + ); + } + + /** + * @requires PHP 8 + */ + public function testExtendsReflectionType() + { + $var = new ExtendsReflectionTypeFixture(); + $this->assertDumpMatchesFormat( + <<<'EOTXT' +Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture { + allowsNull: false +} +EOTXT + , $var + ); + } + + /** + * @requires PHP < 8 + */ + public function testLegacyExtendsReflectionType() + { + $var = new ExtendsReflectionTypeFixture(); + $this->assertDumpMatchesFormat( + <<<'EOTXT' +Symfony\Component\VarDumper\Tests\Fixtures\ExtendsReflectionTypeFixture { + name: "fake" + allowsNull: false + isBuiltin: false +} +EOTXT + , $var + ); + } + public function testReturnType() { $f = eval('return function ():int {};'); diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/ExtendsReflectionTypeFixture.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/ExtendsReflectionTypeFixture.php new file mode 100644 index 0000000000..1ee3d8c384 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/ExtendsReflectionTypeFixture.php @@ -0,0 +1,21 @@ + Date: Wed, 17 Mar 2021 09:49:43 +0100 Subject: [PATCH 3/5] [PhpUnitBridge] fix compat with symfony/debug --- .../DeprecationErrorHandler/Deprecation.php | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php index ac1a95ef41..3ef1ee280a 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php @@ -11,6 +11,8 @@ namespace Symfony\Bridge\PhpUnit\DeprecationErrorHandler; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestSuite; use PHPUnit\Util\Test; use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor; use Symfony\Component\Debug\DebugClassLoader as LegacyDebugClassLoader; @@ -82,37 +84,41 @@ class Deprecation } } - if (isset($line['object']) || isset($line['class'])) { - if (!isset($line['class'], $trace[$i - 2]['function']) || 0 !== strpos($line['class'], SymfonyTestsListenerFor::class)) { - $this->originClass = isset($line['object']) ? \get_class($line['object']) : $line['class']; - $this->originMethod = $line['function']; + if (!isset($line['object']) && !isset($line['class'])) { + return; + } - return; - } + if (!isset($line['class'], $trace[$i - 2]['function']) || 0 !== strpos($line['class'], SymfonyTestsListenerFor::class)) { + $this->originClass = isset($line['object']) ? \get_class($line['object']) : $line['class']; + $this->originMethod = $line['function']; - if ('trigger_error' !== $trace[$i - 2]['function'] || isset($trace[$i - 2]['class'])) { - $this->originClass = \get_class($line['args'][0]); - $this->originMethod = $line['args'][0]->getName(); + return; + } - return; - } + $test = isset($line['args'][0]) ? $line['args'][0] : null; - set_error_handler(function () {}); - $parsedMsg = unserialize($this->message); - restore_error_handler(); - $this->message = $parsedMsg['deprecation']; - $this->originClass = $parsedMsg['class']; - $this->originMethod = $parsedMsg['method']; - if (isset($parsedMsg['files_stack'])) { - $this->originalFilesStack = $parsedMsg['files_stack']; - } - // If the deprecation has been triggered via - // \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest() - // then we need to use the serialized information to determine - // if the error has been triggered from vendor code. - if (isset($parsedMsg['triggering_file'])) { - $this->triggeringFile = $parsedMsg['triggering_file']; - } + if (($test instanceof TestCase || $test instanceof TestSuite) && ('trigger_error' !== $trace[$i - 2]['function'] || isset($trace[$i - 2]['class']))) { + $this->originClass = \get_class($line['args'][0]); + $this->originMethod = $line['args'][0]->getName(); + + return; + } + + set_error_handler(function () {}); + $parsedMsg = unserialize($this->message); + restore_error_handler(); + $this->message = $parsedMsg['deprecation']; + $this->originClass = $parsedMsg['class']; + $this->originMethod = $parsedMsg['method']; + if (isset($parsedMsg['files_stack'])) { + $this->originalFilesStack = $parsedMsg['files_stack']; + } + // If the deprecation has been triggered via + // \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest() + // then we need to use the serialized information to determine + // if the error has been triggered from vendor code. + if (isset($parsedMsg['triggering_file'])) { + $this->triggeringFile = $parsedMsg['triggering_file']; } } From d3412e919f42e04614028ec90635a5da05ace4a4 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Wed, 17 Mar 2021 16:36:05 +0100 Subject: [PATCH 4/5] [Inflector] Fixed pluralize "coupon" --- src/Symfony/Component/Inflector/Inflector.php | 3 +++ src/Symfony/Component/Inflector/Tests/InflectorTest.php | 1 + 2 files changed, 4 insertions(+) diff --git a/src/Symfony/Component/Inflector/Inflector.php b/src/Symfony/Component/Inflector/Inflector.php index 8d2323cc4b..461c5f6090 100644 --- a/src/Symfony/Component/Inflector/Inflector.php +++ b/src/Symfony/Component/Inflector/Inflector.php @@ -231,6 +231,9 @@ final class Inflector // bacteria (bacterium), criteria (criterion), phenomena (phenomenon) ['noi', 3, true, true, 'ions'], + // coupon (coupons) + ['nop', 3, true, true, 'pons'], + // seasons (season), treasons (treason), poisons (poison), lessons (lesson) ['nos', 3, true, true, 'sons'], diff --git a/src/Symfony/Component/Inflector/Tests/InflectorTest.php b/src/Symfony/Component/Inflector/Tests/InflectorTest.php index a3c22ac0f5..62b3790fd3 100644 --- a/src/Symfony/Component/Inflector/Tests/InflectorTest.php +++ b/src/Symfony/Component/Inflector/Tests/InflectorTest.php @@ -201,6 +201,7 @@ class InflectorTest extends TestCase ['crisis', 'crises'], ['criteria', 'criterion'], ['cup', 'cups'], + ['coupon', 'coupons'], ['data', 'data'], ['day', 'days'], ['disco', 'discos'], From 760be8831001282833fd7e1254a09ed089472874 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Tue, 2 Mar 2021 16:31:24 -0500 Subject: [PATCH 5/5] [Console] Fix line wrapping for decorated text in block output --- src/Symfony/Component/Console/Style/SymfonyStyle.php | 11 +++++++++-- .../Fixtures/Style/SymfonyStyle/output/output_13.txt | 10 +++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 2056f04c99..ba89fb4220 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -471,7 +471,12 @@ class SymfonyStyle extends OutputStyle $message = OutputFormatter::escape($message); } - $lines = array_merge($lines, explode(\PHP_EOL, wordwrap($message, $this->lineLength - $prefixLength - $indentLength, \PHP_EOL, true))); + $decorationLength = Helper::strlen($message) - Helper::strlenWithoutDecoration($this->getFormatter(), $message); + $messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength); + $messageLines = explode(\PHP_EOL, wordwrap($message, $messageLineLength, \PHP_EOL, true)); + foreach ($messageLines as $messageLine) { + $lines[] = $messageLine; + } if (\count($messages) > 1 && $key < \count($messages) - 1) { $lines[] = ''; @@ -491,7 +496,9 @@ class SymfonyStyle extends OutputStyle } $line = $prefix.$line; - $line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line)); + $decorationLength = Helper::strlen($line) - Helper::strlenWithoutDecoration($this->getFormatter(), $line); + $messageLineLength = min($this->lineLength - $prefixLength - $indentLength + $decorationLength, $this->lineLength); + $line .= str_repeat(' ', max($this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line), 0)); if ($style) { $line = sprintf('<%s>%s', $style, $line); diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt index 0f3704b748..ea8e4351ea 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_13.txt @@ -1,7 +1,7 @@ - // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et  - // dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea  - // commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla  - // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim - // id est laborum + // Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore  + // magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  + // consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla  + // pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id + // est laborum