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

pretty-php fails when there is whitespace between a nowdoc operator and its identifier #195

Closed
efedoso opened this issue Jan 19, 2025 · 2 comments · Fixed by #198
Closed
Labels

Comments

@efedoso
Copy link

efedoso commented Jan 19, 2025

pretty-php throws the following error when using nowdoc in sprintf() in the particular case when:

  • A space exists between nowdoc operator and identifier.
  • sprintf() numbered specifiers are used.

pretty-php failed: Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port). pretty-php v0.4.87 (5af7575) PHP 8.3.15 ! Unable to format: [php file] Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port). ! FormatterException: Parsed output doesn't match input in phar:///${HOME}/.vscode-oss/extensions/lkrms.pretty-php-0.4.87-universal/bin/pretty-php.phar/src/Formatter.php:1105

As an example the following code does not throw any error:

<?php

$str = sprintf(
    <<<'EOP'
        Lorem ipsum... %1$s
        EOP,
    'You know'
);

Whereas this one does:

$str = sprintf(
    <<< 'EOP'
        Lorem ipsum... %1$s
        EOP,
    'You know'
);

Thanks in advance.

@efedoso efedoso changed the title pretty-php triggers Xdebug error when using nowdoc in sprintf() with numbered specifiers. pretty-php triggers Xdebug error when using nowdoc in sprintf() with numbered specifiers and space between nowdoc operator and identifier. Jan 19, 2025
@lkrms lkrms added the bug label Jan 20, 2025
@lkrms
Copy link
Owner

lkrms commented Jan 20, 2025

Thanks for reporting this.

The Xdebug error doesn't seem to be related; I imagine that's being reported in pretty-php's output panel when formatting works, too. When Xdebug is enabled via one of your php.ini files, a message is printed every time php runs and can't connect to a debugging client. It's annoying but harmless, although formatting may be slower with Xdebug enabled. (I've made a note to disable it in a future update to the VS Code extension.)

The issue here is the space between the <<< and the nowdoc identifier, which is syntax I hadn't seen and didn't think was legal from my own testing, but it must be if you're seeing a Parsed output doesn't match input exception! I'm guessing this breaks pretty-php's nowdoc detection, causing it to normalise the string as if it were a heredoc (by escaping the $ in this example). Thankfully the equivalence check doesn't pass and the result is rejected.

So, thank you, and congratulations on discovering this exciting new syntax 😂 The next update will normalise code like <<< 'EOF' to <<<'EOF', in line with PHP's documentation, PSR-12/PER, and the heredoc/nowdoc syntax most widely adopted. Until it's released, you could remove this whitespace manually, or disable string normalisation in your configuration.

@lkrms lkrms changed the title pretty-php triggers Xdebug error when using nowdoc in sprintf() with numbered specifiers and space between nowdoc operator and identifier. pretty-php fails when there is whitespace between a nowdoc operator and its identifier Jan 20, 2025
lkrms added a commit that referenced this issue Jan 21, 2025
- Fix issue where nowdocs with whitespace before labels are not detected
  (reported in #195)
- Fix issue where newlines before heredocs at the start of statements
  are suppressed when strict PSR-12 compliance is enabled
lkrms added a commit that referenced this issue Jan 21, 2025
- Fix issue where nowdocs with whitespace before labels are not detected
  (reported in #195)
- Fix issue where newlines before heredocs at the start of statements
  are suppressed when strict PSR-12 compliance is enabled
@lkrms
Copy link
Owner

lkrms commented Jan 21, 2025

Fixed in v0.4.88. The VS Code extension now suppresses Xdebug, too 😊

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

Successfully merging a pull request may close this issue.

2 participants