DEV Community

Nacho Colomina Torregrosa
Nacho Colomina Torregrosa

Posted on

Using ChatGPT to migrate from PHP annotations to attributes

If you are migrating php projects from php 7.x to 8.x, you possibly have encountered the situation of having to change all the models that use annotations to use attributes.
It can be a long and heavy task, specially when your project have many classes which use annotations such as entities, domain models etc.

In this short post I would like to share with you a chatgpt prompt which has saved me a lot of time in doing so repetitive task.

Let's see the prompt:

The following piece of php code use doctrine annotations to define fields metadata:
    ```


    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"list"})
     */
    private ?int $id = null;

    /**
     * @ORM\Column(type="string", length=100)
     * @Groups({"list"})
     */
    private string $username;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private string $password;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"list"})
     */
    private ?string $name = null;

    /**
     * @ORM\Column(type="datetime")
     * @Gedmo\Timestampable(on="create")
     */
    private ?DateTimeInterface $createdAt = null;

    /**
     * @ORM\Column(type="json", nullable=true)
     * @Groups({"list"})
     */
    private array $roles = [];

    /**
     * @ORM\Column(type="boolean")
     * @Groups({"list"})
     */
    private bool $enabled;



    ```

Your task is to transform it to use attributes instead using the following rules: 
    - Avoid writing "use" statements
    - Avoid class name and curly braces
Enter fullscreen mode Exit fullscreen mode

Let's review the prompt step by step:

The following piece of php code use doctrine annotations to define fields metadata:
Enter fullscreen mode Exit fullscreen mode

I start the prompt by telling chatGPT what the bellow piece of code represents. Then, I wrap the piece of code between three quotes so that chatGPT can distinguish where the code starts and ends.

Your task is to transform it to use attributes instead using the following rules: 
    - Avoid writing "use" statements
    - Avoid class name and curly braces
Enter fullscreen mode Exit fullscreen mode

Finally, we tell chatgpt what it has to do (transform it to use attributes instead) and then we instruct it with some rules: Avoid writing "use" statements and Avoid class name and curly braces. This rules are useful since they remove all elements I do not need to copy and, when chatgpt returns the result, I directly copy the content from the "Copy code" button.

The following image shows the result for the last prompt:

ChatGPT prompt

As shown in the above image, I only have to press copy code button and replace my annotated class properties with the ones returned by chatgpt.

And that's all :), I hope it can serve others to save time in their repetitive tasks.

Top comments (2)

Collapse
 
cryde profile image
Jérémy

Why not using Rector ?

Collapse
 
icolomina profile image
Nacho Colomina Torregrosa

I didn't know it, thanks for the info !