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

How to make the dumper produce guaranteed-valid PHP strings? #568

Open
phil-s opened this issue Sep 6, 2019 · 4 comments
Open

How to make the dumper produce guaranteed-valid PHP strings? #568

phil-s opened this issue Sep 6, 2019 · 4 comments

Comments

@phil-s
Copy link

phil-s commented Sep 6, 2019

I think it's CliDumper::dumpString() which is responsible for the likes of these?

>>> '"'
=> """

>>> 'foo
... bar'
=> """
   foo\n
   bar
   """

Neither of those outputs are valid PHP strings, and I would like them to be.

e.g.

>>> '"'
=> "\""

>>> 'foo
... bar'
=> "foo
bar"

That dumpString() method itself seems pretty hard-coded in this respect, and I couldn't see anything in the manual other than the casters pointer; but based on the upstream description I'm not sure that's actually relevant to simple string values?

Objects and resources nested in a PHP variable are "cast" to arrays in the intermediate Data representation. You can customize the array representation for each object/resource by hooking a Caster into this process. The component already includes many casters for base PHP classes and other common classes.

It looks like a bit of a rabbit hole, so I thought I'd ask for directions...

@bobthecow
Copy link
Owner

The dumper doesn't produce valid anything, and isn't really meant to :)

It has "object literals", which definitely aren't valid PHP. It folds nested properties and array values into Array(n). It annotates dumped objects with #2345 ids so you can tell which object they are. It often includes things that aren't properties at all, and leaves things out that are properties.

It's meant to show you what something is in a way that's easy to understand. You should think of it as analogous to var_dump. If you want valid PHP values, use var_export.

@phil-s
Copy link
Author

phil-s commented Oct 4, 2019

So perhaps there could be a config option to have var_export used in place of the default dumper?

@bobthecow
Copy link
Owner

There is a --raw-ouput flag, which does use var_export, but currently only works with non-interactive input (for example, when executing stdin).

Screen Shot 2019-10-04 at 4 46 30 AM

It would be pretty easy to make it work even when using PsySH interactively.

Unfortunately, it's also pretty easy to make it explode. Returning a moderately complex object makes a mess of everything. For example, if the return value is a PsySH Configuration object, it spits out 200 warnings about circular references, and 60,000+ lines of worthless PHP code. And since PsySH is a REPL, even when you don't want to dump return values, you often end up doing it 😕

@bobthecow
Copy link
Owner

Given the problems mentioned, I'm not super comfortable handing people a footgun like that.

It might be that we could special case strings, specifically, to have valid output rather than pretty output. cc @nicolas-grekas, who is a good person to ask!

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

No branches or pull requests

2 participants