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
[VarDumper] Add support of named arguments to dd()
and dump()
to display a label
#48432
base: 6.3
Are you sure you want to change the base?
[VarDumper] Add support of named arguments to dd()
and dump()
to display a label
#48432
Conversation
7afb2b1
to
f148793
Compare
@@ -266,6 +271,11 @@ public function dump(DumperInterface $dumper) | |||
if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) { | |||
$cursor->attr['if_links'] = true; | |||
$cursor->hashType = -1; | |||
|
|||
if (null !== ($sectionName = $this->context['section_name'] ?? 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.
no need for the extra brackets
@@ -266,6 +271,11 @@ public function dump(DumperInterface $dumper) | |||
if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) { | |||
$cursor->attr['if_links'] = true; | |||
$cursor->hashType = -1; | |||
|
|||
if (null !== ($sectionName = $this->context['section_name'] ?? null)) { | |||
$dumper->dumpScalar($cursor, 'default', $sectionName); |
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.
let's make a single call to dumpScalar for label + ^
6.3 | ||
--- | ||
|
||
* Add support of named arguments to `dd()` and `dump()` to display a "section name" |
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.
a "label"?
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.
The function can be named according to what it actually does.
* Add support of named arguments to `dd()` and `dump()` to display a "section name" | |
* Add support of named arguments to `dd()` and `dump()` to display the argument name. |
@@ -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.
public static function dump(mixed $var, string $sectionName = null) | |
/** | |
* @param string|null $name | |
*/ | |
public static function dump(mixed $var, /*string $name = 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 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.
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, see https://3v4l.org/2cUek
6.3 | ||
--- | ||
|
||
* Add support of named arguments to `dd()` and `dump()` to display a "section name" |
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.
The function can be named according to what it actually does.
* Add support of named arguments to `dd()` and `dump()` to display a "section name" | |
* Add support of named arguments to `dd()` and `dump()` to display the argument name. |
@@ -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.
public static function dump(mixed $var, string $sectionName = null) | |
/** | |
* @param string|null $name | |
*/ | |
public static function dump(mixed $var, /*string $name = null*/) |
*/ | ||
function dump(mixed $var, mixed ...$moreVars): mixed | ||
function dump(mixed ...$vars): mixed |
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.
the previous signature made it impossible to call the function without any argument. This is not the case anymore.
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.
If we keep the same signature, we're not able to name the first argument. The following code doesn't work:
dump(first: 'Hello');
PHP will tell that $var
parameter is missing. But we may throw a \LogicException
if no argument is passed at all. Would that suit?
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.
no exception please, not for a debugging tool
dump()
could just output the ^
?
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.
^ 🐛
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 seems (unfortunately) tricky to me to display only the caret (or anything else "special"), because VarDumper:dump()
must absolutely take an argument. Obviously we can't pass null
to it.
Could dumping null
when no argument is passed to dump()
be acceptable?
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.
We can pass a stub object, and with a custom stubcaster, display it as we want.
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.
@alexandre-daubois I would not dump null
, as that's a valid dump value as well
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.
Yep, I definitely agree @stof! That was not a good idea.
Just learnt about StubCaster
, it works great. Thanks for the hint @nicolas-grekas!
I updated the first screenshot of the description to reflect the changes
@@ -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()
and dump()
to display a "section name"dd()
and dump()
to display a label
…display the argument name
f148793
to
984aceb
Compare
Oh, 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 ($vars as $key => $v) { | |
VarDumper::dump($v, is_numeric($key) ? null : $key); | |
} | |
$i = 0; | |
foreach ($vars as $key => $v) { | |
VarDumper::dump($v, is_numeric($key) ? "$i" : "$key@$i"); | |
$i++; | |
} |
Nice feature ! |
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);
!