Skip to content

Commit 3043932

Browse files
authored
fix: resolve static/self/parent relative to @param-closure-this bound class
1 parent 4c2586e commit 3043932

5 files changed

Lines changed: 156 additions & 3 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@ public function restoreThis(self $restoreThisScope): self
20202020
$expressionTypes,
20212021
$nativeExpressionTypes,
20222022
$this->conditionalExpressions,
2023-
$this->inClosureBindScopeClasses,
2023+
$restoreThisScope->inClosureBindScopeClasses,
20242024
$this->anonymousFunctionReflection,
20252025
$this->inFirstLevelStatement,
20262026
[],
@@ -2059,6 +2059,31 @@ public function isInClosureBind(): bool
20592059
return $this->inClosureBindScopeClasses !== [];
20602060
}
20612061

2062+
/**
2063+
* @param list<non-empty-string> $scopeClasses
2064+
*/
2065+
public function withClosureBindScopeClasses(array $scopeClasses): self
2066+
{
2067+
return $this->scopeFactory->create(
2068+
$this->context,
2069+
$this->isDeclareStrictTypes(),
2070+
$this->getFunction(),
2071+
$this->getNamespace(),
2072+
$this->expressionTypes,
2073+
$this->nativeExpressionTypes,
2074+
$this->conditionalExpressions,
2075+
$scopeClasses,
2076+
$this->anonymousFunctionReflection,
2077+
$this->isInFirstLevelStatement(),
2078+
$this->currentlyAssignedExpressions,
2079+
$this->currentlyAllowedUndefinedExpressions,
2080+
$this->inFunctionCallsStack,
2081+
$this->afterExtractCall,
2082+
$this->parentScope,
2083+
$this->nativeTypesPromoted,
2084+
);
2085+
}
2086+
20622087
/**
20632088
* @api
20642089
* @param ParameterReflection[]|null $callableParameters
@@ -2397,6 +2422,23 @@ public function getFunctionType($type, bool $isNullable, bool $isVariadic): Type
23972422
false,
23982423
)), new AccessoryArrayListType()]);
23992424
}
2425+
if (
2426+
$type instanceof Name
2427+
&& $this->inClosureBindScopeClasses !== []
2428+
&& $this->inClosureBindScopeClasses !== ['static']
2429+
&& in_array($type->toLowerString(), ['static', 'self', 'parent'], true)
2430+
&& $this->reflectionProvider->hasClass($this->inClosureBindScopeClasses[0])
2431+
) {
2432+
return $this->initializerExprTypeResolver->getFunctionType(
2433+
$type,
2434+
$isNullable,
2435+
false,
2436+
InitializerExprContext::fromClassReflection(
2437+
$this->reflectionProvider->getClass($this->inClosureBindScopeClasses[0]),
2438+
),
2439+
);
2440+
}
2441+
24002442
return $this->initializerExprTypeResolver->getFunctionType($type, $isNullable, false, InitializerExprContext::fromScope($this));
24012443
}
24022444

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3656,7 +3656,8 @@ public function processArgs(
36563656
$closureThisType = $this->resolveClosureThisType($callLike, $calleeReflection, $parameter, $scopeToPass);
36573657
if ($closureThisType !== null) {
36583658
$restoreThisScope = $scopeToPass;
3659-
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes());
3659+
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes())
3660+
->withClosureBindScopeClasses($closureThisType->getObjectClassNames());
36603661
}
36613662
}
36623663

@@ -3722,7 +3723,8 @@ public function processArgs(
37223723
) {
37233724
$closureThisType = $this->resolveClosureThisType($callLike, $calleeReflection, $parameter, $scopeToPass);
37243725
if ($closureThisType !== null) {
3725-
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes());
3726+
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes())
3727+
->withClosureBindScopeClasses($closureThisType->getObjectClassNames());
37263728
}
37273729
}
37283730

‎tests/PHPStan/Analyser/nsrt/param-closure-this.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,75 @@ public function doFoo(): void
316316
}
317317

318318
}
319+
320+
class FooParent
321+
{
322+
323+
}
324+
325+
class FooChild extends FooParent
326+
{
327+
328+
private string $secret = 'secret';
329+
330+
}
331+
332+
class StaticReturnClosureThis
333+
{
334+
335+
/**
336+
* @param-closure-this FooChild $cb
337+
*/
338+
public function withFooChild(callable $cb): void
339+
{
340+
341+
}
342+
343+
/**
344+
* @param-closure-this Foo $cb
345+
*/
346+
public function withFoo(callable $cb): void
347+
{
348+
349+
}
350+
351+
}
352+
353+
class TestStaticResolutionInParamClosureThis
354+
{
355+
356+
public function testStaticReturnType(StaticReturnClosureThis $o): void
357+
{
358+
$o->withFoo(function (): static {
359+
assertType('ParamClosureThis\Foo', $this);
360+
return $this;
361+
});
362+
363+
$o->withFoo(fn (): static => $this);
364+
}
365+
366+
public function testSelfReturnType(StaticReturnClosureThis $o): void
367+
{
368+
$o->withFooChild(function (): self {
369+
assertType('ParamClosureThis\FooChild', $this);
370+
return $this;
371+
});
372+
}
373+
374+
public function testParentReturnType(StaticReturnClosureThis $o): void
375+
{
376+
$o->withFooChild(function (): parent {
377+
assertType('ParamClosureThis\FooChild', $this);
378+
return $this;
379+
});
380+
}
381+
382+
public function testNullableStaticReturnType(StaticReturnClosureThis $o): void
383+
{
384+
$o->withFoo(function (): ?static {
385+
assertType('ParamClosureThis\Foo', $this);
386+
return rand() ? $this : null;
387+
});
388+
}
389+
390+
}

‎tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,6 +3394,15 @@ public function testClosureBindToParamClosureThis(): void
33943394
]);
33953395
}
33963396

3397+
public function testBug11010(): void
3398+
{
3399+
$this->checkThisOnly = false;
3400+
$this->checkNullables = true;
3401+
$this->checkUnionTypes = true;
3402+
$this->checkExplicitMixed = true;
3403+
$this->analyse([__DIR__ . '/data/bug-11010.php'], []);
3404+
}
3405+
33973406
public function testPureCallable(): void
33983407
{
33993408
$this->checkThisOnly = false;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug11010;
4+
5+
class HelloWorld
6+
{
7+
protected function sayHello(): void
8+
{
9+
echo 'Hello';
10+
}
11+
12+
/**
13+
* @param-closure-this self $cb
14+
*/
15+
public static function cb(\Closure $cb): void
16+
{
17+
$cb = $cb->bindTo(new self, self::class);
18+
if ($cb) {
19+
$cb();
20+
}
21+
}
22+
}
23+
24+
function (): void {
25+
HelloWorld::cb(function () {
26+
$this->sayHello();
27+
});
28+
};

0 commit comments

Comments
 (0)