# Modernizing PHPDoc Array annotations with Rector

> Faced with hundreds of outdated PHPDoc array annotations in my Symfony project, I had three choices: manual find-replace hell, AI parsing token limits or building a custom Rector rule. Spoiler alert: I chose the smart path and automated the entire Type\[\] to array transformation in seconds.

Source: https://antistatique.net/en/blog/modernizing-phpdoc-array-annotations-with-rector

Category: Development

# Modernizing PHPDoc Array annotations with Rector

Author(s)

[

![Kevin](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Kevin-300x300.jpg)



](/en/agence/kevin)

[Kevin](/en/agence/kevin)17 October 2025

In one of my project, I’m working with Symfony _Serializer_, _phpstan/phpdoc-parser_ and _phpdocumentor/reflection-docblock_ to handle serialization of complex objects.  
These tools rely on [PHPDoc annotations](https://en.wikipedia.org/wiki/PHPDoc) to understand data types, particularly for arrays of objects.

In PHPDocs, there are (at least) 2 possible syntaxes for array values:

```
/**
 * @param Foo[] $arrayOfFoos
 */
```

```
/**
 * @param array<Foo> $arrayOfFoos
 */
```

**Since 8.1** we have the [concept of](https://www.php.net/manual/en/function.array-is-list.php) `[list](https://www.php.net/manual/en/function.array-is-list.php)` [in PHP](https://www.php.net/manual/en/function.array-is-list.php).  
That makes `Foo[]` ambiguous because is it could be:

-   a list
-   an array where the key is important

`list<Foo>` _is almost the same as_ `array<Foo>` _(implicit_ `int` _keys) or_ `array<int, Foo>` _with a slight difference you can be sure that integers in keys consecutive numbers from 0 up_.

But here’s where things get interesting: with recent updates to tools like **Psalm**, **PHPStan** and Symfony’s Reflection-based Serializer handler, this `Type[]|array` syntax started causing issues. These tools began struggling with the ambiguity - they couldn't reliably determine whether we meant a strict list or a more flexible array structure.

## Exploring Options

I therefore had many solution to resolve my issue:

Faced with hundreds of annotations needing updates across my codebase, I had three potential paths:

1.  **Manual transformation** — Copy-paste and find-replace across hundreds of files. Let’s be honest, this would be error-prone and mind-numbing work. Hard pass.

1.  **AI-assisted refactoring** — Ask Claude or another AI to parse my entire codebase. While tempting, my project has thousands of files that would exceed token limits, and I wanted a repeatable, version-controllable solution.

1.  **Rector rule** — Write once, run everywhere, with the confidence that comes from automated refactoring tools. This felt like the best approach.

The choice was obvious. Plus, creating a Rector rule means other developers facing the same challenge can benefit from the work.

**Plot twist**: While writing this article, I discovered that [PHP CS Fixer](https://cs.symfony.com/) already provides a similar rule called `[phpdoc_array_type](https://cs.symfony.com/doc/rules/phpdoc/phpdoc_array_type.html)` that handles these transformations! Sometimes reinventing the wheel teaches you how wheels work ✨

## A custom Rector Rule

So, instead of manually transforming hundreds of annotations in my project, I created a custom Rector rule that automates this task.  
Here’s how it works:

The `TransformArrayTypeAnnotationRector` Rule

```
final class TransformArrayTypeAnnotationRector extends AbstractRector
{
    public function getRuleDefinition(): RuleDefinition
    {
        return new RuleDefinition(
            'Transform array type annotations from "Type[]|array" to "array<int, Type>"',
            [
                new CodeSample(
                    '/**
                     * @var \App\Entity\Environment[]|array
                     */
                    private $environments;',
                    '/**
                     * @var array<int, \App\Entity\Environment>
                     */
                    private $environments;'
                ),
            ]
        );
    }

    public function getNodeTypes(): array
    {
        return [Property::class, ClassMethod::class, Class_::class];
    }

    public function refactor(Node $node): ?Node
    {
        $docComment = $node->getDocComment();
        if (null === $docComment) {
            return null;
        }
        $originalText = $docComment->getText();
        $transformedText = $this->transformDocComment($originalText);
        if ($originalText === $transformedText) {
            return null;
        }
        $node->setDocComment(new Doc($transformedText));
        return $node;
    }
}
```

## Configuration and Usage

In my `rector.php` file, I registered my custom rule:

```
use App\Utils\Rector\TransformArrayTypeAnnotationRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__.'/src',
        __DIR__.'/tests',
    ]);
    $rectorConfig->rule(TransformArrayTypeAnnotationRector::class);
};
```

## Results

Running this rule on my project automatically transformed 243 files in seconds:

```
./vendor/bin/rector
243/243 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

243 files with changes
Applied rules:
 * TransformArrayTypeAnnotationRector
[OK] 243 files have been changed by Rector
```

Each transformation follows this pattern:

```
/**
- * @var \App\Dto\Output\Category[]|array
+ * @var array<int, \App\Dto\Output\Category>
 */
```

❤️ Love on your keyboards.

## Sources

**PHPStan** (Sept 2025 ) PHPDoc Types.  
[https://phpstan.org/writing-php-code/phpdoc-types](https://phpstan.org/writing-php-code/phpdoc-types)

**PHP** (Feb 2022) `array_is_list`.  
[https://www.php.net/manual/en/function.array-is-list.php](https://www.php.net/manual/en/function.array-is-list.php)

**PHP Coding Standards Fixer** (Feb 2019) PHPDoc `array<T>` type must be used instead of `T[]`.  
[https://cs.symfony.com/doc/rules/phpdoc/phpdoc\_array\_type.html](https://cs.symfony.com/doc/rules/phpdoc/phpdoc_array_type.html)

## Resources

**Claude AI** [https://claude.ai](https://claude.ai/)  
Helped with writing and text refinement.

**Imagen,** [https://deepmind.google/models/imagen/](https://deepmind.google/models/imagen/)  
Generated the very accurate article’s cover illustration.

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
