Find a Rector rule
Find the best Rector rule to solve your problem. Searching through 1082 rules.
Found 19 rules, grouped by set: Clear
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);
}
}
Remove unused parent call with no parent class
class OrphanClass
{
public function __construct()
{
- parent::__construct();
}
}
Change get_class($object) to faster $object::class
class SomeClass
{
public function run($object)
{
- return get_class($object);
+ return $object::class;
}
}
Change the integer associative argument of the get_headers() function to a boolean
-get_headers($url, 1);
+get_headers($url, true);
Streamline the operator argument of the version_compare() function
-version_compare(PHP_VERSION, '5.6', 'gte');
+version_compare(PHP_VERSION, '5.6', 'ge');
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_ends_with()
class SomeClass
{
public function run()
{
- $isMatch = substr($haystack, -strlen($needle)) === $needle;
+ $isMatch = str_ends_with($haystack, $needle);
- $isNotMatch = substr($haystack, -strlen($needle)) !== $needle;
+ $isNotMatch = !str_ends_with($haystack, $needle);
}
}
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);
}
}
Replace strpos() !== false and strstr() with str_contains()
class SomeClass
{
public function run()
{
- return strpos('abc', 'a') !== false;
+ return str_contains('abc', 'a');
}
}
Change ternary type resolve to get_debug_type()
class SomeClass
{
public function run($value)
{
- return is_object($value) ? get_class($value) : gettype($value);
+ return get_debug_type($value);
}
}
Change null check ternary around a method call or property fetch to nullsafe operator
-$value = null !== $dateTime ? $dateTime->format('d/m/Y') : '';
+$value = $dateTime?->format('d/m/Y') ?? '';
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;
}
}
Change method visibility from final private to only private
class SomeClass
{
- final private function getter() {
+ private function getter() {
return $this;
}
}
Add 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)
{
}
}
Add reasonable default value when a required parameter follows an optional one
class SomeObject
{
- public function run($optional = 1, int $required)
+ public function run($optional = 1, int $required = 0)
{
}
}