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: 4 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ public function processStmtNode(
$originalStorage = $storage;
$storage = $originalStorage->duplicate();
$condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep());
$beforeCondBooleanType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean();
$beforeCondBooleanType = $scope->getType($stmt->cond)->toBoolean();
$condScope = $condResult->getFalseyScope();
if (!$context->isTopLevel() && $beforeCondBooleanType->isFalse()->yes()) {
if (!$this->polluteScopeWithLoopInitialAssignments) {
Expand Down Expand Up @@ -1493,7 +1493,7 @@ public function processStmtNode(
$alwaysIterates = false;
$neverIterates = false;
if ($context->isTopLevel()) {
$condBooleanType = ($this->treatPhpDocTypesAsCertain ? $bodyScopeMaybeRan->getType($stmt->cond) : $bodyScopeMaybeRan->getNativeType($stmt->cond))->toBoolean();
$condBooleanType = $bodyScopeMaybeRan->getType($stmt->cond)->toBoolean();
$alwaysIterates = $condBooleanType->isTrue()->yes();
$neverIterates = $condBooleanType->isFalse()->yes();
}
Expand Down Expand Up @@ -1591,7 +1591,7 @@ public function processStmtNode(

$alwaysIterates = false;
if ($context->isTopLevel()) {
$condBooleanType = ($this->treatPhpDocTypesAsCertain ? $bodyScope->getType($stmt->cond) : $bodyScope->getNativeType($stmt->cond))->toBoolean();
$condBooleanType = $bodyScope->getType($stmt->cond)->toBoolean();
$alwaysIterates = $condBooleanType->isTrue()->yes();
}

Expand Down Expand Up @@ -1662,7 +1662,7 @@ public function processStmtNode(
// only the last condition expression is relevant whether the loop continues
// see https://www.php.net/manual/en/control-structures.for.php
if ($condExpr === $lastCondExpr) {
$condTruthiness = ($this->treatPhpDocTypesAsCertain ? $condResultScope->getType($condExpr) : $condResultScope->getNativeType($condExpr))->toBoolean();
$condTruthiness = $condResultScope->getType($condExpr)->toBoolean();
$isIterableAtLeastOnce = $isIterableAtLeastOnce->and($condTruthiness->isTrue());
}

Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/Bug14522Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class Bug14522Test extends TypeInferenceTestCase
{

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-14522.php');
}

/**
* @param mixed ...$args
*/
#[DataProvider('dataFileAsserts')]
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/bug-14522.neon',
];
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/bug-14522.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
treatPhpDocTypesAsCertain: false
53 changes: 53 additions & 0 deletions tests/PHPStan/Analyser/data/bug-14522.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace Bug14522;

use function PHPStan\Testing\assertType;

/**
* @return int<1, max>
*/
function getBackoffTime(int $retryCount, int $maxBackoff): int
{
$retryCount = max(0, $retryCount);
$maxBackoff = max(1, $maxBackoff);

$total = 0;
for ($i = 0; $i <= $retryCount; ++$i) {
$total += min(2 ** $i, $maxBackoff);
}
assertType('int<1, max>', $total);
return $total;
}

/** @param int<0, max> $n */
function simpleForLoopAlwaysEnters(int $n): void
{
$total = 0;
for ($i = 0; $i <= $n; $i++) {
$total++;
}
assertType('int<1, max>', $total);
}

function forLoopWithMaxAlwaysEnters(int $n): void
{
$n = max(0, $n);
$total = 0;
for ($i = 0; $i <= $n; $i++) {
$total++;
}
assertType('int<1, max>', $total);
}

function whileLoopAlwaysEnters(int $n): void
{
$n = max(0, $n);
$i = 0;
$total = 0;
while ($i <= $n) {
$total++;
$i++;
}
assertType('int<1, max>', $total);
}
73 changes: 73 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14522.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types = 1);

namespace Bug14522;

use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertNativeType;

/**
* @return int<1, max>
*/
function getBackoffTime(int $retryCount, int $maxBackoff): int
{
$retryCount = max(0, $retryCount);
$maxBackoff = max(1, $maxBackoff);

$total = 0;
for ($i = 0; $i <= $retryCount; ++$i) {
$total += min(2 ** $i, $maxBackoff);
}
assertType('int<1, max>', $total);
return $total;
}

function simpleForLoopAlwaysEnters(int $n): void
{
$n = max(0, $n);
$total = 0;
for ($i = 0; $i <= $n; $i++) {
$total++;
}
assertType('int<1, max>', $total);
}

function forLoopNeverEnters(): void
{
$total = 0;
for ($i = 0; $i < 0; $i++) {
$total++;
}
assertType('0', $total);
}

function forLoopMaybeEnters(int $n): void
{
$total = 0;
for ($i = 0; $i < $n; $i++) {
$total++;
}
assertType('int<0, max>', $total);
}

function whileLoopAlwaysEnters(int $n): void
{
$n = max(0, $n);
$i = 0;
$total = 0;
while ($i <= $n) {
$total++;
$i++;
}
assertType('int<1, max>', $total);
}

function whileLoopMaybeEnters(int $n): void
{
$i = 0;
$total = 0;
while ($i < $n) {
$total++;
$i++;
}
assertType('int<0, max>', $total);
}
Loading