Find the best Rector rule to solve your problem
Turns static call to function call.
-OldClass::oldMethod("args");
+new_function("args");
Streamline the operator arguments of version_compare function
-version_compare(PHP_VERSION, '5.6', 'gte');
+version_compare(PHP_VERSION, '5.6', 'ge');
This Rector adds new default arguments in calls of defined methods and class types.
$someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);
class MyCustomClass extends SomeExampleClass
{
- public function someMethod()
+ public function someMethod($value = true)
{
}
}
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);
}
}
Turns defined function call new one.
-view("...", []);
+Laravel\Templating\render("...", []);
Change get_class($object) to faster $object::class
class SomeClass
{
public function run($object)
{
- return get_class($object);
+ return $object::class;
}
}
Change $this::class to static::class or self::class depends on class modifier
class SomeClass
{
public function run()
{
- return $this::class;
+ return static::class;
}
}
Remove unused variable in catch()
final class SomeClass
{
public function run()
{
try {
- } catch (Throwable $notUsedThrowable) {
+ } catch (Throwable) {
}
}
}
Change helper functions to str_starts_with()
class SomeClass
{
public function run()
{
- $isMatch = substr($haystack, 0, strlen($needle)) === $needle;
+ $isMatch = str_starts_with($haystack, $needle);
- $isNotMatch = substr($haystack, 0, strlen($needle)) !== $needle;
+ $isNotMatch = ! str_starts_with($haystack, $needle);
}
}
Change switch() to match()
-switch ($input) {
- case Lexer::T_SELECT:
- $statement = 'select';
- break;
- case Lexer::T_UPDATE:
- $statement = 'update';
- break;
- default:
- $statement = 'error';
-}
+$statement = match ($input) {
+ Lexer::T_SELECT => 'select',
+ Lexer::T_UPDATE => 'update',
+ default => 'error',
+};
Add Stringable
interface to classes with __toString()
method
-class SomeClass
+class SomeClass implements Stringable
{
- public function __toString()
+ public function __toString(): string
{
return 'I can stringz';
}
}
Change simple property init and assign to constructor promotion
class SomeClass
{
- public float $price;
-
public function __construct(
- float $price = 0.0
+ public float $price = 0.0
) {
- $this->price = $price;
}
}
Changes method visibility from final private to only private
class SomeClass
{
- final private function getter() {
+ private function getter() {
return $this;
}
}
Adds static visibility to __set_state() methods
class SomeClass
{
- public function __set_state($properties) {
+ public static function __set_state($properties) {
}
}
Add missing parameter based on parent class method
class A
{
public function execute($foo)
{
}
}
class B extends A{
- public function execute()
+ public function execute($foo)
{
}
}
Move required parameters after optional ones
class SomeObject
{
- public function run($optional = 1, $required)
+ public function run($required, $optional = 1)
{
}
}