Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@ public function restoreThis(self $restoreThisScope): self
$expressionTypes,
$nativeExpressionTypes,
$this->conditionalExpressions,
$this->inClosureBindScopeClasses,
$restoreThisScope->inClosureBindScopeClasses,
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
[],
Expand Down Expand Up @@ -2059,6 +2059,31 @@ public function isInClosureBind(): bool
return $this->inClosureBindScopeClasses !== [];
}

/**
* @param list<non-empty-string> $scopeClasses
*/
public function withClosureBindScopeClasses(array $scopeClasses): self
{
return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
$this->expressionTypes,
$this->nativeExpressionTypes,
$this->conditionalExpressions,
$scopeClasses,
$this->anonymousFunctionReflection,
$this->isInFirstLevelStatement(),
$this->currentlyAssignedExpressions,
$this->currentlyAllowedUndefinedExpressions,
$this->inFunctionCallsStack,
$this->afterExtractCall,
$this->parentScope,
$this->nativeTypesPromoted,
);
}

/**
* @api
* @param ParameterReflection[]|null $callableParameters
Expand Down Expand Up @@ -2397,6 +2422,23 @@ public function getFunctionType($type, bool $isNullable, bool $isVariadic): Type
false,
)), new AccessoryArrayListType()]);
}
if (
$type instanceof Name
&& $this->inClosureBindScopeClasses !== []
&& $this->inClosureBindScopeClasses !== ['static']
&& in_array($type->toLowerString(), ['static', 'self', 'parent'], true)
&& $this->reflectionProvider->hasClass($this->inClosureBindScopeClasses[0])
Comment thread
calebdw marked this conversation as resolved.
) {
return $this->initializerExprTypeResolver->getFunctionType(
$type,
$isNullable,
Comment thread
calebdw marked this conversation as resolved.
false,
InitializerExprContext::fromClassReflection(
$this->reflectionProvider->getClass($this->inClosureBindScopeClasses[0]),
),
);
}

return $this->initializerExprTypeResolver->getFunctionType($type, $isNullable, false, InitializerExprContext::fromScope($this));
}

Expand Down
6 changes: 4 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3656,7 +3656,8 @@ public function processArgs(
$closureThisType = $this->resolveClosureThisType($callLike, $calleeReflection, $parameter, $scopeToPass);
if ($closureThisType !== null) {
$restoreThisScope = $scopeToPass;
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes());
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes())
->withClosureBindScopeClasses($closureThisType->getObjectClassNames());
}
}

Expand Down Expand Up @@ -3722,7 +3723,8 @@ public function processArgs(
) {
$closureThisType = $this->resolveClosureThisType($callLike, $calleeReflection, $parameter, $scopeToPass);
if ($closureThisType !== null) {
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes());
$scopeToPass = $scopeToPass->assignVariable('this', $closureThisType, new ObjectWithoutClassType(), TrinaryLogic::createYes())
->withClosureBindScopeClasses($closureThisType->getObjectClassNames());
}
}

Expand Down
72 changes: 72 additions & 0 deletions tests/PHPStan/Analyser/nsrt/param-closure-this.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,75 @@ public function doFoo(): void
}

}

class FooParent
{

}

class FooChild extends FooParent
{

private string $secret = 'secret';

}

class StaticReturnClosureThis
{

/**
* @param-closure-this FooChild $cb
*/
public function withFooChild(callable $cb): void
{

}

/**
* @param-closure-this Foo $cb
*/
public function withFoo(callable $cb): void
{

}

}

class TestStaticResolutionInParamClosureThis
Comment thread
calebdw marked this conversation as resolved.
{

public function testStaticReturnType(StaticReturnClosureThis $o): void
{
$o->withFoo(function (): static {
assertType('ParamClosureThis\Foo', $this);
return $this;
});

$o->withFoo(fn (): static => $this);
}

public function testSelfReturnType(StaticReturnClosureThis $o): void
{
$o->withFooChild(function (): self {
assertType('ParamClosureThis\FooChild', $this);
return $this;
});
}

public function testParentReturnType(StaticReturnClosureThis $o): void
{
$o->withFooChild(function (): parent {
assertType('ParamClosureThis\FooChild', $this);
return $this;
});
}

public function testNullableStaticReturnType(StaticReturnClosureThis $o): void
{
$o->withFoo(function (): ?static {
assertType('ParamClosureThis\Foo', $this);
return rand() ? $this : null;
});
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3394,6 +3394,15 @@ public function testClosureBindToParamClosureThis(): void
]);
}

public function testBug11010(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-11010.php'], []);
}

public function testPureCallable(): void
{
$this->checkThisOnly = false;
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-11010.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Bug11010;

class HelloWorld
{
protected function sayHello(): void
{
echo 'Hello';
}

/**
* @param-closure-this self $cb
*/
public static function cb(\Closure $cb): void
{
$cb = $cb->bindTo(new self, self::class);
if ($cb) {
$cb();
}
}
}

function (): void {
HelloWorld::cb(function () {
$this->sayHello();
});
};
Loading