- Added a cleanup method to the migration process

This commit is contained in:
Dave Mc Nicoll 2023-02-23 09:36:54 -05:00
parent 564a05a1d4
commit e3e922e3bf
1 changed files with 70 additions and 61 deletions

View File

@ -3,7 +3,7 @@
array_shift($argv);
$opt = getopt('c::f:v', [ 'folder:', 'confirm', 'verbose' ]);
$opt = getopt('c::f:v', [ 'folder:', 'confirm', 'verbose', 'clean' ]);
if ( ! $opt ) {
exit("Bad arguments provided, you must specify a fullpath using the -f or --folder option\n");
@ -30,70 +30,79 @@ foreach ($iterator as $info) {
$content = file_get_contents($info->getPathname());
foreach(explode(PHP_EOL, $content) as $lineIdx => $line) {
$tline = trim($line);
if ( isset($opt['clean']) ) {
foreach (explode(PHP_EOL, $content) as $lineIdx => $line) {
if ( $pos = strpos($line, '# migrated from: ') ) {
$attr = true;
if ($tline === "/**") {
$opened = true;
}
elseif ($tline === '*/') {
$opened = false;
}
elseif (substr($tline, 0, 1) === '*') {
$annotation = trim(substr($tline, 1));
$newContent[] = $newLine = substr($line, 0, $pos);
if (strpos($annotation, '@') === 0 ) {
$argpos = strpos($annotation, '(');
if ($argpos !== false) {
$name = substr($annotation, 1, $argpos - 1);
$args = substr($annotation, $argpos + 1, strrpos($annotation, ')') - $argpos - 1);
try{
$array = eval("return [{$args}];");
$renderedArgs = [];
foreach($array as $key => $argument) {
if ( is_array($argument) ) {
$argument = var_export($argument, true);
}
elseif ( is_bool($argument) ) {
$argument = $argument ? 'true' : 'false';
}
elseif (! is_numeric($argument) ) {
$argument = "\"$argument\"";
}
if (is_numeric($key)) {
$renderedArgs[] = $argument;
}
else {
$renderedArgs[] = "$key: $argument";
}
}
$renderedArgs = implode(', ', $renderedArgs);
}
catch(\Throwable $e) {
echo sprintf("!!! WARNING : %s in file (%s) line %d ; using previous notation.\n", $e->getMessage(), $info->getPathname(), $lineIdx + 1);
$renderedArgs = str_replace(' => ', ': ', $args);
}
}
else {
$name = substr($annotation, 1);
$renderedArgs = "";
}
$attr[] = $attribute = sprintf("%s#[%s%s]%s", str_repeat(" ", strlen($line) - strlen(ltrim($line)) >= 4 ? 4 : 0), $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? " # migrated from: $args" : "");
$newContent[] = $attribute;
echo sprintf("Changed line '%s' \n", trim($newLine));
}
else {
$newContent[] = $line;
}
}
else {
$newContent[] = $line;
}
else {
foreach (explode(PHP_EOL, $content) as $lineIdx => $line) {
$tline = trim($line);
if ($tline === "/**") {
$opened = true;
} elseif ($tline === '*/') {
$opened = false;
} elseif (substr($tline, 0, 1) === '*') {
$annotation = trim(substr($tline, 1));
if (strpos($annotation, '@') === 0) {
$argpos = strpos($annotation, '(');
if ($argpos !== false) {
$name = substr($annotation, 1, $argpos - 1);
$args = substr($annotation, $argpos + 1, strrpos($annotation, ')') - $argpos - 1);
try {
$array = eval("return [{$args}];");
$renderedArgs = [];
foreach ($array as $key => $argument) {
if (is_array($argument)) {
$argument = var_export($argument, true);
} elseif (is_bool($argument)) {
$argument = $argument ? 'true' : 'false';
} elseif (!is_numeric($argument)) {
$argument = "\"$argument\"";
}
if (is_numeric($key)) {
$renderedArgs[] = $argument;
} else {
$renderedArgs[] = "$key: $argument";
}
}
$renderedArgs = implode(', ', $renderedArgs);
} catch (\Throwable $e) {
echo sprintf("!!! WARNING : %s in file (%s) line %d ; using previous notation.\n", $e->getMessage(), $info->getPathname(), $lineIdx + 1);
$renderedArgs = str_replace(' => ', ': ', $args);
}
} else {
$name = substr($annotation, 1);
$renderedArgs = "";
}
$attr[] = $attribute = sprintf("%s#[%s%s]%s", str_repeat(" ", strlen($line) - strlen(ltrim($line)) >= 4 ? 4 : 0), $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? " # migrated from: $args" : "");
$newContent[] = $attribute;
}
} else {
$newContent[] = $line;
}
}
}