Find the best Rector rule to solve your problem
Add explicit public method visibility.
class SomeClass
{
- function foo()
+ public function foo()
{
}
}
Changes use of function calls to use constants
class SomeClass
{
public function run()
{
- $value = php_sapi_name();
+ $value = PHP_SAPI;
}
}
Changes various implode forms to consistent one
class SomeClass
{
public function run(array $items)
{
- $itemsAsStrings = implode($items);
- $itemsAsStrings = implode($items, '|');
+ $itemsAsStrings = implode('', $items);
+ $itemsAsStrings = implode('|', $items);
}
}
Refactor call_user_func() on known class method to a method call
final class SomeClass
{
public function run()
{
- $result = \call_user_func([$this->property, 'method'], $args);
+ $result = $this->property->method($args);
}
}
Makes array_search search for identical elements
-array_search($value, $items);
+array_search($value, $items, true);
Replace call_user_func_array() with variadic
class SomeClass
{
public function run()
{
- call_user_func_array('some_function', $items);
+ some_function(...$items);
}
}
Change count array comparison to empty array comparison to improve performance
-count($array) === 0;
-count($array) > 0;
-! count($array);
+$array === [];
+$array !== [];
+$array === [];
Changes use of call to version compare function to use of PHP version constant
class SomeClass
{
public function run()
{
- version_compare(PHP_VERSION, '5.3.0', '<');
+ PHP_VERSION_ID < 50300;
}
}
Type and name of catch exception should match
try {
// ...
-} catch (SomeException $typoException) {
- $typoException->getMessage();
+} catch (SomeException $someException) {
+ $someException->getMessage();
}
Separate grouped properties to own lines
class SomeClass
{
/**
* @var string
*/
- public $isIt, $isIsThough;
+ public $isIt;
+
+ /**
+ * @var string
+ */
+ public $isIsThough;
}
Split multiple inline assigns to each own lines default value, to prevent undefined array issues
class SomeClass
{
public function run()
{
- $one = $two = 1;
+ $one = 1;
+ $two = 1;
}
}
Add new line after statements to tidify code
class SomeClass
{
public function first()
{
}
+
public function second()
{
}
}
Remove useless alias in use statement as same name with last use statement name
-use App\Bar as Bar;
+use App\Bar;
Wrap encapsed variables in curly braces
function run($world)
{
- echo "Hello $world!";
+ echo "Hello {$world}!";
}
Convert enscaped {$string} to more readable sprintf or concat, if no mask is used
-echo "Unsupported format {$format} - use another";
+echo sprintf('Unsupported format %s - use another', $format);
-echo "Try {$allowed}";
+echo 'Try ' . $allowed;
Use class
keyword for class name resolution in string instead of hardcoded string reference
-$value = 'App\SomeClass::someMethod()';
+$value = \App\SomeClass::class . '::someMethod()';
Prefer quote that are not inside the string
class SomeClass
{
public function run()
{
- $name = "\" Tom";
- $name = '\' Sara';
+ $name = '" Tom';
+ $name = "' Sara";
}
}
Assign outcome of ternary condition to variable, where applicable
function ternary($value)
{
- $value ? $a = 1 : $a = 0;
+ $a = $value ? 1 : 0;
}
Separate class constant to own lines
class SomeClass
{
- const HI = true, HELLO = 'true';
+ const HI = true;
+ const HELLO = 'true';
}
Remove final from constants in classes defined as final
final class SomeClass
{
- final public const NAME = 'value';
+ public const NAME = 'value';
}
Changes negate of empty comparison of nullable value to explicit === or !== compare
/** @var stdClass|null $value */
-if ($value) {
+if ($value !== null) {
}
-if (!$value) {
+if ($value === null) {
}
Make method visibility same as parent one
class ChildClass extends ParentClass
{
- public function run()
+ protected function run()
{
}
}
class ParentClass
{
protected function run()
{
}
}
Add extra space before new assign set
final class SomeClass
{
public function run()
{
$value = new Value;
$value->setValue(5);
+
$value2 = new Value;
$value2->setValue(1);
}
}
Refactor func_get_args() in to a variadic param
-function run()
+function run(...$args)
{
- $args = \func_get_args();
}
Split multi use imports and trait statements to standalone lines
-use A, B;
+use A;
+use B;
class SomeClass
{
- use SomeTrait, AnotherTrait;
+ use SomeTrait;
+ use AnotherTrait;
}
Replace string class names by
class AnotherClass
{
}
class SomeClass
{
public function run()
{
- return 'AnotherClass';
+ return \AnotherClass::class;
}
}