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

ReflectionParameter::__toString mangles special characters in strings #9622

Closed
nicolas-grekas opened this issue Sep 27, 2022 · 5 comments
Closed

Comments

@nicolas-grekas
Copy link
Contributor

Description

Issue demoed at https://3v4l.org/bMiN9#v8.1.10

The following code:

function f($b = ["\n", new \stdClass])
{
}

var_dump((string) (new ReflectionFunction("f"))->getParameters()[0]);

function g($b = ["\n"])
{
}

var_dump((string) (new ReflectionFunction('g'))->getParameters()[0]);

Resulted in this output:

string(55) "Parameter #0 [ <optional> $b = ['
', new \stdClass()] ]"
string(39) "Parameter #0 [ <optional> $b = ['\n'] ]"

But I expected this output instead:

string(55) "Parameter #0 [ <optional> $b = ['
', new \stdClass()] ]"
string(39) "Parameter #0 [ <optional> $b = ['
'] ]"

PHP Version

PHP 8.1+

Operating System

No response

@nicolas-grekas nicolas-grekas changed the title Special charactersBroken string representation of ReflectionParameter::__toString mangles special characters in strings Sep 27, 2022
@malarzm
Copy link

malarzm commented Sep 27, 2022

Not sure if feasible or not so just throwing ideas: preserving " in this case would also be fine (but probably could have other side effects too)

@nicolas-grekas
Copy link
Contributor Author

Using double quotes would work, provided we also escape dollar signs.

@nicolas-grekas
Copy link
Contributor Author

I'm closing here because I had a look at smart_str_append_escaped and the situation is quite manageable. It's easy to write code around the way PHP escapes default values. Also because changing this way might break existing code.

@stof
Copy link
Contributor

stof commented Sep 28, 2022

@nicolas-grekas do you intend to submit the fix to doctrine/common then ?

@nicolas-grekas
Copy link
Contributor Author

See doctrine/common#988 (comment) yes

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

4 participants