Skip to content
Open
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
8 changes: 6 additions & 2 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3330,7 +3330,11 @@ public function processArgs(
}

$this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $storage, $context);
$closureResult = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context, $parameterType ?? null);
$closureScopeToPass = $scopeToPass;
foreach ($deferredByRefClosureResults as $deferredClosureResult) {
$closureScopeToPass = $deferredClosureResult->applyByRefUseScope($closureScopeToPass);
}
$closureResult = $this->processClosureNode($stmt, $arg->value, $closureScopeToPass, $storage, $nodeCallback, $context, $parameterType ?? null);
if ($this->callCallbackImmediately($parameter, $parameterType, $calleeReflection)) {
$throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints()));
$impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints());
Expand All @@ -3348,7 +3352,7 @@ public function processArgs(
$uses[] = $use->var->name;
}

$scope = $closureResult->getScope();
$scope = $scopeToPass;
$deferredByRefClosureResults[] = $closureResult;
$invalidateExpressions = $closureResult->getInvalidateExpressions();
if ($restoreThisScope !== null) {
Expand Down
56 changes: 56 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14096.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types = 1);

namespace Bug14096;

use function PHPStan\Testing\assertType;

class AbstractView {}
class App {}
interface ServerRequestInterface {}

class Test
{
/**
* @template T of App
*
* @param \Closure(ServerRequestInterface): T $createAppFx
* @param \Closure(T): ServerRequestInterface $simulateRequestFx
*
* @return T
*/
protected function simulateAppCallback(\Closure $createAppFx, \Closure $simulateRequestFx): App
{
$appBase = $createAppFx(new class() implements ServerRequestInterface {});
$request = $simulateRequestFx($appBase);

$app = $createAppFx($request);

return $app;
}

/**
* @template T of AbstractView
*
* @param \Closure(ServerRequestInterface): T $createViewFx
* @param \Closure(T): ServerRequestInterface $simulateRequestFx
*
* @return T
*/
protected function simulateViewCallback(\Closure $createViewFx, \Closure $simulateRequestFx): AbstractView
{
$view = null;
$this->simulateAppCallback(static function (ServerRequestInterface $request) use ($createViewFx, &$view) {
$view = $createViewFx($request);

return new App();
}, static function () use ($simulateRequestFx, &$view) {
assertType('T of Bug14096\AbstractView (method Bug14096\Test::simulateViewCallback(), argument)|null', $view);

return $simulateRequestFx($view);
});

assertType('T of Bug14096\AbstractView (method Bug14096\Test::simulateViewCallback(), argument)|null', $view);

return $view;
}
}
Loading