rector icon indicating copy to clipboard operation
rector copied to clipboard

Incorrect behavior of AnnotationToAttributeRector

Open astronom opened this issue 3 years ago • 2 comments

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.org/demo/4aed0527-1eb1-420d-8af3-81de2423063a

<?php
use Symfony\Component\Serializer\Annotation as Serializer;

/**
 * @Serializer\DiscriminatorMap(typeProperty = "type", mapping = {
 *     "FOO" : TestFoo::class,
 *     "BAR" : TestBar::class
 * })
 */
interface TInterface
{
}

/**
 * @Serializer\DiscriminatorMap(typeProperty = "type", mapping = {
 *     "FOO" : TestFoo::class,
 *     "BAR" : TestBar::class
 * })
 */
class TestFoo implements TInterface {
}

Responsible rules

  • AnnotationToAttributeRector

Expected Behavior

Interfaces should also be supported by AnnotationToAttributeRector

astronom avatar Sep 21 '22 08:09 astronom

@astronom could you provide a PR patch for it, you can do the following steps:

  1. Add failing fixture at https://github.com/rectorphp/rector-src/tree/8615aa59fb9e0f650e529f8e08e02b8b3a7dd0ac/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture

  2. Add class annotation to be transformed to configure_rule.php for sample https://github.com/rectorphp/rector-src/blob/8615aa59fb9e0f650e529f8e08e02b8b3a7dd0ac/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/configured_rule.php

  3. Run test:

vendor/bin/phpunit rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/AnnotationToAttributeRectorTest.php
  1. Make a patch, it should only add \PhpParser\Node\Stmt\Interface_ at getNodeTypes() return data:

https://github.com/rectorphp/rector-src/blob/8615aa59fb9e0f650e529f8e08e02b8b3a7dd0ac/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php#L99

  1. Update param doc and maybe code related to it to include \PhpParser\Node\Stmt\Interface_ as well

https://github.com/rectorphp/rector-src/blob/8615aa59fb9e0f650e529f8e08e02b8b3a7dd0ac/rules/Php80/Rector/Class_/AnnotationToAttributeRector.php#L113

Thank you.

samsonasik avatar Sep 21 '22 09:09 samsonasik

@samsonasik ok, I will try

astronom avatar Sep 21 '22 09:09 astronom