Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/Analyser/ExprHandler/ClosureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function supports(Expr $expr): bool
public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult
{
$processClosureResult = $nodeScopeResolver->processClosureNode($stmt, $expr, $scope, $storage, $nodeCallback, $context, null);
$scope = $processClosureResult->applyByRefUseScope($processClosureResult->getScope());
$scope = $processClosureResult->applyByRefUseScope($processClosureResult->getScope(), null);

return new ExpressionResult(
$scope,
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3808,7 +3808,7 @@

$variableType = $closureScope->getVariableType($variableName);

if ($prevScope !== null) {
if ($prevScope !== null && $prevScope->hasVariableType($variableName)->yes()) {

Check warning on line 3811 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $variableType = $closureScope->getVariableType($variableName); - if ($prevScope !== null && $prevScope->hasVariableType($variableName)->yes()) { + if ($prevScope !== null && !$prevScope->hasVariableType($variableName)->no()) { $prevVariableType = $prevScope->getVariableType($variableName); if (!$variableType->equals($prevVariableType)) { $variableType = TypeCombinator::union($variableType, $prevVariableType);

Check warning on line 3811 in src/Analyser/MutatingScope.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $variableType = $closureScope->getVariableType($variableName); - if ($prevScope !== null && $prevScope->hasVariableType($variableName)->yes()) { + if ($prevScope !== null && !$prevScope->hasVariableType($variableName)->no()) { $prevVariableType = $prevScope->getVariableType($variableName); if (!$variableType->equals($prevVariableType)) { $variableType = TypeCombinator::union($variableType, $prevVariableType);
$prevVariableType = $prevScope->getVariableType($variableName);
if (!$variableType->equals($prevVariableType)) {
$variableType = TypeCombinator::union($variableType, $prevVariableType);
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3455,7 +3455,7 @@ public function processArgs(
}

foreach ($deferredByRefClosureResults as $deferredClosureResult) {
$scope = $deferredClosureResult->applyByRefUseScope($scope);
$scope = $deferredClosureResult->applyByRefUseScope($scope, $scope);
}

if ($parameters !== null) {
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/ProcessClosureResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function getScope(): MutatingScope
return $this->scope;
}

public function applyByRefUseScope(MutatingScope $scope): MutatingScope
public function applyByRefUseScope(MutatingScope $scope, ?MutatingScope $prevScope): MutatingScope
{
if ($this->byRefClosureResultScope === null) {
return $scope;
}

return $scope->processClosureScope($this->byRefClosureResultScope, null, $this->byRefUses);
return $scope->processClosureScope($this->byRefClosureResultScope, $prevScope, $this->byRefUses);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14096.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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) {
return $simulateRequestFx($view);
});

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