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:


Enter fullscreen mode Exit fullscreen mode
/**
 * @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;

```
Enter fullscreen mode Exit fullscreen mode

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


Let's review the prompt step by step:

Enter fullscreen mode Exit fullscreen mode

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

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.

Enter fullscreen mode Exit fullscreen mode

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


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dd1o03g2wj8j2urhkr8.png)

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.
Enter fullscreen mode Exit fullscreen mode

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 !