Skip to content

Commit

Permalink
Narrow to non-empty-string/non-falsey-string after mb_strlen()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed May 3, 2024
1 parent 4d162fa commit 3361c84
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -210,11 +210,11 @@ public function specifyTypesInCondition(
$expr->left instanceof FuncCall
&& count($expr->left->getArgs()) === 1
&& $expr->left->name instanceof Name
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen'], true)
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen', 'mb_strlen'], true)
&& (
!$expr->right instanceof FuncCall
|| !$expr->right->name instanceof Name
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen'], true)
|| !in_array(strtolower((string) $expr->right->name), ['count', 'sizeof', 'strlen', 'mb_strlen'], true)
)
) {
$inverseOperator = $expr instanceof Node\Expr\BinaryOp\Smaller
Expand Down Expand Up @@ -265,7 +265,7 @@ public function specifyTypesInCondition(
&& $expr->right instanceof FuncCall
&& count($expr->right->getArgs()) === 1
&& $expr->right->name instanceof Name
&& strtolower((string) $expr->right->name) === 'strlen'
&& in_array(strtolower((string) $expr->right->name), ['strlen', 'mb_strlen'], true)
&& $leftType->isInteger()->yes()
) {
if (
Expand Down Expand Up @@ -977,7 +977,7 @@ private function specifyTypesForConstantBinaryExpression(
&& $exprNode instanceof FuncCall
&& count($exprNode->getArgs()) === 1
&& $exprNode->name instanceof Name
&& strtolower((string) $exprNode->name) === 'strlen'
&& in_array(strtolower((string) $exprNode->name), ['strlen', 'mb_strlen'], true)
&& $constantType instanceof ConstantIntegerType
) {
if ($context->truthy() || $constantType->getValue() === 0) {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -1476,6 +1476,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10187.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10834.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10952b.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10893.php');
}

Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/data/bug-10952b.php
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace Bug10952b;

use function PHPStan\Testing\assertType;

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

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

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

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

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

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

0 comments on commit 3361c84

Please sign in to comment.