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
13 changes: 10 additions & 3 deletions src/Analyser/ExprHandler/MatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
ExpressionContext::createTopLevel(),
);
$armScope = $armResult->getScope();
$armBodyScopes[] = $armScope;
if (!$matchArmBodyScope->getType($arm->body) instanceof NeverType) {
$armBodyScopes[] = $armScope;
}
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
Expand Down Expand Up @@ -367,12 +369,15 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$hasDefaultCond = true;
$matchArmBody = new MatchExpressionArmBody($matchScope, $arm->body);
$armNodes[$i] = new MatchExpressionArm($matchArmBody, [], $arm->getStartLine());
$armBodyType = $matchScope->getType($arm->body);
$armResult = $nodeScopeResolver->processExprNode($stmt, $arm->body, $matchScope, $storage, $nodeCallback, ExpressionContext::createTopLevel());
$matchScope = $armResult->getScope();
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
$armBodyScopes[] = $matchScope;
if (!$armBodyType instanceof NeverType) {
$armBodyScopes[] = $matchScope;
}
continue;
}

Expand Down Expand Up @@ -423,7 +428,9 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
ExpressionContext::createTopLevel(),
);
$armScope = $armResult->getScope();
$armBodyScopes[] = $armScope;
if (!$bodyScope->getType($arm->body) instanceof NeverType) {
$armBodyScopes[] = $armScope;
}
$hasYield = $hasYield || $armResult->hasYield();
$throwPoints = array_merge($throwPoints, $armResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $armResult->getImpurePoints());
Expand Down
10 changes: 10 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,16 @@ public function processStmtNode(
foreach ($stmt->loop as $loopExpr) {
$loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $storage, $nodeCallback, ExpressionContext::createTopLevel())->getScope();
}

if (
!$alwaysIterates->yes()
&& $context->isTopLevel()
&& $lastCondExpr !== null
&& ($this->treatPhpDocTypesAsCertain ? $loopScope->getType($lastCondExpr) : $loopScope->getNativeType($lastCondExpr))->toBoolean()->isTrue()->yes()
) {
$alwaysIterates = TrinaryLogic::createYes();
}

$finalScope = $finalScope->generalizeWith($loopScope);

if ($lastCondExpr !== null) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Missing/MissingReturnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,11 @@ public function testBug12722(): void
$this->analyse([__DIR__ . '/data/bug-12722.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug9444(): void
{
$this->checkExplicitMixedMissingReturn = true;
$this->analyse([__DIR__ . '/data/bug-9444.php'], []);
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Missing/data/bug-9444.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php // lint >= 8.0

namespace Bug9444;

class One
{
private static int $i = 0;

public static function run(): string
{
self::$i++;
if (self::$i <= 3) {
throw new \Exception('One:run');
}

return 'Ok';
}
}

class Main
{
public function process(): string
{
for ($i = 0; $i <= 5; $i++) {
try {
return One::run();
} catch (\Throwable $e) {
$sleep = match($i) {
0 => 0.5,
1 => 1,
2 => 3,
3 => 6,
4 => 9,
default => throw $e,
};

echo $sleep . PHP_EOL;
}
}
}
}
Loading