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
3 changes: 2 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,8 @@ private function processStmtNode(
$alwaysIterates = $condBooleanType->isTrue()->yes();
}

$this->callNodeCallback($nodeCallback, new DoWhileLoopConditionNode($stmt->cond, $bodyScopeResult->toPublic()->getExitPoints()), $bodyScope, $storage);
$publicResult = $bodyScopeResult->toPublic();
$this->callNodeCallback($nodeCallback, new DoWhileLoopConditionNode($stmt->cond, $publicResult->getExitPoints(), $publicResult->getThrowPoints()), $bodyScope, $storage);

if ($alwaysIterates) {
$alwaysTerminating = count($bodyScopeResult->getExitPointsByType(Break_::class)) === 0;
Expand Down
12 changes: 11 additions & 1 deletion src/Node/DoWhileLoopConditionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
use PhpParser\Node\Expr;
use PhpParser\NodeAbstract;
use PHPStan\Analyser\StatementExitPoint;
use PHPStan\Analyser\ThrowPoint;

final class DoWhileLoopConditionNode extends NodeAbstract implements VirtualNode
{

/**
* @param StatementExitPoint[] $exitPoints
* @param ThrowPoint[] $throwPoints
*/
public function __construct(private Expr $cond, private array $exitPoints)
public function __construct(private Expr $cond, private array $exitPoints, private array $throwPoints)
{
parent::__construct($cond->getAttributes());
}
Expand All @@ -31,6 +33,14 @@ public function getExitPoints(): array
return $this->exitPoints;
}

/**
* @return ThrowPoint[]
*/
public function getThrowPoints(): array
{
return $this->throwPoints;
}

#[Override]
public function getType(): string
{
Expand Down
5 changes: 5 additions & 0 deletions src/Rules/Comparison/DoWhileLoopConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function processNode(Node $node, Scope $scope): array
return [];
}
}
foreach ($node->getThrowPoints() as $throwPoint) {
if ($throwPoint->isExplicit()) {
return [];
}
}
} else {
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ protected function getRule(): Rule
);
}

public function testBug5865(): void
{
$this->analyse([__DIR__ . '/data/bug-5865.php'], []);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/do-while-loop.php'], [
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-5865.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace Bug5865;

class HelloWorld
{
public function sayHello(): void
{
try {
do {
$this->foo();
} while (true);
} catch (\RuntimeException $e) {
// ok
}
}

/**
* @throws \RuntimeException
*/
public function foo(): void
{
throw new \RuntimeException();
}
}
Loading