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
14 changes: 7 additions & 7 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public function processStmtNode(
return;
}

$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
$gatheredReturnStatements[] = new ReturnStatement($scope->toMutatingScope(), $node);
}, StatementContext::createTopLevel())->toPublic();

$this->callNodeCallback($nodeCallback, new FunctionReturnStatementsNode(
Expand Down Expand Up @@ -804,7 +804,7 @@ public function processStmtNode(
return;
}

$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
$gatheredReturnStatements[] = new ReturnStatement($scope->toMutatingScope(), $node);
}, StatementContext::createTopLevel())->toPublic();

$methodReflection = $methodScope->getFunction();
Expand Down Expand Up @@ -842,11 +842,11 @@ public function processStmtNode(

foreach ($gatheredReturnStatements as $statement) {
if ($finalScope === null) {
$finalScope = $statement->getScope()->toMutatingScope();
$finalScope = $statement->getScope();
continue;
}

$finalScope = $finalScope->mergeWith($statement->getScope()->toMutatingScope());
$finalScope = $finalScope->mergeWith($statement->getScope());
}

if ($finalScope !== null) {
Expand All @@ -855,7 +855,7 @@ public function processStmtNode(

}
}
if (!$scope->getClassReflection()->isAnonymous() && !$scope->isInAnonymousFunction()) {
if ($scope->getClassReflection() !== null && !$scope->getClassReflection()->isAnonymous() && !$scope->isInAnonymousFunction()) {
$this->processPendingFibers($storage);
}
} elseif ($stmt instanceof Echo_) {
Expand Down Expand Up @@ -2763,7 +2763,7 @@ public function processClosureNode(
return;
}

$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
$gatheredReturnStatements[] = new ReturnStatement($scope->toMutatingScope(), $node);
};

if (count($byRefUses) === 0) {
Expand Down Expand Up @@ -3142,7 +3142,7 @@ private function processPropertyHooks(
return;
}

$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
$gatheredReturnStatements[] = new ReturnStatement($scope->toMutatingScope(), $node);
}, StatementContext::createTopLevel())->toPublic();

$this->callNodeCallback($nodeCallback, new PropertyHookReturnStatementsNode(
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/StatementExitPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class StatementExitPoint
{

public function __construct(private Stmt $statement, private Scope $scope)
public function __construct(private Stmt $statement, private MutatingScope $scope)
{
}

Expand All @@ -19,7 +19,7 @@ public function getStatement(): Stmt
return $this->statement;
}

public function getScope(): Scope
public function getScope(): MutatingScope
{
return $this->scope;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/StatementResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class StatementResult
* @param EndStatementResult[] $endStatements
*/
public function __construct(
private Scope $scope,
private MutatingScope $scope,
private bool $hasYield,
private bool $isAlwaysTerminating,
private array $exitPoints,
Expand All @@ -29,7 +29,7 @@ public function __construct(
{
}

public function getScope(): Scope
public function getScope(): MutatingScope
{
return $this->scope;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Node/ReturnStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\MutatingScope;

/**
* @api
Expand All @@ -14,12 +14,12 @@ final class ReturnStatement

private Node\Stmt\Return_ $returnNode;

public function __construct(private Scope $scope, Return_ $returnNode)
public function __construct(private MutatingScope $scope, Return_ $returnNode)
{
$this->returnNode = $returnNode;
}

public function getScope(): Scope
public function getScope(): MutatingScope
{
return $this->scope;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14001.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace Bug14001;

use PHPStan\Analyser\StatementResult;
use PHPStan\Node\ExecutionEndNode;
use function PHPStan\Testing\assertType;

class Foo
{
/**
* @param list<ExecutionEndNode> $executionEnds
*/
public function testWithEarlyTermination(array $executionEnds): void
{
$finalScope = null;
foreach ($executionEnds as $executionEnd) {
$statementResult = $executionEnd->getStatementResult();
if ($statementResult->isAlwaysTerminating()) {
continue;
}
if ($finalScope === null) {
$finalScope = $statementResult->getScope();
continue;
}

$finalScope = $finalScope->mergeWith($statementResult->getScope());
}
assertType('PHPStan\Analyser\MutatingScope|null', $finalScope);
}

/**
* @param list<ExecutionEndNode> $executionEnds
*/
public function testWithoutEarlyTermination(array $executionEnds): void
{
$finalScope = null;
foreach ($executionEnds as $executionEnd) {
$endScope = $executionEnd->getStatementResult()->getScope();
if ($finalScope === null) {
$finalScope = $endScope;
continue;
}

$finalScope = $finalScope->mergeWith($endScope);
}
assertType('PHPStan\Analyser\MutatingScope|null', $finalScope);
}
}
Loading