Find the best Rector rule to solve your problem. Searching through 897 rules.

Found 14 rules:

ForeachToArrayAnyRector

Replace foreach with boolean assignment + break OR foreach with early return with array_any

-$found = false;
-foreach ($animals as $animal) {
-    if (str_starts_with($animal, 'c')) {
-        $found = true;
-        break;
-    }
-}
+$found = array_any($animals, fn($animal) => str_starts_with($animal, 'c'));
SETS:  PHP 8.4 Polyfills

ForeachToArrayFindKeyRector

Replace foreach with assignment and break with array_find_key

 $animals = ['dog', 'cat', 'cow', 'duck', 'goose'];

-$found = null;
-foreach ($animals as $idx => $animal) {
-    if (str_starts_with($animal, 'c')) {
-        $found = $idx;
-        break;
-    }
-}
+$found = array_find_key($animals, fn($animal) => str_starts_with($animal, 'c'));
SETS:  PHP 8.4 Polyfills

ForeachToArrayAllRector

Replace foreach with boolean assignment and break OR foreach with early return with array_all

-$found = true;
-foreach ($animals as $animal) {
-    if (!str_starts_with($animal, 'c')) {
-        $found = false;
-        break;
-    }
-}
+$found = array_all($animals, fn($animal) => str_starts_with($animal, 'c'));
SETS:  PHP 8.4 Polyfills

ForeachToArrayFindRector

Replace foreach with assignment and break with array_find

-$found = null;
-foreach ($animals as $animal) {
-    if (str_starts_with($animal, 'c')) {
-        $found = $animal;
-        break;
-    }
-}
+$found = array_find($animals, fn($animal) => str_starts_with($animal, 'c'));
SETS:  PHP 8.4 Polyfills

DeprecatedAnnotationToDeprecatedAttributeRector

Change @deprecated annotation to Deprecated attribute

-/**
- * @deprecated 1.0.0 Use SomeOtherFunction instead
- */
+#[\Deprecated(message: 'Use SomeOtherFunction instead', since: '1.0.0')]
 function someFunction()
 {
 }
SETS:  PHP 8.4 Polyfills

ArrayKeyFirstLastRector

Make use of array_key_first() and array_key_last()

-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
SETS:  PHP 7.3 Polyfills

IsCountableRector

Changes is_array + Countable check to is_countable

-is_array($foo) || $foo instanceof Countable;
+is_countable($foo);
SETS:  PHP 7.3 Polyfills

JsonValidateRector

Replace json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE with json_validate()

-if (json_decode($json, true) !== null && json_last_error() === JSON_ERROR_NONE) {
+if (json_validate($json)) {
 }
SETS:  PHP 8.3 Polyfills

Configurable

AddOverrideAttributeToOverriddenMethodsRector

Add override attribute to overridden methods

 class ParentClass
 {
     public function foo()
     {
         echo 'default';
     }
 }

 final class ChildClass extends ParentClass
 {
+    #[\Override]
     public function foo()
     {
         echo 'override default';
     }
 }
SETS:  PHP 8.3 Polyfills

StrEndsWithRector

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);
     }
 }
SETS:  PHP 8.0 Polyfills

StrStartsWithRector

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);
     }
 }
SETS:  PHP 8.0 Polyfills

StrContainsRector

Replace strpos() !== false and strstr() with str_contains()

 class SomeClass
 {
     public function run()
     {
-        return strpos('abc', 'a') !== false;
+        return str_contains('abc', 'a');
     }
 }
SETS:  PHP 8.0 Polyfills

MbStrContainsRector

Replace mb_strpos() !== false and mb_strstr() with str_contains()

 class SomeClass
 {
     public function run()
     {
-        return mb_strpos('abc', 'a') !== false;
+        return str_contains('abc', 'a');
     }
 }
SETS:  Polyfills

GetDebugTypeRector

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);
     }
 }
SETS:  PHP 8.0 Polyfills