Skip to content
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

Open
wants to merge 1 commit into
base: 6.3
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Contributor

@alexandre-daubois alexandre-daubois commented Dec 1, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR Todo

Following an idea from @nicolas-grekas, the goal here is to ease debugging with dd() and dump() by supporting named arguments passed to them. This will display a label, helping understand the goal/meaning of a dump.

Example in web browser:

image

Example in CLI:

image

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);!

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Send screenshots! We want to make the buzz around this ;)

@@ -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)) {
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 1, 2022

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);
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 1, 2022

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"
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 1, 2022

Choose a reason for hiding this comment

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

a "label"?

Copy link
Member

@GromNaN GromNaN Dec 2, 2022

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.

Suggested change
* 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)
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 1, 2022

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

Copy link
Member

@stof stof Dec 2, 2022

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

Copy link
Member

@GromNaN GromNaN Dec 2, 2022

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.

Suggested change
public static function dump(mixed $var, string $sectionName = null)
/**
* @param string|null $name
*/
public static function dump(mixed $var, /*string $name = null*/)

Copy link
Contributor Author

@alexandre-daubois alexandre-daubois Dec 2, 2022

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. 👍

Copy link
Member

@nicolas-grekas nicolas-grekas Dec 2, 2022

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

Copy link
Member

@GromNaN GromNaN left a comment

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.

6.3
---

* Add support of named arguments to `dd()` and `dump()` to display a "section name"
Copy link
Member

@GromNaN GromNaN Dec 2, 2022

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.

Suggested change
* 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)
Copy link
Member

@GromNaN GromNaN Dec 2, 2022

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.

Suggested change
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
Copy link
Member

@stof stof Dec 2, 2022

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.

Copy link
Contributor Author

@alexandre-daubois alexandre-daubois Dec 2, 2022

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?

Copy link
Member

@nicolas-grekas nicolas-grekas Dec 2, 2022

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 ^?

Copy link
Member

@nicolas-grekas nicolas-grekas Dec 2, 2022

Choose a reason for hiding this comment

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

^ 🐛

Copy link
Contributor Author

@alexandre-daubois alexandre-daubois Dec 2, 2022

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?

Copy link
Member

@nicolas-grekas nicolas-grekas Dec 2, 2022

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.

Copy link
Member

@stof stof Dec 2, 2022

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

Copy link
Contributor Author

@alexandre-daubois alexandre-daubois Dec 2, 2022

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)
Copy link
Member

@stof stof Dec 2, 2022

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

@alexandre-daubois alexandre-daubois changed the title [VarDumper] Add support of named arguments to dd() and dump() to display a "section name" [VarDumper] Add support of named arguments to dd() and dump() to display a label Dec 2, 2022
@lyrixx
Copy link
Member

lyrixx commented Dec 2, 2022

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

dd($value, max_depth: 4);

to automatically expand the 4 depth level

@alexandre-daubois
Copy link
Contributor Author

alexandre-daubois commented Dec 2, 2022

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 dump() and dd() behaviours sounds great!

foreach ($vars as $key => $v) {
VarDumper::dump($v, is_numeric($key) ? null : $key);
}
Copy link
Contributor

@azjezz azjezz Dec 2, 2022

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?

Suggested change
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++;
}

@drupol
Copy link
Contributor

drupol commented Dec 2, 2022

Nice feature !

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

Successfully merging this pull request may close these issues.

None yet

8 participants