Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[VarDumper] Add support for options indump()/dd()#48667

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
alexandre-daubois wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromalexandre-daubois:feat/var-dumper-options

Conversation

alexandre-daubois
Copy link
Member

@alexandre-dauboisalexandre-daubois commentedDec 15, 2022
edited
Loading

QA
Branch?6.4
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix#48474
LicenseMIT

Following up#48432, this PR brings an options builder to configure VarDumper's behavior thanks to a special_options named argument. Here is a snippet of it in action and a screenshot of the result. Feedbacks on UI are really welcomed, especially about the debug backtrace!

<?phpuseSymfony\Component\VarDumper\Dumper\CliDumper;require'vendor/autoload.php';dump(    var: ['Hi','Greetings' =>'Good morning, Symfony!'],    _trace:true,    _flags: CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_STRING_LENGTH,);

image

Toflar, lee-to, lotyp, and lyrixx reacted with thumbs up emojiToflar, butschster, lee-to, and lyrixx reacted with heart emoji
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

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

Thanks for tackling this, that's a complex feature because the matrix of possibilities is huge.

Here are some random thoughts:

  • I like the style proposed in the issue:dump()->trace()->maxDepth(4)->values($var);. I'm just wondering ifvalues() shouldn't be nameddump() orvar() or ?
  • I also like thedump($var, _trace: true) style, which could work well with autocompletion if we describe the options as virtual arguments on the function?array{_trace: bool, ...}
  • instead of trace_limit, what abouttrace: bool|int?
  • have a look atFrameStub/TraceStub for dumping traces
  • we might want to provide a way to define the default options on the VarDumper class?

alexandre-daubois and GromNaN reacted with thumbs up emoji
@lyrixx
Copy link
Member

  • I like the style proposed in the issue:dump()->trace()->maxDepth(4)->values($var);. I'm just wondering ifvalues() shouldn't be nameddump() orvar() or ?

As states by @derrabus, this cannot work


I don't really like the current object API:

$options = (newVarDumperOptions())    ->trace()    ->stringLength();

it's too long to type. We should not forgot we are a indebug context. So we want to be able to get a result ASAP. that's why we have the functiondd(). As short as possible.

That's why I prefer the second bullet point of@nicolas-grekascomment:

dd($this, _trace:true, _maxDepth:4);

@nicolas-grekas
Copy link
Member

#48474 (comment), this cannot work

yes it can, see#48474 (comment) ;)

@lyrixx
Copy link
Member

This is ugly :) I really don't like it. And it's too slow to type.

@lyrixx
Copy link
Member

@ro0NL I mixed my thought :/ I was talking about:

$options = (newVarDumperOptions())    ->trace()    ->stringLength();dump($this, _options:$options);

@alexandre-daubois
Copy link
MemberAuthor

I agree the current object API is a bit much to type. I am 👍 to find a better solution.

Should I try to implement both@GromNaN solution (dump returningmixed|VarDumperOptions) and@nicolas-grekas' one, together? I wonder if it would be a bit disturbing to have both ways available, and I don't have, yet, a strong opinion for one solution over the other.

Also, I really like the solution proposed by@ro0NL. If I may find a drawback, I'm afraid this syntax could be difficult to be "discovered" by yourself without reading the documentation.

@GromNaN
Copy link
Member

The syntax that don't work is the one that rely on __destruct.
The last suggestion mentioned by@nicolas-grekas isdump()->trace()->maxDepth(4)->dump($var) which returns the$var as expected.

@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedDec 17, 2022
edited
Loading

I updated the feature. The new version allows you to do both ways:

// Passing options as named parametersdump('Values', _trace:5, _flags: AbstractDumper::DUMP_STRING_LENGTH);// Use the options builder syntaxdump()    ->trace(5)    ->stringLength()    ->dd('Values')// or ->dump('Values');

Still investigating on how to useTraceStub. Otherwise, this works well! And it isso much cleaner and quicker to type.

Also, I made the choice to only have the options builder syntax ondump, notdd. I did this, because I think it is, in my opinion, a good thing to keepnever return type ondd (becausenever|VarDumpOptions is obviously a forbidden return type). Of course, passing options as named parameters todd also works!

pierresh reacted with thumbs up emoji

@alexandre-dauboisalexandre-dauboisforce-pushed thefeat/var-dumper-options branch 2 times, most recently from1b933a2 toc8f0cd9CompareDecember 19, 2022 13:00
@alexandre-daubois
Copy link
MemberAuthor

I've been able to useTraceStub to display the backtrace. Here is the result:

image

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

About the default options, I'd suggest configuring them via a VAR_DUMPER_OPTIONS env var. I'd go with a url-encoded string so that it's easy to turn into an options array

alexandre-daubois reacted with thumbs up emoji
}

if (self::requiresRegister($options)) {
self::register($options);

Choose a reason for hiding this comment

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

this will change the options of the default handler when any options changes?
this would be unexpected to me.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

requiresRegister helps on taking care of registering again the handler ifdump/dd is not called with the exact same set of options as the previous call (this should work great with options defined in an env var!). So if you call the default dumper (I assume what you called "default dumper" is the one called without any special option? I may be wrong 😄), it should be registered again with its default options.

I struggled a bit on this part, only a few options need a full and new registration. I may be missing something?

@dunglas
Copy link
Member

When dumping large objects, it could be nice to be able to have the possibility to dump only one or several properties instead of the full object. Something like:_path: 'foo.bar' and_path: ['foo.bar', 'baz.bat'] maybe?

@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedDec 23, 2022
edited
Loading

@nicolas-grekas thank you for the review, I'll check this out! The env var is also a great idea too, in my opinion.
Edit: the env var mechanism is in!

@dunglas I really like your proposition! Giving the size of this PR, I think it would deserve a separate one. Definitely keeping your idea this in mind 👍

dunglas, lyrixx, WebMamba, OskarStark, and alamirault reacted with thumbs up emoji

@nicolas-grekasnicolas-grekas added this to the7.1 milestoneNov 15, 2023
@butschster
Copy link

butschster commentedMay 2, 2024
edited
Loading

Hey@alexandre-daubois! We're currently working onBuggregator, and it can be used as a server for displaying dumps for the var-dumper component. I really like your PR with options. However, it would be great if you also provided the ability to add custom context as an array to adump function.

$options = (newVarDumperOptions())    ->withContext(['language' =>'php']);dump($this, _options:$options);

This feature can be used in various scenarios, such as providing information about syntaxhighlighting for string dumps or offering details about the project to which a dump should be linked.

Would you kindly consider adding support for custom context in thedump function

Thanks for PR!

butschster, markinigor, meekstellar, lee-to, and roxblnfk reacted with heart emojialexandre-daubois reacted with eyes emoji

@alexandre-daubois
Copy link
MemberAuthor

Hey@butschster! Thank you for your interest in this PR! I kinda forgot about it, but I still think it is worth it to have this in the framework.

About the context feature you're talking about: while I understand it would help in your case, I find it weird to add some context here that won't be used apart from super precise use cases. Two solutions here:

  • If you have some strong reasons and use case, a complementary PR could be proposed ;
  • We may keepVarDumpOptionsnot final so you can extend it for all your needs. I think that's the quick win 😊

Anyway, I'll try finalize this PR. Thanks for digging it up!

butschster reacted with hooray emoji

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

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

Coming back after a few months here, I'm wondering if this is worth the extra complexity...
We have an unresolved concern, which is that dump-handlers don't have to react to options, and we don't have an internal API to communicate those options to them. If we do want options, we need that API.

I'm wondering if we need this options class. We could instead document options using@param on the dump() function, so that using them would look like:dump($var, _max_depth: 3). That might be good enough in terms of DX.

OskarStark reacted with thumbs up emojialexandre-daubois reacted with eyes emoji
@alexandre-dauboisalexandre-dauboisforce-pushed thefeat/var-dumper-options branch 5 times, most recently froma94b07a todf524cbCompareAugust 19, 2024 12:06
@alexandre-daubois
Copy link
MemberAuthor

I pushed a rebased, cleaner version. I removed the Options class and replaced it with an array, which seems enough. Tests are fixed.

We have an unresolved concern, which is that dump-handlers don't have to react to options, and we don't have an internal API to communicate those options to them. If we do want options, we need that API.

I wasn't exactly sure what you meant here. I updatedVarDumper::dump() in order to pass options to the registered dump handler. This way, if one defines its own handler, options can either be handled or ignored. Is that what you had in mind?

@alexandre-dauboisalexandre-dauboisforce-pushed thefeat/var-dumper-options branch 2 times, most recently fromb3bd92a toe1a90f4CompareAugust 19, 2024 12:11

if ($cursor->attr || '' !== $label) {
$dumper->dumpScalar($cursor, 'label', $label);
}
$cursor->hashType = 0;
$this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);

if (false !== ($options['_trace'] ?? false) && $cursor->attr = $this->context[BacktraceContextProvider::class] ?? []) {

Choose a reason for hiding this comment

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

comparisons to boolean... you missed testing this is=== true :P

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Actually,_trace can be either a bool or a integer.false disables the trace. If it's an integer, we limit the trace size. Also, the limit is disabled if_trace is <= 0. That's why we can't use=== true here 😄 But maybe limiting the stack trace isn't that useful and we may only accept a boolean for_trace?

Choose a reason for hiding this comment

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

ah, makes sense then, let's keep as is :)

alexandre-daubois reacted with thumbs up emoji
@OskarStark
Copy link
Contributor

PR title and header needs to be updated to reflect the content of this PR, thanks

alexandre-daubois reacted with thumbs up emoji

@alexandre-dauboisalexandre-daubois changed the title[VarDumper] Add an options builder to configure VarDumper's behavior at runtime[VarDumper] Add support for options indump()/dd()Aug 21, 2024
@alexandre-daubois
Copy link
MemberAuthor

Should be better now@OskarStark

@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@GromNaNGromNaNGromNaN left review comments

@ro0NLro0NLro0NL left review comments

@alamiraultalamiraultalamirault left review comments

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

Add options todump function
12 participants
@alexandre-daubois@lyrixx@nicolas-grekas@GromNaN@dunglas@butschster@OskarStark@ro0NL@alamirault@fabpot@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp