Skip to content

Is it possible to use imported namespaces with AttributeKeyToClassConstFetchRector? #7908

@timri

Description

@timri

Bug Report/Feature request

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/a460202b-0782-4844-b547-14f975bbc2b2

<?php

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

class DemoFile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: Types::INTEGER)]
    private int $id;

    #[ORM\Column(type: 'string')]
    private string $name;

}

Responsible rules

  • AttributeKeyToClassConstFetchRector

Expected Behavior

Currently, both columns are changed so that they use the FQCN \Doctrine\DBAL\Types\Types for the type: field, even though
that class is already imported in the use statement.
I would appreciate rector to leave the first column ("id") as is and only change the second column.
Preferably it should detect the already imported class and use that.
So I want the result to look like:

<?php

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

class DemoFile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: Types::INTEGER)]
    private int $id;

    #[ORM\Column(type: Types::STRING)]
    private string $name;

}

alternatively, this would also work (only touch changed lines, i.e. lines that use a literal instead of a class-constant):

<?php

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

class DemoFile
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: Types::INTEGER)]
    private int $id;

    #[ORM\Column(type: \Doctrine\DBAL\Types\Types::STRING)]
    private string $name;

}

I know that I could use $reactorConfig->importNames(true), but that would also import all other classes/namespaces, I only want to import some of them. Or is there a possibility to limit that to specific namespace?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions