Coding style 用于将snake_案例转换为camelCase的代码嗅探器
我需要一个代码嗅探器,用于将我的PHP文件中的所有变量从蛇壳转换为骆驼壳 我不需要在php中对字符串执行此操作的函数,我希望将php文件中的变量转换为camelCase 我很感激你的帮助。我找到了一种方法。 我集成了cakePHP的代码(有扩展名为ctp的文件和set函数传递给view的变量) 将以下代码另存为到camelcase.phpCoding style 用于将snake_案例转换为camelCase的代码嗅探器,coding-style,phpcodesniffer,Coding Style,Phpcodesniffer,我需要一个代码嗅探器,用于将我的PHP文件中的所有变量从蛇壳转换为骆驼壳 我不需要在php中对字符串执行此操作的函数,我希望将php文件中的变量转换为camelCase 我很感激你的帮助。我找到了一种方法。 我集成了cakePHP的代码(有扩展名为ctp的文件和set函数传递给view的变量) 将以下代码另存为到camelcase.php php tocamelcase.php the/path/of/your/app 文件内容: <?php function snakeToCamel(
php tocamelcase.php the/path/of/your/app
文件内容:
<?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) {
global $j;
$name = '/(\$[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(->[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(::[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(\sfunction\s+[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'.
'(\$this->set\(\'[a-zA-Z0-9]+_[a-zA-Z0-9_]+\'\,)|'.
'(\$this->set\(\"[a-zA-Z0-9]+_[a-zA-Z0-9_]+\"\,)'.
'/i';
$str = preg_replace_callback($name, function($matches){
return snakeToCamel($matches[0]);
},$str);
return $str;
}
$path = $argv[1];
$Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$i=1;
foreach($Iterator as $file){
if(substr($file,-4) !== '.php' && substr($file,-4) !== '.ctp')
continue;
echo($i.": ".$file."\n");
$i++;
$out = convert(file_get_contents($file));
file_put_contents($file, $out);
}
链接无效。你能看看吗?