php全量修改变量为驼峰
<?php
function underscoreToCamelCase(string $name): string
{
return lcfirst(str_replace('_', '', ucwords($name, '_')));
}
function convertVariableNames(string $filePath)
{
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as &$line) {
if (preg_match('/^\s*(?:public|protected|private)\s+\$[a-zA-Z_]\w*/', $line)) {
continue;
}
$pattern = '/\$([a-zA-Z_]\w*)/';
preg_match_all($pattern, $line, $matches);
foreach ($matches[1] as $variable) {
if ($variable === '_') {
continue;
}
if (strpos($variable, '_') !== false && !in_array($variable, ['_POST', '_GET', '_SESSION', '_COOKIE', '_SERVER', '_FILES', '_REQUEST', '_ENV'])) {
$camelCaseVariable = underscoreToCamelCase($variable);
$line = str_replace('$' . $variable, '$' . $camelCaseVariable, $line);
}
}
}
file_put_contents($filePath, implode("\n", $lines));
echo "文件 {$filePath} 中的变量名已转换完成。\n";
}
$directory = "D:/phpstudy_pro/WWW/erp_api/app/Http/Controllers";
$directoryIterator = new RecursiveDirectoryIterator($directory);
$iterator = new RecursiveIteratorIterator($directoryIterator);
foreach ($iterator as $file) {
if ($file->getExtension() === 'php') {
$filePath = $file->getRealPath();
convertVariableNames($filePath);
}
}