Last active
July 19, 2017 07:43
-
-
Save joelhy/3c5bdc9d009892cbf3cb54a8e53467d2 to your computer and use it in GitHub Desktop.
Convert snake case methods, properties and variables to camel case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function snakeToCamel($val) { | |
preg_match('#^_*#', $val, $underscores); | |
$underscores = current($underscores); | |
$camel = str_replace(' ', '', ucwords(str_replace('_', ' ', $val))); | |
$camel = strtolower(substr($camel, 0, 1)).substr($camel, 1); | |
return $underscores.$camel; | |
} | |
function convert($str) { | |
$name = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; | |
$snake_regexps = array( | |
"#->($name)#i", | |
'#\$('.$name.')#i', | |
"#function ($name)#i", | |
); | |
foreach($snake_regexps as $regexp) | |
$str = preg_replace_callback($regexp, function($matches) { | |
//print_r($matches); | |
$camel = snakeToCamel($matches[1]); | |
return str_replace($matches[1],$camel,$matches[0]); | |
},$str); | |
return $str; | |
} | |
$path = $argv[1]; | |
$Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); | |
foreach($Iterator as $file){ | |
if(substr($file,-4) !== '.php') | |
continue; | |
echo($file); | |
$out = convert(file_get_contents($file)); | |
file_put_contents($file, $out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
copy from https://phpixie.com/blog/convert-snake-case-methods-properties-and-variables-to-camel-case.html
usage:
php convertCase.php /projects/project1/