Skip to content

Commit 31d0ce0

Browse files
authored
[12.x] Improve Context::scope() return type (#58012)
* add Context::scope template * types test * proper directory * random string * is that random enough for you, stan? * i give up, make it an int * explicit
1 parent a78e156 commit 31d0ce0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Illuminate/Log/Context/Repository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,14 @@ protected function isHiddenStackable($key)
535535
}
536536

537537
/**
538+
* @template TReturn of mixed
539+
*
538540
* Run the callback function with the given context values and restore the original context state when complete.
539541
*
540-
* @param callable $callback
542+
* @param (callable(): TReturn) $callback
541543
* @param array<string, mixed> $data
542544
* @param array<string, mixed> $hidden
543-
* @return mixed
545+
* @return TReturn
544546
*
545547
* @throws \Throwable
546548
*/

types/Log/Context.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Illuminate\Events\Dispatcher;
4+
use Illuminate\Log\Context\Repository;
5+
6+
use function PHPStan\Testing\assertType;
7+
8+
$repository = new Repository(new Dispatcher());
9+
10+
$value = $repository->scope(fn (): int => random_int(-100, 100));
11+
assertType('int<-100, 100>', $value);
12+
13+
$void = $repository->scope(function () { // @phpstan-ignore method.void
14+
});
15+
assertType('null', $void);

0 commit comments

Comments
 (0)