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

Heredoc and Nowdoc converted to inline string when parsed #151

Closed
GromNaN opened this issue Jan 17, 2024 · 4 comments
Closed

Heredoc and Nowdoc converted to inline string when parsed #151

GromNaN opened this issue Jan 17, 2024 · 4 comments

Comments

@GromNaN
Copy link
Contributor

GromNaN commented Jan 17, 2024

Version: 4.1.2

PHP                              8.3.1
nette/php-generator              v4.1.2
nette/utils                      v4.0.4
nikic/php-parser                 v4.18.0

Bug Description

When a PHP file contains a heredoc or a nowdic, it is converted to a double-quoted string by the parsed.
This transforms the contents of a the files that are modified using this package, and this can generate invalid PHP syntax if the string contains a double quote.

Steps To Reproduce

Create a file that contains a heredoc or a nowdoc (called file.php):

<?php

function test()
{
    return <<<'EOL'
        Hello "You"!
        EOL;
}

Create a script that reads this file, parse it and dump it:

<?php

use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\PsrPrinter;

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

$file = PhpFile::fromCode(file_get_contents(__DIR__ . '/file.php'));

echo (new PsrPrinter())->printFile($file);

Run the script.

The result is an invalid PHP file with the string inlined in double quotes.

<?php

function test()
{
    return "Hello "You"!";
}

Expected Behavior

The output should be identical to the input file.php.

<?php

function test()
{
    return <<<'EOL'
        Hello "You"!
        EOL;
}

Possible Solution

No idea, sorry.

@GromNaN GromNaN changed the title Heredoc and Nowdoc converted to string when parsed Heredoc and Nowdoc converted to inline string when parsed Jan 17, 2024
@dg dg closed this as completed in 19ac08c Jan 17, 2024
@GromNaN
Copy link
Contributor Author

GromNaN commented Jan 17, 2024

Thanks for the fix @dg. Isn't it possible to keep the heredoc/nowdoc in the function body?

@dg
Copy link
Member

dg commented Jan 17, 2024

In principle, since PHP 7.3 it is possible, I will try to implement it.

@dg
Copy link
Member

dg commented Jan 17, 2024

@GromNaN it should be working in master by now, can you test it?

@GromNaN
Copy link
Contributor Author

GromNaN commented Jan 18, 2024

Perfect, it works. The upgrade to nikic/php-parser: v5.0.0 was not required but nice to have.
Thank you!

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

No branches or pull requests

2 participants