Skip to content

Commit d9f7f2b

Browse files
authored
[9.x] Improve Conditional::when and ::unless docblocks (#41106)
* Improve Conditional::when and ::unless templates * Apply StyleCI
1 parent 37b21f1 commit d9f7f2b

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed

src/Illuminate/Conditionable/Traits/Conditionable.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ trait Conditionable
1010
/**
1111
* Apply the callback if the given "value" is (or resolves to) truthy.
1212
*
13+
* @template TWhenParameter
1314
* @template TWhenReturnType
1415
*
15-
* @param bool $value
16-
* @param (callable($this): TWhenReturnType)|null $callback
17-
* @param (callable($this): TWhenReturnType)|null $default
16+
* @param (callable($this): TWhenParameter)|TWhenParameter $value
17+
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $callback
18+
* @param (callable($this, TWhenParameter): TWhenReturnType)|null $default
1819
* @return $this|TWhenReturnType
1920
*/
2021
public function when($value, callable $callback = null, callable $default = null)
@@ -37,11 +38,12 @@ public function when($value, callable $callback = null, callable $default = null
3738
/**
3839
* Apply the callback if the given "value" is (or resolves to) falsy.
3940
*
41+
* @template TUnlessParameter
4042
* @template TUnlessReturnType
4143
*
42-
* @param bool $value
43-
* @param (callable($this): TUnlessReturnType) $callback
44-
* @param (callable($this): TUnlessReturnType)|null $default
44+
* @param (callable($this): TUnlessParameter)|TUnlessParameter $value
45+
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $callback
46+
* @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $default
4547
* @return $this|TUnlessReturnType
4648
*/
4749
public function unless($value, callable $callback = null, callable $default = null)

types/Support/Collection.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,46 @@
247247

248248
return 'string';
249249
}));
250+
assertType('Illuminate\Support\Collection<int, User>|void', $collection->when('Taylor', function ($collection, $name) {
251+
assertType('Illuminate\Support\Collection<int, User>', $collection);
252+
assertType('string', $name);
253+
}));
254+
assertType(
255+
'Illuminate\Support\Collection<int, User>|void',
256+
$collection->when(
257+
'Taylor',
258+
function ($collection, $name) {
259+
assertType('Illuminate\Support\Collection<int, User>', $collection);
260+
assertType('string', $name);
261+
},
262+
function ($collection, $name) {
263+
assertType('Illuminate\Support\Collection<int, User>', $collection);
264+
assertType('string', $name);
265+
}
266+
)
267+
);
268+
assertType('Illuminate\Support\Collection<int, User>|void', $collection->when(fn () =>'Taylor', function ($collection, $name) {
269+
assertType('Illuminate\Support\Collection<int, User>', $collection);
270+
assertType('string', $name);
271+
}));
272+
assertType(
273+
'Illuminate\Support\Collection<int, User>|void',
274+
$collection->when(
275+
function ($collection) {
276+
assertType('Illuminate\Support\Collection<int, User>', $collection);
277+
278+
return 14;
279+
},
280+
function ($collection, $count) {
281+
assertType('Illuminate\Support\Collection<int, User>', $collection);
282+
assertType('int', $count);
283+
},
284+
function ($collection, $count) {
285+
assertType('Illuminate\Support\Collection<int, User>', $collection);
286+
assertType('int', $count);
287+
}
288+
)
289+
);
250290

251291
assertType('bool|Illuminate\Support\Collection<int, User>', $collection->whenEmpty(function ($collection) {
252292
assertType('Illuminate\Support\Collection<int, User>', $collection);
@@ -289,6 +329,46 @@
289329

290330
return 'string';
291331
}));
332+
assertType('Illuminate\Support\Collection<int, User>|void', $collection->unless('Taylor', function ($collection, $name) {
333+
assertType('Illuminate\Support\Collection<int, User>', $collection);
334+
assertType('string', $name);
335+
}));
336+
assertType(
337+
'Illuminate\Support\Collection<int, User>|void',
338+
$collection->unless(
339+
'Taylor',
340+
function ($collection, $name) {
341+
assertType('Illuminate\Support\Collection<int, User>', $collection);
342+
assertType('string', $name);
343+
},
344+
function ($collection, $name) {
345+
assertType('Illuminate\Support\Collection<int, User>', $collection);
346+
assertType('string', $name);
347+
}
348+
)
349+
);
350+
assertType('Illuminate\Support\Collection<int, User>|void', $collection->unless(fn () => 'Taylor', function ($collection, $name) {
351+
assertType('Illuminate\Support\Collection<int, User>', $collection);
352+
assertType('string', $name);
353+
}));
354+
assertType(
355+
'Illuminate\Support\Collection<int, User>|void',
356+
$collection->unless(
357+
function ($collection) {
358+
assertType('Illuminate\Support\Collection<int, User>', $collection);
359+
360+
return 14;
361+
},
362+
function ($collection, $count) {
363+
assertType('Illuminate\Support\Collection<int, User>', $collection);
364+
assertType('int', $count);
365+
},
366+
function ($collection, $count) {
367+
assertType('Illuminate\Support\Collection<int, User>', $collection);
368+
assertType('int', $count);
369+
}
370+
)
371+
);
292372

293373
assertType('bool|Illuminate\Support\Collection<int, User>', $collection->unlessEmpty(function ($collection) {
294374
assertType('Illuminate\Support\Collection<int, User>', $collection);

0 commit comments

Comments
 (0)