Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -1457,8 +1457,12 @@ Use the base collection methods instead of their aliases.
$collection = new Collection([0, 1, null, -1]);
-$collection->average();
-$collection->some(fn (?int $number): bool => is_null($number));
-$collection->unlessEmpty(fn(Collection $collection) => $collection->push('Foo'));
-$collection->unlessNotEmpty(fn(Collection $collection) => $collection->push('Foo'));
+$collection->avg();
+$collection->contains(fn (?int $number): bool => is_null($number));
+$collection->whenNotEmpty(fn(Collection $collection) => $collection->push('Foo'));
+$collection->whenEmpty(fn(Collection $collection) => $collection->push('Foo'));
```

<br>
Expand Down
7 changes: 6 additions & 1 deletion src/Rector/MethodCall/UnaliasCollectionMethodsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ private function updateMethodCall(MethodCall $methodCall): ?MethodCall
$replacement = 'contains';
} elseif ($this->isName($name, 'average')) {
$replacement = 'avg';
} else {
} elseif ($this->isName($name, 'unlessEmpty')) {
$replacement = 'whenNotEmpty';
} elseif ($this->isName($name, 'unlessNotEmpty')) {
$replacement = 'whenEmpty';
}
else {
return null;
}

Expand Down
22 changes: 22 additions & 0 deletions stubs/Illuminate/Support/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,26 @@ public function filter(?callable $callback = null);
* @return static
*/
public function reject($callback = true);

/**
* Apply the callback unless the collection is empty.
*
* @template TUnlessEmptyReturnType
*
* @param callable($this): TUnlessEmptyReturnType $callback
* @param (callable($this): TUnlessEmptyReturnType)|null $default
* @return $this|TUnlessEmptyReturnType
*/
public function unlessEmpty();

/**
* Apply the callback unless the collection is not empty.
*
* @template TUnlessNotEmptyReturnType
*
* @param callable($this): TUnlessNotEmptyReturnType $callback
* @param (callable($this): TUnlessNotEmptyReturnType)|null $default
* @return $this|TUnlessNotEmptyReturnType
*/
public function unlessNotEmpty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use Illuminate\Support\Collection;
$collection = new Collection([0, 1, null, -1]);
$collection->average();
$collection->some(fn (?int $number): bool => is_null($number));
$collection->unlessEmpty(fn(Collection $collection) => $collection->push('Foo'));
$collection->unlessNotEmpty(fn(Collection $collection) => $collection->push('Foo'));

?>
-----
Expand All @@ -19,5 +21,7 @@ use Illuminate\Support\Collection;
$collection = new Collection([0, 1, null, -1]);
$collection->avg();
$collection->contains(fn (?int $number): bool => is_null($number));
$collection->whenNotEmpty(fn(Collection $collection) => $collection->push('Foo'));
$collection->whenEmpty(fn(Collection $collection) => $collection->push('Foo'));

?>
Loading