Find a Rector rule

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

Found 5 rules, grouped by set: Clear

Collection 5 matches

Use the base collection methods instead of their aliases.

 use Illuminate\Support\Collection;

 $collection = new Collection([0, 1, null, -1]);
-$collection->average();
-$collection->some(fn (?int $number): bool => is_null($number));
+$collection->avg();
+$collection->contains(fn (?int $number): bool => is_null($number));

Convert toArray() to all() when the collection does not contain any Arrayable objects.

 use Illuminate\Support\Collection;

-new Collection([0, 1, -1])->toArray();
+new Collection([0, 1, -1])->all();

Avoid negated conditionals in filter() by using reject(), or vice versa.

 use Illuminate\Support\Collection;

 $collection = new Collection([0, 1, null, -1]);
-$collection->filter(fn (?int $number): bool => ! is_null($number));
-$collection->filter(fn (?int $number): bool => ! $number);
-$collection->reject(fn (?int $number) => ! $number > 0);
+$collection->reject(fn (?int $number): bool => is_null($number)); // Avoid negation
+$collection->reject(fn (?int $number): bool => (bool) $number); // Explicitly cast
+$collection->filter(fn (?int $number): bool => $number > 0); // Adds return type

Convert negated calls to contains to doesntContain, or vice versa.

 use Illuminate\Support\Collection;

 $collection = new Collection([0, 1, null, -1]);
-! $collection->contains(fn (?int $number): bool => is_null($number));
-! $collection->doesntContain(fn (?int $number) => $number > 0);
+$collection->doesntContain(fn (?int $number): bool => is_null($number));
+$collection->contains(fn (?int $number) => $number > 0);

Convert collection count() comparisons against 0 or 1 to isEmpty() or isNotEmpty()

 use Illuminate\Support\Collection;

 $collection = new Collection([1, 2, 3]);
-$collection->count() > 0;
-$collection->count() === 0;
+$collection->isNotEmpty();
+$collection->isEmpty();