Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[VarDumper] Add support of named arguments todd()
anddump()
to display a label#48432
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
[VarDumper] Add support of named arguments todd()
anddump()
to display a label#48432
Uh oh!
There was an error while loading.Please reload this page.
Conversation
7afb2b1
tof148793
Compare
nicolas-grekas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Send screenshots! We want to make the buzz around this ;)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
@@ -37,13 +37,13 @@ class VarDumper | |||
*/ | |||
private static $handler; | |||
public static function dump(mixed $var) | |||
public static function dump(mixed $var, string $sectionName = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
$label
adding a new argument should be done in a BC way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is a static method, so I don't think we have a BC issue here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would call it$name
(for argument name) or$key
(for array key)
Like this.
publicstaticfunction dump(mixed$var,string$sectionName =null) | |
/** | |
* @param string|null$name | |
*/ | |
publicstaticfunction dump(mixed$var,/*string $name = null*/) |
alexandre-dauboisDec 2, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think I would go for$label
which is clearer and more suitable to the context of "displaying an information".
I'll do the change for the BC promise, as I didn't find any exception mentioning static methods in the prohibition to add an extra parameter to a public method. 👍
nicolas-grekasDec 2, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
it's a BC break@stof, seehttps://3v4l.org/2cUek
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Very nice little feature.
I find the term "section" confusing. A section normally groups several things together. Here, there is only one variable for each name.
Uh oh!
There was an error while loading.Please reload this page.
@@ -37,13 +37,13 @@ class VarDumper | |||
*/ | |||
private static $handler; | |||
public static function dump(mixed $var) | |||
public static function dump(mixed $var, string $sectionName = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would call it$name
(for argument name) or$key
(for array key)
Like this.
publicstaticfunction dump(mixed$var,string$sectionName =null) | |
/** | |
* @param string|null$name | |
*/ | |
publicstaticfunction dump(mixed$var,/*string $name = null*/) |
Uh oh!
There was an error while loading.Please reload this page.
@@ -37,13 +37,13 @@ class VarDumper | |||
*/ | |||
private static $handler; | |||
public static function dump(mixed $var) | |||
public static function dump(mixed $var, string $sectionName = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is a static method, so I don't think we have a BC issue here
dd()
anddump()
to display a "section name"dd()
anddump()
to display a labelf148793
to984aceb
CompareOh, I like this so much. Can we leverage this, to be able to configuration the dumper? (may be in another PR). I would love to do something like
to automatically expand the 4 depth level |
Big yes to me,@lyrixx! When talking about this idea, Nicolas seemed to have a few other ones that could definitely fit with this evolution in future PRs. A set of "reserved" named parameters to tweak |
foreach ($vars as $key => $v) { | ||
VarDumper::dump($v, is_numeric($key) ? null : $key); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
wouldn't be better to dump argument index as well here?
foreach ($varsas$key =>$v) { | |
VarDumper::dump($v,is_numeric($key) ?null :$key); | |
} | |
$i =0; | |
foreach ($varsas$key =>$v) { | |
VarDumper::dump($v,is_numeric($key) ?"$i" :"$key@$i"); | |
$i++; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
When there is only one argument, I wouldn't dump the index as label. But when there are more, it might be nice to dump the index+1 even when it's numeric (+1 to start from 1, and the index should be the key, no need for $i)
(same for dump() of course)
alexandre-dauboisDec 6, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I was 👍 on this proposition, but:
$argc =count($vars);foreach ($varsas$key =>$v) {$displayLabel =1 <$argc || !is_numeric($key);$label =is_numeric($key) ?$key+1 :$key; VarDumper::dump($v,$displayLabel ?$label :null);}
This seems a bit complicated logic for the actual gain, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Like this?
if (isset($vars[0]) &&1 ===count($vars)) { VarDumper::dump($vars[0]);}else {// loop}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Nice feature ! |
nicolas-grekas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for working on this. Here are some comments.
I like the idea of using special names to configure the dump.
I'm keepinga branch from 2016 with all possible options.
Here is what I collected back then:
dump_destination
format
(cli, html, txt, default)max_items
max_depth
max_items_per_depth
max_string_length
max_string_width
collapse_depth
collapse_length
use_ref_handles
casters
replace_casters
filter
light_array
string_length
charset
trace
src_context
trace_args
We could even have an option builder:
$options = VarDumper::options()->trace()->maxDepth(4)->toArray();dump($var, ...$options);
As a convention, it might be worth prefixing all options by an underscore?
Anyway, I'd just suggest considering this in a follow up PR to keep this one focused.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
foreach ($vars as $key => $v) { | ||
VarDumper::dump($v, is_numeric($key) ? null : $key); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
When there is only one argument, I wouldn't dump the index as label. But when there are more, it might be nice to dump the index+1 even when it's numeric (+1 to start from 1, and the index should be the key, no need for $i)
(same for dump() of course)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Let's discuss this other feature in#48474 |
7dbba29
tob1e4214
CompareThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I only have minor CS suggestions. Thanks!
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
00fc4ba
to3da860c
Comparealexandre-daubois commentedDec 15, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Everything has been updated according to your feedback. Thanks! |
Names starting with |
nicolas-grekas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Names starting with _ should be forbidden and reserved to the futur options.
let's do this in the PR that will follow up the issue you opened (same for the "no-args" behavior that needs to be adjusted if we follow your idea of a builder)
3da860c
tob08b677
Compare…display the argument name
b08b677
to2519c5c
CompareThank you@alexandre-daubois. |
} | ||
if (1 < func_num_args()) { | ||
return func_get_args(); | ||
if (isset($vars[0]) && 1 === count($vars)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
count($vars)
is called twice, maybe use an intermediary var to avoid doing the job twice?
Uh oh!
There was an error while loading.Please reload this page.
Following an idea from@nicolas-grekas, the goal here is to ease debugging with
dd()
anddump()
by supporting named arguments passed to them. This will display a label, helping understand the goal/meaning of a dump.Example in web browser:
Example in CLI:
The above example is clickable and points to
file:///home/alexandredaubois/PhpstormProjects/dummy_project/src/Command/TestCommand.php#L15
.No more
dd("First one", $var1, "Second var", $var2);
!