plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Attributes removed since 0.19.2 if method argument has an attribute with attributes in them and arrays and some new lines

Open AndreasA opened this issue 2 years ago • 0 comments

Since 0.19.2 it seems that prettier removes attributes, if an argument also has attributes that are more complex. See examples below. Especially relevant with PHP 8.2 setting and even newer prettier versions.

Not providing playground example as playground pretter plugin version is too old.

Might be related to: https://github.com/prettier/plugin-php/issues/2101

Prettier 2.8.8 However, also tried the latest prettier version with 0.21.0

PHP Plugin 0.19.6 However, also tried 0.19.2 and the latest 0.21.0 and to confirm it works with 0.19.1

# 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\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: Request::METHOD_POST)]
    public function save(
        #[
            MapRequestPayload(
                serializationContext: [
                    AbstractNormalizer::CALLBACKS => [
                        'abc' => 'trim',
                    ],
                ],
            ),
        ]
        BarDto $data,
    ): 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\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class EnrollmentController extends AbstractController
{
    public function save(
        #[
            MapRequestPayload(
                serializationContext: [
                    AbstractNormalizer::CALLBACKS => [
                        'abc' => 'trim',
                    ],
                ],
            ),
        ]
        BarDto $data,
    ): Response {
        return $this->json(['foo']);
    }
}

Expected behavior: That the Route attribute is not removed but formatted accordingly.

What is interesting is that the following works fine, though code wise it is the same:

<?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\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: Request::METHOD_POST)]
    public function save(
        #[MapRequestPayload(serializationContext: [AbstractNormalizer::CALLBACKS => ['abc' => 'trim',],],), ]
        BarDto $data,
    ): Response {
        return $this->json(['foo']);
    }
}

AndreasA avatar Nov 24 '23 06:11 AndreasA