Skip to content

Commit cf1243f

Browse files
MrYamousGeniJaho
andauthored
Add new alias to UnaliasCollectionMethods (#305)
* add new alias to UnaliasCollectionMethods * cs --------- Co-authored-by: Geni Jaho <[email protected]>
1 parent 9a43971 commit cf1243f

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

docs/rector_rules_overview.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,8 +1457,12 @@ Use the base collection methods instead of their aliases.
14571457
$collection = new Collection([0, 1, null, -1]);
14581458
-$collection->average();
14591459
-$collection->some(fn (?int $number): bool => is_null($number));
1460+
-$collection->unlessEmpty(fn(Collection $collection) => $collection->push('Foo'));
1461+
-$collection->unlessNotEmpty(fn(Collection $collection) => $collection->push('Foo'));
14601462
+$collection->avg();
14611463
+$collection->contains(fn (?int $number): bool => is_null($number));
1464+
+$collection->whenNotEmpty(fn(Collection $collection) => $collection->push('Foo'));
1465+
+$collection->whenEmpty(fn(Collection $collection) => $collection->push('Foo'));
14621466
```
14631467

14641468
<br>

src/Rector/MethodCall/UnaliasCollectionMethodsRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ private function updateMethodCall(MethodCall $methodCall): ?MethodCall
7070
$replacement = 'contains';
7171
} elseif ($this->isName($name, 'average')) {
7272
$replacement = 'avg';
73+
} elseif ($this->isName($name, 'unlessEmpty')) {
74+
$replacement = 'whenNotEmpty';
75+
} elseif ($this->isName($name, 'unlessNotEmpty')) {
76+
$replacement = 'whenEmpty';
7377
} else {
7478
return null;
7579
}

stubs/Illuminate/Support/Enumerable.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,26 @@ public function filter(?callable $callback = null);
7979
* @return static
8080
*/
8181
public function reject($callback = true);
82+
83+
/**
84+
* Apply the callback unless the collection is empty.
85+
*
86+
* @template TUnlessEmptyReturnType
87+
*
88+
* @param callable($this): TUnlessEmptyReturnType $callback
89+
* @param (callable($this): TUnlessEmptyReturnType)|null $default
90+
* @return $this|TUnlessEmptyReturnType
91+
*/
92+
public function unlessEmpty();
93+
94+
/**
95+
* Apply the callback unless the collection is not empty.
96+
*
97+
* @template TUnlessNotEmptyReturnType
98+
*
99+
* @param callable($this): TUnlessNotEmptyReturnType $callback
100+
* @param (callable($this): TUnlessNotEmptyReturnType)|null $default
101+
* @return $this|TUnlessNotEmptyReturnType
102+
*/
103+
public function unlessNotEmpty();
82104
}

tests/Rector/MethodCall/UnaliasCollectionMethodsRector/Fixture/fixture.php.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use Illuminate\Support\Collection;
77
$collection = new Collection([0, 1, null, -1]);
88
$collection->average();
99
$collection->some(fn (?int $number): bool => is_null($number));
10+
$collection->unlessEmpty(fn(Collection $collection) => $collection->push('Foo'));
11+
$collection->unlessNotEmpty(fn(Collection $collection) => $collection->push('Foo'));
1012

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

2327
?>

0 commit comments

Comments
 (0)