Skip to content

Loading…

[DataCollector] Improves the readability of the collected arrays in the profiler. #10155

Closed
wants to merge 2 commits into from

6 participants

@sukei
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT

It simply improves the readability of the collected arrays in the profiler:

before:

Array(date => Array(year => , month => , day => ), time => Array(hour => ))

after:

[
  date => [
      year => , 
      month => , 
      day => 
  ], 
  time => [
      hour => 
  ]
]
@WouterJ
Symfony member

:+1:

@vicb vicb commented on an outdated diff
...onent/HttpKernel/DataCollector/Util/ValueExporter.php
((10 lines not shown))
{
if (is_object($value)) {
return sprintf('Object(%s)', get_class($value));
}
+ if (is_array($value) && empty($value)) {
@vicb
vicb added a note

may be this should be merged with the if just below ?

@sukei
sukei added a note

I made it this way to improve readability, but you are right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sukei

Nobody want that? :worried:

@sstok

Can you add some tests?

@cordoval cordoval commented on the diff
...onent/HttpKernel/DataCollector/Util/ValueExporter.php
((10 lines not shown))
{
if (is_object($value)) {
return sprintf('Object(%s)', get_class($value));
}
if (is_array($value)) {
+ if (empty($value)) {
+ return '[]';
+ }
+
+ $indent = str_repeat(' ', $depth);

is here 2 spaces or 4 spaces better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@cordoval

:+1: it is a good idea

@fabpot
Symfony member

I don't understand how it could work with just these modifications. In my test on Symfony 2.3, 2.4, and 2.5, it just did not had the expected behavior described here. So, I've made the needed changes in #10352

@fabpot fabpot closed this
@sukei

I have a 2.4.1 and it just works as expected.

profiler

@fabpot fabpot added a commit that referenced this pull request
@fabpot fabpot feature #10352 [DataCollector] Improves the readability of the collec…
…ted arrays in the profiler (fabpot)

This PR was merged into the 2.5-dev branch.

Discussion
----------

[DataCollector] Improves the readability of the collected arrays in the profiler

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

This PR is based on #10155.

Original description:

It simply improves the readability of the collected arrays in the profiler:

__before__:
```
Array(date => Array(year => , month => , day => ), time => Array(hour => ))
```

__after__:
```
[
  date => [
      year => ,
      month => ,
      day =>
  ],
  time => [
      hour =>
  ]
]
```

Commits
-------

dce66c9 removed double-stringification of values in the profiler
1cda2d4 [HttpKernel] tweaked value exporter
3f297ea Improves the readability of the collected arrays in the profiler.
65c9aca
@sukei sukei deleted the sukei:value-exporter branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
2 src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php
@@ -25,7 +25,7 @@ class FormDataExtractorTest_SimpleValueExporter extends ValueExporter
/**
* {@inheritdoc}
*/
- public function exportValue($value)
+ public function exportValue($value, $depth = 0)
{
return is_object($value) ? sprintf('object(%s)', get_class($value)) : var_export($value, true);
}
View
13 src/Symfony/Component/HttpKernel/DataCollector/Util/ValueExporter.php
@@ -20,22 +20,29 @@ class ValueExporter
* Converts a PHP value to a string.
*
* @param mixed $value The PHP value
+ * @param integer $depth The depth of the value to export
*
* @return string The string representation of the given value
*/
- public function exportValue($value)
+ public function exportValue($value, $depth = 0)
{
if (is_object($value)) {
return sprintf('Object(%s)', get_class($value));
}
if (is_array($value)) {
+ if (empty($value)) {
+ return '[]';
+ }
+
+ $indent = str_repeat(' ', $depth);

is here 2 spaces or 4 spaces better?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
$a = array();
foreach ($value as $k => $v) {
- $a[] = sprintf('%s => %s', $k, $this->exportValue($v));
+ $a[] = sprintf('%s %s => %s', $indent, $k, $this->exportValue($v, $depth + 1));
}
- return sprintf("Array(%s)", implode(', ', $a));
+ return sprintf("[\n%s%s\n%s]", $indent, implode(sprintf(", \n%s", $indent), $a), $indent);
}
if (is_resource($value)) {
Something went wrong with that request. Please try again.