Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor YamlToAttributeDoctrineMappingRector to use attributes as more reliable #289

Merged
merged 6 commits into from
Feb 15, 2024

Conversation

TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Feb 15, 2024

The old annotations required many extra checks, and were causing troubles on nested annotations.
Instead of doing this 2-step jump, we can use native attributes instead 👍

Upgrade to PHP 8.1, then use this rule to handle your yaml configs 💪

@TomasVotruba TomasVotruba changed the title tv yaml to attributes Refactor YamlToAttributeDoctrineMappingRector to use attributes as more reliable Feb 15, 2024
@TomasVotruba TomasVotruba merged commit b630ecd into main Feb 15, 2024
6 checks passed
@TomasVotruba TomasVotruba deleted the tv-yaml-to-attributes branch February 15, 2024 00:54
@cbeyer
Copy link

cbeyer commented Feb 18, 2024

Hi, should this update be included in a new repo version ?
I'm using rector/rector 1.01 and it seems this update is not included.

I am trying to migrate my old Doctrine YAML files to attributes (Symfony 6.3 project) without success, I have found no example or doc about this

@TomasVotruba
Copy link
Member Author

Hi, try dev-main if this rule is not in 1.0.1

We're still testing it, before putting to the docs. But you can figure out the configuration from configure() method of this rule 👍

@cbeyer
Copy link

cbeyer commented Feb 19, 2024

OK, thanks.
It is not better, I got "[WARNING] Register rules or sets in your "rector.php" config", trying with multiples paths.

Using

$rectorConfig->sets([
        DoctrineSetList::YAML_TO_ANNOTATIONS,
]);

@TomasVotruba
Copy link
Member Author

TomasVotruba commented Feb 19, 2024

You can use the rule itself and provide paths to load yaml mapping (maybe that could be automated too 🤔 ):

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ])
    ->withConfiguredRule(YamlToAttributeDoctrineMappingRector::class, [
        __DIR__ . '/Resources/config/doctrine/',
    ]);

@cbeyer
Copy link

cbeyer commented Mar 18, 2024

Thanks. I tried this, but it does nothing :/

use Rector\Doctrine\CodeQuality\Rector\Class_\YamlToAttributeDoctrineMappingRector;

(...)

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/src/MyBundle1/Entity/',
    ])
    ->withConfiguredRule(YamlToAttributeDoctrineMappingRector::class, [
        __DIR__ . '/config/doctrine/MyBundle1/',
    ])
;

After executing, Rector displays "[OK] Rector is done!" but no file is modified and no progress bar is shown. What is wrong ?

@TomasVotruba
Copy link
Member Author

We'll need a reproducer repository, to see where the issue is.

@cbeyer
Copy link

cbeyer commented Mar 25, 2024

I have a Symfony (now 6.4) project, with code cut into "bundles". Here a example with one entity in one of my home-made-bundle.

Properties are declarated in /config/doctrine/Alert/Alerte.orm.yml :

App\Alert\Entity\Alerte:
    type: entity
    table: alerte_alerte
    repositoryClass: App\Alert\Repository\AlerteRepository

    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: IDENTITY

The equivalent entity is defined with the other PHP code, in src/Alert/Entity/Alerte.php :

namespace App\Alert\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Alerte
{
    /**
     * @var int
     */
    private $id;

    (...) getters/setters
}

And Doctrine configuration, in config/packages/doctrine.yaml :

doctrine:
    orm:
        auto_generate_proxy_classes: true
        enable_lazy_ghost_objects: true
        report_fields_where_declared: true
        validate_xml_mapping: true
        auto_mapping: true

        mappings:

            Alert:
                is_bundle: false
                type: yml
                dir: '%kernel.project_dir%/config/doctrine/Alert'
                prefix: 'App\Alert\Entity'

My rector.php :

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\CodeQuality\Rector\Class_\YamlToAttributeDoctrineMappingRector;

return RectorConfig::configure()

    ->withPaths([
        __DIR__ . '/src/Alert/Entity/',
    ])

    ->withConfiguredRule(YamlToAttributeDoctrineMappingRector::class, [
        __DIR__ . '/config/doctrine/Alert/',
    ])

;

I have tried multiple times, with multiple folders, nothing happens :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants