prettier / prettier/plugin-php

Improve formatting of heredoc/nowdoc

Open
#1,590 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
1.9k
Forks
139
PR merge metrics
No merged PRs in 30d

Description

As suggested in https://github.com/prettier/plugin-php/issues/1274#issuecomment-570709723:

I'd say, it would be The Prettier Way™ (and also the prettier way ) to figure out one way to print heredocs and to enforce that.

One process which sounds reasonable to me: If the starting tag goes on its own line, align the content with that. Otherwise indent it one tabWidth further.

Examples of the described rule

Example 1

The heredoc-opening tag goes on its own line.

Input:

some_function(<<<'EOT'
Hello World!
EOT
);

Current output:

some_function(
    <<<'EOT'
Hello World!
EOT
);

Envisioned output:

some_function(
    <<<'EOT'
    Hello World!
    EOT
);
Example 2

The heredoc-opening tag goes on the same line as the preceding code.

Input:

$string = <<<'EOT'
Hello World!
EOT;

Current output:

$string = <<<'EOT'
Hello World!
EOT;

Envisioned output:

$string = <<<'EOT'
    Hello World!
    EOT;

The first example is actually already printed correctly, but the second one isn't (playground)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked playground examples for heredoc and nowdoc formatting, comparing the current and envisioned output. Done means the formatter consistently aligns content and closing tags according to whether the opening tag is on its own line or follows code, with coverage for the shown cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.