rectorphp/rector-doctrine

Nested annotation to attribute support

oojacoboo opened this issue · 4 comments

PHP 8.1 introduced a way to handle nested attributes, something missing from the 8.0 implementation and needed for some Doctrine annotations.

Doctrine has the annotation, @ORM\JoinColumns, used like so:

@ORM\JoinColumns({
    @ORM\JoinColumn(name="thing_id", referencedColumnName="id", nullable=false)
})

Currently, the DoctrineAnnotationClassToAttributeRector skips over this annotation, presumably due to PHP 8.0's lack of nested attribute support. Is there an update on supporting this or a plan in place?

Thanks @samsonasik. I just tried this again with the following configuration. Is there something else I should be trying?

<?php

declare(strict_types = 1);

use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Php80\Rector\Class_\DoctrineAnnotationClassToAttributeRector;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\SymfonySetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    // Here we can define, what sets of rules will be applied
    // tip: use "SetList" class to autocomplete sets
    // $containerConfigurator->import(SetList::CODE_QUALITY);
    $containerConfigurator->import(SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES);

    // Set parameters
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/phpstan.neon');
    $parameters->set(Option::PATHS, [
        __DIR__ . '/../../app/src',
    ]);
    $parameters->set(Option::SKIP, [
        __DIR__ . '/../../app/src/Entity/.Proxy',
    ]);


    // Register single rule
    $services = $containerConfigurator->services();
    $services->set(TypedPropertyRector::class);
    $services->set(NoUnusedImportsFixer::class);
    $services->set(DoctrineAnnotationClassToAttributeRector::class)
        ->configure([DoctrineAnnotationClassToAttributeRector::REMOVE_ANNOTATIONS => true]);
};

Looking at fixture example, you may need to use DoctrineSetList :

    $containerConfigurator->import(\Rector\Doctrine\Set\DoctrineSetList::DOCTRINE_ORM_29);

Thanks @samsonasik that did the trick!