Skip to content

Instantly share code, notes, and snippets.

@violarium
Created December 15, 2021 13:05
Show Gist options
  • Save violarium/9dda75b1ac8bc7b78eafd023fed4630e to your computer and use it in GitHub Desktop.
Save violarium/9dda75b1ac8bc7b78eafd023fed4630e to your computer and use it in GitHub Desktop.
doctrine_annotations_to_attributes.php
<?php
declare(strict_types=1);
$dirPath = __DIR__ . '/app/Entities';
$directory = new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory);
/** @var SplFileInfo $file */
foreach ($iterator as $file) {
if (!$file->isFile()) {
continue;
}
if ($file->getExtension() !== 'php') {
return;
}
echo $file->getRealPath() . "\n";
$content = file_get_contents($file->getRealPath());
$newContent = $content;
if (preg_match_all('/\/\*\*[\s\S]*?\*\//', $content, $phpDocMatches)) {
foreach ($phpDocMatches[0] as $phpDoc) {
$newPhpDoc = $phpDoc;
if (preg_match_all('/( *)\*\s*@.*?\n/', $phpDoc, $annotationMatches)) {
foreach ($annotationMatches[0] as $annotationString) {
$attribute = rtrim($annotationString);
$attribute = preg_replace('/\s?\*\s*@\s*/', '#[', $attribute) . ']';
$attribute = str_replace('=', ': ', $attribute);
$attribute = str_replace('"', '\'', $attribute);
$newPhpDoc = str_replace($annotationString, '', $newPhpDoc);
$newPhpDoc .= "\n$attribute";
}
}
$newPhpDoc = preg_replace('/\/\*\*[\s]+\*\//', '', $newPhpDoc);
$newPhpDoc = trim($newPhpDoc);
$newContent = str_replace($phpDoc, $newPhpDoc, $newContent);
}
}
file_put_contents($file->getRealPath(), $newContent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment