Skip to content
Merged
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/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ public function specifyTypesInCondition(
$argType = $scope->getType($expr->right->getArgs()[0]->value);
if ($argType->isString()->yes()) {
$accessory = new AccessoryNonEmptyStringType();
if ($leftType instanceof ConstantIntegerType && $leftType->getValue() >= 2) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old code missed to handle $offset which turns into off-by-one errors on smaller/greater comparisons


if (IntegerRangeType::createAllGreaterThanOrEqualTo(2 - $offset)->isSuperTypeOf($leftType)->yes()) {
$accessory = new AccessoryNonFalsyStringType();
}

Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-10952b.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public function test(): void
$string = $this->getString();

if (1 < mb_strlen($string)) {
assertType('non-empty-string', $string);
assertType('non-falsy-string', $string);
} else {
assertType("string", $string);
}

if (mb_strlen($string) > 1) {
assertType('non-empty-string', $string);
assertType('non-falsy-string', $string);
} else {
assertType("string", $string);
}
Expand Down
54 changes: 54 additions & 0 deletions tests/PHPStan/Analyser/nsrt/strlen-int-range.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php // lint >= 7.2

namespace StrlenIntRange;

use function PHPStan\Testing\assertType;

/**
* @param int<0, 3> $zeroToThree
* @param int<2, 3> $twoOrThree
* @param int<2, max> $twoOrMore
* @param int<min, 3> $maxThree
* @param int<10, 11> $tenOrEleven
*/
function doFoo(string $s, $zeroToThree, $twoOrThree, $twoOrMore, int $maxThree, $tenOrEleven): void
{
if (strlen($s) >= $zeroToThree) {
assertType('string', $s);
}
if (strlen($s) > $zeroToThree) {
assertType('non-empty-string', $s);
}

if (strlen($s) >= $twoOrThree) {
assertType('non-falsy-string', $s);
}
if (strlen($s) > $twoOrThree) {
assertType('non-falsy-string', $s);
}

if (strlen($s) > $twoOrMore) {
assertType('non-falsy-string', $s);
}

$oneOrMore = $twoOrMore-1;
if (strlen($s) > $oneOrMore) {
assertType('non-falsy-string', $s);
}
if (strlen($s) >= $oneOrMore) {
assertType('non-empty-string', $s);
}
if (strlen($s) <= $oneOrMore) {
assertType('string', $s);
} else {
assertType('non-falsy-string', $s);
}

if (strlen($s) > $maxThree) {
assertType('string', $s);
}

if (strlen($s) > $tenOrEleven) {
assertType('non-falsy-string', $s);
}
}
Loading