Skip to content

[VarDumper] Make dump() a little bit more easier to use #24280

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
wants to merge 3 commits into from

Conversation

freekmurze
Copy link
Contributor

@freekmurze freekmurze commented Sep 21, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes

Imagine you have this code:

$object->method();

If you want to dump the object, this is currently the way to do that:

dump($object);

$object->method();

This PR makes adding (and removing) the dump function easier. When using one argument is used, that argument is returned.

dump($object)->method();

When dumping multiple things, they all get returned. So you can to this:

$object->someMethod(...dump($arg1, $arg2, $arg3));

Imagine you have this code:

```php
$object->method();
```

If you want to dump the object, this is currently the way to do that:

```php
dump($object);

$object->method();
```

This PR makes adding (and removing) the `dump` function easier.

```php
dump($object)->method();
```
@freekmurze freekmurze changed the title Make dump() a little bit more easier to use [VarDumper] Make dump() a little bit more easier to use Sep 21, 2017
@nicolas-grekas
Copy link
Member

I wanted that yesterday, so definitely yes :)
Should target 3.4 when merging.

Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

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

Nice change! @freekmurze thanks (and congrats!) for your first contribution to Symfony!! 🎉

@Taluu
Copy link
Contributor

Taluu commented Sep 21, 2017

This is wrong, because if I do a dump($object, $foo, $bar), I know we will have $object but from an external point of view, this is not so obvious...

@yceruto
Copy link
Member

yceruto commented Sep 21, 2017

@Taluu in that case the last argument is the var that is returned ;)

dump($string, $float, $object)->method();

Copy link
Contributor

@ogizanagi ogizanagi left a comment

Choose a reason for hiding this comment

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

Nice 👍

@greg0ire
Copy link
Contributor

@Taluu in that case the last argument is the var that is returned ;)

Which means it is even less obvious than you think it was... maybe return func_get_args() or null in this case, so that it can't be misused?

@Taluu
Copy link
Contributor

Taluu commented Sep 21, 2017

That's the same thing (I read too fast), but still. If I am using echo, var_dump, error_log and so on, I am not expecting those to "return" the (last ?) argument. And returning all the args is not really intuitive too IMO.

This is making something fluent just for the sake of making it fluent : https://ocramius.github.io/blog/fluent-interfaces-are-evil/

@ogizanagi
Copy link
Contributor

This is just about easing inspecting some vars when developing. It's not just for the sake of making it fluent. Thanks to this, you just have to wrap a variable or an expression instead of writing a new line and possibly executing the same piece of code twice.

This is wrong, because if I do a dump($object, $foo, $bar), I know we will have $object but from an external point of view, this is not so obvious...

maybe return func_get_args() or null in this case, so that it can't be misused?

Returning func_get_args() would defeat the purpose of this, as you won't be able to chain.
What it returns in case of multiple arguments "does not have to be particularly obvious when reading", because you're not likely to read that code anyway: you generally are the one writing this code for debugging purpose so it won't remains. Either you know about the feature, how it behaves and use it, either you don't and keep dumping around the old way.

@Taluu
Copy link
Contributor

Taluu commented Sep 21, 2017

For the func_get_args() I agree, but on the other thing (returning something), I disagree.

Something that would be more convenient IMO would be to yield each value on multiple args, rather than arbitrarily returning the last arg

@ogizanagi
Copy link
Contributor

ogizanagi commented Sep 21, 2017

But what would be the usefulness of this in practice, appart from the debatable less arbitrary nature of the returned value? How would it be convenient? It'll return a generator and won't allow chaining, so again defeats the purpose of this PR.

@franzliedke
Copy link
Contributor

When you dump multiple values, you're probably not using them inline anyways, so the benefit of simply adding and removing dump() around the variable would not be existent anyways...

@greg0ire
Copy link
Contributor

Returning func_get_args() would defeat the purpose of this, as you won't be able to chain.

I wrote "in that case", meaning when you pass multiple vars.

@JulienTant
Copy link

Don't forget that the goal of this PR is it so make dump($object)->method(); works.

We can accept that this PR is a first step, and create new PRs later to improve the behavior of what's returned in the case of multiple arguments ?

@stof
Copy link
Member

stof commented Sep 22, 2017

@greg0ire you cannot make a function become a generator conditionally, as this is determined at compile time (based on the presence of a yield in it).

@greg0ire
Copy link
Contributor

TIL 💡 , but I think you meant to answer to @Taluu

Copy link
Contributor

@greg0ire greg0ire left a comment

Choose a reason for hiding this comment

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

Very useful for people who hate one-time variables like I do

@hannesvdvreken
Copy link

hannesvdvreken commented Sep 22, 2017

We can have it return the exact same list of things of course..

if ($moreVars) {
    return func_get_args();
}

return $var;

and then we could do both

dump($stuff)->someMethod();

and:

$object->someMethod(...dump($arg1, $arg2, $arg3));

imho: it would be a bit awkward to do

dump($foo, $bar, $object)->methodOnObject();

The goal is to dump some variables and make your application still work, kind of, without having to do too many keystrokes.

@freekmurze
Copy link
Contributor Author

I really like what @hannesvdvreken proposed. I'll update this PR.

@greg0ire
Copy link
Contributor

greg0ire commented Sep 22, 2017

That's exactly what I proposed BTW :P Although I didn't think about the unpacking at the time, good thinking @hannesvdvreken !

@nicolas-grekas
Copy link
Member

Thank you @freekmurze.

nicolas-grekas added a commit that referenced this pull request Sep 23, 2017
…use (freekmurze)

This PR was submitted for the master branch but it was squashed and merged into the 3.4 branch instead (closes #24280).

Discussion
----------

[VarDumper] Make `dump()` a little bit more easier to use

| Q             | A
| ------------- | ---
| Branch?       |  3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes

Imagine you have this code:

```php
$object->method();
```

If you want to dump the object, this is currently the way to do that:

```php
dump($object);

$object->method();
```

This PR makes adding (and removing) the `dump` function easier. When using one argument is used, that argument is returned.

```php
dump($object)->method();
```

When dumping multiple things, they all get returned. So you can to this:

```php
$object->someMethod(...dump($arg1, $arg2, $arg3));
```

Commits
-------

42f0984 [VarDumper] Make `dump()` a little bit more easier to use
This was referenced Oct 18, 2017
@lyrixx
Copy link
Member

lyrixx commented Dec 20, 2017

Hello.

I think this is a BC break. Now we can not die(dump()) Anymore.
Exemple:

>[/tmp/toto] cat index.php
<?php

require __DIR__.'/vendor/autoload.php';

die(dump(new datetime()));
>[/tmp/toto] php -n index.php 
DateTime @1513766992 {#3
  date: 2017-12-20 11:49:52.615055 Europe/Berlin (+01:00)
}

Recoverable fatal error: Object of class DateTime could not be converted to string in /tmp/toto/index.php on line 5

@hannesvdvreken
Copy link

Oh true. Before it would return null, and thus you would exit (or die) with null die(null).

This will require a small change to dump($stuff); die; instead.

At least you won't run die(dump()); in production, right ;-)

@lyrixx
Copy link
Member

lyrixx commented Dec 20, 2017

Yeah I know why it is not working anymore ;)
I don't know if this BC break is covered by our BC promise

@hannesvdvreken
Copy link

where can we read more about that?

@nicolas-grekas
Copy link
Member

dump() is not covered by the BC policy, it's not part of any "API", it's just a temporary helper.
If you need BC, VarDumper::dump() should be used instead.

@michielvandenboogaart
Copy link

It's a shame. This PR has broken the dump function since it both outputs and returns now. This whole functionality should have been on a different function or something.

R.I.P. dump()

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.