Find a Rector rule
Find the best Rector rule to solve your problem. Searching through 1005 rules.
Found 13 rules, grouped by set: Clear
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'));
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'));
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'));
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'));
Make use of array_key_first() and array_key_last()
-reset($items);
-$firstKey = key($items);
+$firstKey = array_key_first($items);
Make use of array_key_first() and array_key_last() instead of reading a single key off array_keys()
-$firstKey = current(array_keys($items));
-$lastKey = end(array_keys($items));
+$firstKey = array_key_first($items);
+$lastKey = array_key_last($items);
Changes is_array + Countable check to is_countable
-is_array($foo) || $foo instanceof Countable;
+is_countable($foo);
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)) {
}
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);
}
}
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');
}
}