Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] fix trailling comma when dumping an exception #24735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2017

Conversation

Simperfit
Copy link
Contributor

Q A
Branch? 3.3
Bug fix? yesish
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #24581
License MIT
Doc PR O

This PR is fixing a behaviour when a exception got every single line with a comma.

Copy link
Contributor

@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More broadly: it only uses the trailing comma for actual arrays, which is what targeted the initial feature anyway. Not adding a trailing comma to each line.

👍

@nicolas-grekas
Copy link
Member

Not sure: as @lyrixx mentioned, the comma is needed when dumping on a single line. Then, it's also useful for objects, isn't it?

@ogizanagi
Copy link
Contributor

ogizanagi commented Oct 29, 2017

Perhaps, but AFAIU, nested structures aren't dumped inlined, and that's probably not something we should target as it'll be pretty unreadable and useless to me.

See format.php code
#!/usr/bin/env php
<?php

use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\OptionsResolver\OptionsResolver;

require __DIR__.'/vendor/autoload.php';

$input = new ArgvInput(null, (new InputDefinition([new InputOption('multiline')])));

$formatter = new ConsoleFormatter(['multiline' => $input->getOption('multiline')]);

$std = new stdClass();
$std->foo = 123;
$std->bar = 'bar';

(new ConsoleOutput())->writeln($formatter->format([
    'level' => 100,
    'level_name' => 'info',
    'message' => 'foo',
    'datetime' => new \DateTime(),
    'channel' => 'app',
    'context' => [
        'date' => new \DateTime(),
        'opt' => new OptionsResolver(),
        'std' => $std,
    ],
    'extra' => [],
]));

./format.php will output:

10:46:31 info      [app] foo ["date" => DateTime @1509270391 {date: 2017-10-29 10:46:31.987557 Europe/Zurich (+01:00)},"opt" => Symfony\Component\OptionsResolver\OptionsResolver { …},"std" => { …}] []

whereas ./format.php --multiline:

10:46:38 info      [app] foo
[
  "date" => DateTime @1509270398 {
    date: 2017-10-29 10:46:38.564727 Europe/Zurich (+01:00)
  },
  "opt" => Symfony\Component\OptionsResolver\OptionsResolver {
    -defined: []
    -defaults: []
    -required: []
    -resolved: []
    -normalizers: []
    -allowedValues: []
    -allowedTypes: []
    -lazy: []
    -calling: []
    -locked: false
  },
  "std" => {
    +"foo": 123
    +"bar": "bar"
  }
]
[]

So IMHO it's okay.

@@ -494,9 +495,10 @@ protected function dumpLine($depth, $endOfValue = false)

protected function endValue(Cursor $cursor)
{
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
$isStubArray = (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be an "if" that wraps the other "if"s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYM?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
index 48f9dc1108..5bf89112e6 100644
--- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
+++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php
@@ -526,11 +526,12 @@ class CliDumper extends AbstractDumper
 
     protected function endValue(Cursor $cursor)
     {
-        $isStubArray = (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType);
-        if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth && $isStubArray) {
-            $this->line .= ',';
-        } elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex && $isStubArray) {
-            $this->line .= ',';
+        if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) {
+            if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
+                $this->line .= ',';
+            } elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) {
+                $this->line .= ',';
+            }
         }
 
         $this->dumpLine($cursor->depth, true);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ogizanagi, done @nicolas-grekas.

@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Oct 30, 2017
@nicolas-grekas
Copy link
Member

Thank you @Simperfit.

@nicolas-grekas nicolas-grekas merged commit fc3fe7f into symfony:3.3 Nov 1, 2017
nicolas-grekas added a commit that referenced this pull request Nov 1, 2017
…(Simperfit)

This PR was merged into the 3.3 branch.

Discussion
----------

[VarDumper] fix trailling comma when dumping an exception

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yesish
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #24581
| License       | MIT
| Doc PR        |  O

This PR is fixing a behaviour when a exception got every single line with a comma.

Commits
-------

fc3fe7f [VarDumper] fix trailling comma when dumping an exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants