rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.62k stars 680 forks source link

Incorrect behavior of AnnotationToAttributeRector #6985

Closed niji-mneute closed 2 years ago

niji-mneute commented 2 years ago

Bug Report

Subject Details
Rector version last dev-master
Installed as composer dependency

When a constant is used to define an array key inside an annotation, Rector wraps it with quotes when converting to attributes (ie: AbstractObjectNormalizer::SKIP_NULL_VALUES becomes 'AbstractObjectNormalizer::SKIP_NULL_VALUES').

Minimal PHP Code Causing Issue

See https://getrector.org/demo/1ec85a79-2b52-641e-89af-8128ec1e3b09

<?php

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

/**
 * @ApiResource(
 *     itemOperations={
 *         "get" = {
 *             "normalization_context" =  {
 *                 AbstractObjectNormalizer::SKIP_NULL_VALUES = true
 *             }
 *         }
 *     }
 * )
 */
final class DemoClass
{
}

Responsible rules

Expected Behavior

The constant used as an array key is kept as is.

TomasVotruba commented 2 years ago

Thank you for your report and demo link!

Could you send a failing test case in a pull-request, so we have it covered in Rector? You can click "Create a Test" button at demo page.

niji-mneute commented 2 years ago

Like this https://github.com/rectorphp/rector-src/pull/1764 ?

TomasVotruba commented 2 years ago

Yes, that's correct. It should be as simple as possible though. E.g. with one nesting. It will make test better isolated and easier to fix.

TomasVotruba commented 2 years ago

Related with https://github.com/rectorphp/rector-src/pull/1661/files

samsonasik commented 2 years ago

Resolved at https://github.com/rectorphp/rector-src/pull/1766