88use PHPStan \Analyser \Scope ;
99use PHPStan \Reflection \FunctionReflection ;
1010use PHPStan \Reflection \ParameterReflection ;
11- use PHPStan \Reflection \ReflectionProvider ;
1211use PHPStan \Type \FunctionParameterClosureThisExtension ;
13- use PHPStan \Type \ObjectType ;
1412use PHPStan \Type \Type ;
15- use PHPStan \Type \TypeCombinator ;
16- use PHPUnit \Framework \TestCase ;
1713
1814final class TestClosureThisTypeExtension implements FunctionParameterClosureThisExtension
1915{
@@ -31,8 +27,7 @@ final class TestClosureThisTypeExtension implements FunctionParameterClosureThis
3127 ];
3228
3329 public function __construct (
34- private readonly PestConfigReader $ pestConfigReader ,
35- private readonly ReflectionProvider $ reflectionProvider ,
30+ private readonly PestTestCaseType $ pestTestCaseType ,
3631 ) {}
3732
3833 public function isFunctionSupported (FunctionReflection $ functionReflection , ParameterReflection $ parameter ): bool
@@ -49,43 +44,6 @@ public function getClosureThisTypeFromFunctionCall(
4944 ParameterReflection $ parameter ,
5045 Scope $ scope
5146 ): Type {
52- $ types = $ this ->toClassObjectTypes (
53- $ this ->pestConfigReader ->resolveFileBindings ($ scope ->getFile ()),
54- );
55-
56- if ($ types === []) {
57- $ types = $ this ->toClassObjectTypes (
58- $ this ->pestConfigReader ->resolveBindings ($ scope ->getFile ()),
59- );
60- }
61-
62- if ($ types === []) {
63- return new ObjectType (TestCase::class);
64- }
65-
66- return count ($ types ) === 1 ? $ types [0 ] : TypeCombinator::intersect (...$ types );
67- }
68-
69- /**
70- * @param list<string> $bindings
71- * @return list<ObjectType>
72- */
73- private function toClassObjectTypes (array $ bindings ): array
74- {
75- $ types = [];
76-
77- foreach ($ bindings as $ binding ) {
78- if (! $ this ->reflectionProvider ->hasClass ($ binding )) {
79- continue ;
80- }
81-
82- if ($ this ->reflectionProvider ->getClass ($ binding )->isTrait ()) {
83- continue ;
84- }
85-
86- $ types [] = new ObjectType ($ binding );
87- }
88-
89- return $ types ;
47+ return $ this ->pestTestCaseType ->resolve ($ scope ->getFile ());
9048 }
9149}
0 commit comments