Skip to content

Commit

Permalink
Fix strlen($x) > $n === true negation
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 8, 2024
1 parent 4e5020d commit cab49eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -269,8 +269,8 @@ public function specifyTypesInCondition(
&& $leftType->isInteger()->yes()
) {
if (
$context->truthy() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
|| ($context->falsey() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
$context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
|| ($context->false() && (new ConstantIntegerType(1 - $offset))->isSuperTypeOf($leftType)->yes())
) {
$argType = $scope->getType($expr->right->getArgs()[0]->value);
if ($argType->isString()->yes()) {
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10952c.php
@@ -0,0 +1,30 @@
<?php declare(strict_types = 1);

namespace Bug10952c;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function getString(): string
{
return 'hallo';
}

public function test(): void
{
$string = $this->getString();

if ((strlen($string) > 1) === true) {
assertType('non-empty-string', $string);
} else {
assertType("string", $string);
}

match (true) {
(strlen($string) > 1) => assertType('non-empty-string', $string),
default => assertType("string", $string),
};

}
}

0 comments on commit cab49eb

Please sign in to comment.