Skip to content

Commit a1b05f3

Browse files
committed
Script to rename methods
1 parent 3d90f97 commit a1b05f3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

convert_codestyle.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function print_usage_and_exit(string $msg = '') {
77
}
88

99
function convert_codestyle() {
10+
error_reporting(E_ALL);
1011
global $argv;
1112
if (count($argv) !== 2) {
1213
print_usage_and_exit();
@@ -26,6 +27,23 @@ function convert_codestyle() {
2627
return '_' . strtolower($msg[0]) . $msg[1];
2728
}, $args[2]);
2829
}, $contents);
30+
31+
preg_match_all('@\bfunction\s+(_?[a-z]+(_[a-z]+)+)\s*\(@', $new_contents, $matches);
32+
$function_names = $matches[1];
33+
$function_names_set = array_flip($function_names);
34+
$new_contents = preg_replace_callback('@\b_?[a-z]+(_[a-z]+)+\b@', function(array $args) use ($function_names_set) : string {
35+
$name = $args[0];
36+
if (!isset($function_names_set[$name])) {
37+
return $name;
38+
}
39+
$name = ltrim($name, '_');
40+
$name = preg_replace_callback('@_[a-z]@', function(array $args) {
41+
$msg = $args[0];
42+
assert(strlen($msg) === 2);
43+
return strtoupper($msg[1]);
44+
}, $name);
45+
return $name;
46+
}, $new_contents);
2947
echo $new_contents;
3048
}
3149

0 commit comments

Comments
 (0)