prettier / prettier/plugin-php

Comments in attributes (e.g. for an array item) are weirdly moved to somewhere completely else

Open
#2,282 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Various comments inside attributes are moved to really weird places instead of staying where they were.
The example below is not the only one where something like this happens. There are a few other scenarios like e..g for a parameter etc.

Prettier 2.88.0 (but also tried the latest)

PHP Plugin 0.19.6 (but also tried the latest)

# Options (if any):
--no-options

Input:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: [
        Request::METHOD_GET, // GET is required for third party app.
        Request::METHOD_POST,
    ])]
    public function save(): Response
    {
        return $this->json(['foo']);
    }
}

Output:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EnrollmentController extends AbstractController // GET is required for third party app.
{
    #[Route(path: '/foo', name: 'app.foo', methods: [Request::METHOD_GET, Request::METHOD_POST])]
    public function save(): Response
    {
        return $this->json(['foo']);
    }
}

Expected behavior:

Output should keep the comment where it was. IT definitely should not be moved to the class:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: [
          Request::METHOD_GET, // GET is required for third party app.
          Request::METHOD_POST
    ])]
    public function save(): Response
    {
        return $this->json(['foo']);
    }
}

This seems to be the case for quite some time as even the old version on the playground shows the issue: Link to 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

Reproduce the formatter behavior using the PHP input and expected output in the issue, or the linked playground, focusing on comments inside attribute arrays and other parameters. Done means formatting preserves the comment beside the array item instead of moving it to the class declaration; no repository file or test is named in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.