Skip to content

Instantly share code, notes, and snippets.

@jonnyeom
Created December 8, 2023 17:16
Show Gist options
  • Save jonnyeom/77ab72348c80c289b052156d3ea6882e to your computer and use it in GitHub Desktop.
Save jonnyeom/77ab72348c80c289b052156d3ea6882e to your computer and use it in GitHub Desktop.
Sample methodology to automate replacing api-platform 2.x deprecations
<?php
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symfony\Component\Finder\Finder;
static $finder = new Finder();
$finder
->path('api-platform/core/src')
->name('deprecation.php')
->in(__DIR__ . '/../../../../../vendor');
return static function (RectorConfig $rectorConfig) use ($finder): void {
// Your rector config
...
// Here is the magic!
if ($finder->count() === 1) {
require current(iterator_to_array($finder))->getRealPath();
}
/**
* $deprecatedClassesWithAliases is simple namespace change
* $deprecatedClassesWithoutAliases is namespace change + interface changed
*/
if (! isset($deprecatedInterfaces, $deprecatedClassesWithAliases, $deprecatedClassesWithoutAliases)) {
return;
}
// Deprecations that dont work with legacy metadata switch on.
unset($deprecatedClassesWithoutAliases['ApiPlatform\Core\DataProvider\Pagination']);
unset($deprecatedInterfaces['ApiPlatform\Core\DataProvider\PaginatorInterface']);
unset($deprecatedInterfaces['ApiPlatform\Core\JsonSchema\SchemaFactoryInterface']);
unset($deprecatedInterfaces['ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\Factory\DocumentMetadataFactoryInterface']);
// Replace deprecations in code.
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, array_merge($deprecatedInterfaces, $deprecatedClassesWithAliases, $deprecatedClassesWithoutAliases, [
'ApiPlatform\Core\Bridge\Elasticsearch\Serializer\ItemNormalizer' => 'ApiPlatform\Elasticsearch\Serializer\ItemNormalizer',
'ApiPlatform\Core\Bridge\Elasticsearch\DataProvider\Extension\RequestBodySearchCollectionExtensionInterface' => 'ApiPlatform\Elasticsearch\Extension\RequestBodySearchCollectionExtensionInterface',
]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment