epifrin/rector-custom-rules

这三个rector规则可以将私有方法名称转换为驼峰命名法,将局部变量名称转换为驼峰命名法,并在字符串字面量中将双引号替换为单引号。

0.5.1 2024-02-09 15:55 UTC

This package is auto-updated.

Last update: 2024-09-09 17:31:06 UTC


README

这三个rector规则可以将私有方法名称转换为驼峰命名法,将局部变量名称转换为驼峰命名法,并在字符串字面量中将双引号替换为单引号。

Build status Latest Stable Version PHP Version Require Total Downloads License

安装

composer require --dev epifrin/rector-custom-rules

使用方法

将以下内容添加到您的rector配置中

$rectorConfig->rule(\Epifrin\RectorCustomRules\RectorRules\ConvertPrivateMethodsNameToCamelCaseRector::class);
$rectorConfig->rule(\Epifrin\RectorCustomRules\RectorRules\ConvertLocalVariablesNameToCamelCaseRector::class);
$rectorConfig->rule(\Epifrin\RectorCustomRules\RectorRules\ReplaceDoubleQuotesWithSingleRector::class);

rector规则

将局部变量名称转换为驼峰命名法

class SomeClass 
{
    public function aMethod() 
    {
-        $my_variable = 1;
+        $myVariable = 1;

-        return $my_variable;
+        return $myVariable;
    }
}

将私有方法名称转换为驼峰命名法

为什么只转换私有方法?因为更改私有方法名称比公共或受保护方法名称更安全。

class SomeClass 
{
    public function publicMethod() 
    {
-        $this->my_private_method();
+        $this->myPrivateMethod();

-        self::my_static_private_method();
+        self::myStaticPrivateMethod();
    }
    
-    private function my_private_method() {}
+    private function myPrivateMethod() {}

-    private static function my_static_private_method() {}
+    private static function myStaticPrivateMethod() {}
}

替换双引号为单引号

此规则将字符串字面量中的双引号替换为单引号。如果字符串字面量包含变量或替换,则不会替换双引号。

以下是一个此规则如何工作的示例

class SomeClass
{
    public function someMethod()
    {
-        $string = "This is a simple string";
+        $string = 'This is a simple string';

-        $stringWithVariable = "Hello, $name";
+        $stringWithVariable = "Hello, $name";

-        $stringWithSpecialChar = "String with special char: \n";
+        $stringWithSpecialChar = "String with special char: \n";
    }
}