Find the best Rector rule to solve your problem. Searching through 647 rules.
Found 19 rules:
Change typehint from Exception
to Throwable
.
-function handler(Exception $exception) { ... }
+function handler(Throwable $exception) { ... }
set_exception_handler('handler');
Changes $this->call() to static method to static call
class SomeClass
{
public static function run()
{
- $this->eat();
+ static::eat();
}
public static function eat()
{
}
}
Changes ereg*() to preg*() calls
-ereg("hi")
+preg_match("#hi#");
Changes call_user_method()/call_user_method_array() to call_user_func()/call_user_func_array()
-call_user_method($method, $obj, "arg1", "arg2");
+call_user_func(array(&$obj, "method"), "arg1", "arg2");
Renames mktime() without arguments to time()
class SomeClass
{
public function run()
{
$time = mktime(1, 2, 3);
- $nextTime = mktime();
+ $nextTime = time();
}
}
Changes multiple dirname() calls to one with nesting level
-dirname(dirname($path));
+dirname($path, 2);
Changes rand, srand, and getrandmax to newer alternatives
-rand();
+random_int();
Changes static call to instance call, where not useful
class Something
{
public function doWork()
{
}
}
class Another
{
public function run()
{
- return Something::doWork();
+ return (new Something)->doWork();
}
}
list() assigns variables in reverse order - relevant in array assign
-list($a[], $a[]) = [1, 2];
+list($a[], $a[]) = array_reverse([1, 2]);
list() cannot split string directly anymore, use str_split()
-list($foo) = "string";
+list($foo) = str_split("string");
Ensure variable variables are wrapped in curly braces
function run($foo)
{
- global $$foo->bar;
+ global ${$foo->bar};
}
Convert break outside for/foreach/switch context to return
class SomeClass
{
public function run()
{
if ($isphp5)
return 1;
else
return 2;
- break;
+ return;
}
}
Change if with isset and return to coalesce
class SomeClass
{
private $items = [];
public function resolve($key)
{
- if (isset($this->items[$key])) {
- return $this->items[$key];
- }
-
- return 'fallback value';
+ return $this->items[$key] ?? 'fallback value';
}
}
Use <=> spaceship instead of ternary with same effect
function order_func($a, $b) {
- return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
+ return $a <=> $b;
}
Changes unneeded null check to ?? operator
-$value === null ? 10 : $value;
+$value ?? 10;
Remove first default switch, that is ignored
switch ($expr) {
default:
- echo "Hello World";
-
- default:
echo "Goodbye Moon!";
break;
}
list() cannot be empty
-'list() = $values;'
+'list($unusedGenerated) = $values;'
Changes if/else to spaceship <=> where useful
usort($languages, function ($first, $second) {
-if ($first[0] === $second[0]) {
- return 0;
-}
-
-return ($first[0] < $second[0]) ? 1 : -1;
+return $second[0] <=> $first[0];
});
Changes PHP 4 style constructor to __construct.
class SomeClass
{
- public function SomeClass()
+ public function __construct()
{
}
}