DEV Community

Richard McHale
Richard McHale

Posted on

2

Automatically switching laminas-form Annotations to Attributes with Rector

I couldn't find anything else demonstrating this so here's the rector config I used in case it's any use to someone else

<?php

declare(strict_types=1);

use Laminas\Form\Annotation\AllowEmpty;
use Laminas\Form\Annotation\Filter;
use Laminas\Form\Annotation\Name;
use Laminas\Form\Annotation\Required;
use Laminas\Form\Annotation\Validator;
use Rector\Config\RectorConfig;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->paths([
        __DIR__ . '/module/Application/src',
    ]);

    $rectorConfig->importNames();

    $rectorConfig->ruleWithConfiguration(
        AnnotationToAttributeRector::class,
        [
            new AnnotationToAttribute(Filter::class),
            new AnnotationToAttribute(Name::class),
            new AnnotationToAttribute(Required::class),
            new AnnotationToAttribute(Validator::class),
            new AnnotationToAttribute(AllowEmpty::class),
        ]);

    $rectorConfig->rule(StringClassNameToClassConstantRector::class);
};

Enter fullscreen mode Exit fullscreen mode

You will almost certainly need to change the config paths, and add any missing Annotations to the array

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay