Skip to content

Commit e3ac5ee

Browse files
committed
[PHP 8.5] Support for Closure::getCurrent()
1 parent a9aae86 commit e3ac5ee

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Php;
4+
5+
use Closure;
6+
use PhpParser\Node\Expr\StaticCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\DependencyInjection\AutowiredService;
9+
use PHPStan\Reflection\MethodReflection;
10+
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
11+
use PHPStan\Type\NeverType;
12+
use PHPStan\Type\Type;
13+
14+
#[AutowiredService]
15+
final class ClosureGetCurrentDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
16+
{
17+
18+
public function getClass(): string
19+
{
20+
return Closure::class;
21+
}
22+
23+
public function isStaticMethodSupported(MethodReflection $methodReflection): bool
24+
{
25+
return $methodReflection->getName() === 'getCurrent';
26+
}
27+
28+
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
29+
{
30+
if (!$scope->isInAnonymousFunction()) {
31+
return new NeverType();
32+
}
33+
34+
return $scope->getAnonymousFunctionReflection();
35+
}
36+
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint >= 8.5
2+
3+
namespace ClosureGetCurrent;
4+
5+
use Closure;
6+
use function PHPStan\Testing\assertType;
7+
8+
function doFoo(): void {
9+
assertType('*NEVER*', Closure::getCurrent());
10+
}
11+
12+
function (int $i): string {
13+
assertType("Closure(int): 'foo'", Closure::getCurrent());
14+
15+
return 'foo';
16+
};

0 commit comments

Comments
 (0)