Skip to content

Feature request: display string lengths in var dumper #15856

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

Closed
Alorel opened this issue Sep 21, 2015 · 12 comments
Closed

Feature request: display string lengths in var dumper #15856

Alorel opened this issue Sep 21, 2015 · 12 comments

Comments

@Alorel
Copy link

Alorel commented Sep 21, 2015

Hey folks. I've just started using the var dumper and it seems to lack a rather useful feature: displaying string lengths.

How it currently works

If one of the variables passed to the dumper is a string, it will be dumped as "string-contents"

$var = ['foo'];
VarDumper::dump($var);
>>>
array:1 [
   0 => & "foo"
]

Proposed change

Display the string length in a manner similar to how array lengths are displayed:

$var = ['foo'];
VarDumper::dump($var);
>>>
array:1 [
   0 => & string:3 "foo"
]

A working example can be found in Raveren's Kint project.

@Alorel Alorel changed the title Display string lengths Var dumper: Display string lengths Sep 21, 2015
@Alorel Alorel changed the title Var dumper: Display string lengths Feature request: display string lengths in var dumper Sep 21, 2015
@linaori
Copy link
Contributor

linaori commented Sep 21, 2015

The double quotes already imply that it's a string, what about a more intuitive syntax?

array:1 [
   0 => & 3:"foo"
   0 => & "foo" (3)
   0 => & (3) "foo"
]

@Alorel
Copy link
Author

Alorel commented Sep 21, 2015

@iltar Indeed, my proposed syntax was a bit too much - the core proposal is to display the string length in the output, not the fact that it's a string.

@nicolas-grekas
Copy link
Member

fyi, length is displayed on hover in html mode, so you'd like it in cli output also?

@nicolas-grekas
Copy link
Member

(btw, my preference would go for "foo" (3))

@Alorel
Copy link
Author

Alorel commented Sep 21, 2015

@nicolas-grekas I never noticed the title attribute in html - thanks, but yes, something similar would be highly appreciated in cli mode (my preference would be the same). As for html output, perhaps there can be an option to toggle display as "foo" (3) instead of hardcoding it as a title attribute (as it is not immediately obvious at this stage)?

@linaori
Copy link
Contributor

linaori commented Sep 21, 2015

The downside of post-fixing it, means you won't be able to easily see the length without checking the end. With long strings this might be a bit annoying.

@nicolas-grekas
Copy link
Member

Putting (what I think is) a secondary information in front of the main information would just clutter readability. Personally, I never read the length. It's usually useless information to me. That's why the output is the way it is now. IMHO, the readability loss is higher than the gain. I'd really like to discuss the need to output the length before we implement it if you think it's worth.

@linaori
Copy link
Contributor

linaori commented Sep 21, 2015

One case where you might need it, is comparing strings that can contain zero-width spaces for example.

@nicolas-grekas
Copy link
Member

I'm sure there is a use case, but should we clutter all and every dump just in case someone is in this very rare situation?

@Alorel
Copy link
Author

Alorel commented Sep 21, 2015

Another (simple) use case is when you're dealing wish hashes - if you see an invalid length you know you missed something in your code.

A third use case is dealing with encrypted data in limited-length datastores, allowing you to estimate how long the current code implementation can be used for.

I see your point about clutter - indeed, there's no need to force this upon all users, so it could be made as an optional setting which is disabled by default.

@nicolas-grekas
Copy link
Member

See #17305

nicolas-grekas added a commit that referenced this issue Jan 14, 2016
…sentation (nicolas-grekas)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[VarDumper] Add flags to allow fine tuning dumps representation

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15856
| License       | MIT
| Doc PR        | -

The `DUMP_STRING_LENGTH` behavior has been requested in #15856,
the `DUMP_LIGHT_ARRAY` behavior is what should have been done for the VarDumperTestTrait. Anticipated usage is opt-in, by adding `<env name="DUMP_LIGHT_ARRAY" value="1" />` in `phpunit.xml.dist`.
Any more flags proposal anyone? Better names maybe?

Commits
-------

a35ceb0 [VarDumper] Add flags to allow fine tuning dumps representation
@javiereguiluz
Copy link
Member

I'm closing this as fixed because the feature was implemented by Nicolas in #17305 via the DUMP_STRING_LENGTH flag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants