epifrin/rector-custom-rules

These are three rector rules to convert private method name, to convert local variable name to camel case and to replace double quotes with single quotes in string literals.

0.5.1 2024-02-09 15:55 UTC

This package is auto-updated.

Last update: 2024-10-09 17:36:59 UTC


README

These are three rector rules to convert private method name, to convert local variable name to camel case and to replace double quotes with single quotes in string literals.

Build status Latest Stable Version PHP Version Require Total Downloads License

Installation

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

Usage

Add the following to your rector config:

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

Rector rules

Convert local variable names to camel case

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

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

Convert private method names to camel case

Why only private methods? Because it's safer to change private method names than public or protected method names.

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() {}
}

Replace double quotes with single

This rule replaces double quotes with single quotes in string literals. It does not replace double quotes if the string literal contains variables or substitutions.

Here is an example of how this rule works:

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";
    }
}