Skip to content

Commit 8eaeea5

Browse files
committed
Add script to change codestyle
1 parent 242577d commit 8eaeea5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

convert_codestyle.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
function print_usage_and_exit(string $msg = '') {
4+
global $argv;
5+
fprintf(STDERR, "%sUsage: %s pathname.php\nConverts fooBar to foo_bar in variable names\n", $msg ? $msg . "\n" : '', $argv[0]);
6+
exit(1);
7+
}
8+
9+
function convert_codestyle() {
10+
global $argv;
11+
if (count($argv) !== 2) {
12+
print_usage_and_exit();
13+
}
14+
$path = $argv[1];
15+
if (!file_exists($path)) {
16+
print_usage_and_exit("File '$path' does not exist");
17+
}
18+
$contents = file_get_contents($path);
19+
// Conservatively replace fooBar, but not fooBAR or fooB
20+
$new_contents = preg_replace_callback('@(\$)([a-z][A-Za-z0-9]*([A-Z][a-z0-9]+)+)\b@', function(array $args) : string {
21+
list($prefix, $msg) = $args;
22+
return $args[1] . preg_replace_callback('@[A-Z][a-z0-9]@', function(array $inner_args) {
23+
24+
$msg = $inner_args[0];
25+
assert(strlen($msg) === 2);
26+
return '_' . strtolower($msg[0]) . $msg[1];
27+
}, $args[2]);
28+
}, $contents);
29+
echo $new_contents;
30+
}
31+
32+
convert_codestyle();

0 commit comments

Comments
 (0)