Skip to content

Instantly share code, notes, and snippets.

@BusterNeece
Created December 28, 2021 21:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BusterNeece/230f1bf619740b0564b88a69978de82c to your computer and use it in GitHub Desktop.
Save BusterNeece/230f1bf619740b0564b88a69978de82c to your computer and use it in GitHub Desktop.
Rector script to migrate OpenAPI specifications from PHP Annotations to Attributes
<?php
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$openApiAnnotationsRaw = [
'AdditionalProperties',
'Attachable',
'Components',
'Contact',
'Delete',
'Discriminator',
'Examples',
'ExternalDocumentation',
'Flow',
'Get',
'Head',
'Header',
'Info',
'Items',
'JsonContent',
'License',
'Link',
'MediaType',
'OpenApi',
'Operation',
'Options',
'Parameter',
'Patch',
'PatchItem',
'PathParameter',
'Post',
'Property',
'Put',
'RequestBody',
'Response',
'Schema',
'SecurityScheme',
'Server',
'ServerVariable',
'Tag',
'Trace',
'Xml',
'XmlContent',
];
$openApiAnnotations = [];
foreach ($openApiAnnotationsRaw as $className) {
$openApiAnnotations[] = new \Rector\Php80\ValueObject\AnnotationToAttribute(
'OpenApi\\Annotations\\' . $className,
'OpenApi\\Attributes\\' . $className
);
}
$services->set(AnnotationToAttributeRector::class)
->configure($openApiAnnotations);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment