Skip to content

Commit

Permalink
fix inference with smaller/smaller equal on recursive count()
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 8, 2023
1 parent ee52ac4 commit 39554a3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function specifyTypesInCondition(

if (
$expr->left instanceof FuncCall
&& count($expr->left->getArgs()) === 1
&& count($expr->left->getArgs()) >= 1
&& $expr->left->name instanceof Name
&& in_array(strtolower((string) $expr->left->name), ['count', 'sizeof', 'strlen'], true)
&& (
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/data/minmax-arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,24 @@ public function unionType(): void
assertType('9', max([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
}
}

/**
* @param int[] $ints
*/
function countRecursive(array $ints): void
{
if (count($ints, COUNT_RECURSIVE) <= 0) {
assertType('false', min($ints));
assertType('false', max($ints));
} else {
assertType('int', min($ints));
assertType('int', max($ints));
}
if (count($ints, COUNT_RECURSIVE) < 1) {
assertType('false', min($ints));
assertType('false', max($ints));
} else {
assertType('int', min($ints));
assertType('int', max($ints));
}
}
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/data/minmax-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,24 @@ public function unionType(): void
assertType('9', max([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
}
}

/**
* @param int[] $ints
*/
function countRecursive(array $ints): void
{
if (count($ints, COUNT_RECURSIVE) < 1) {
assertType('*ERROR*', min($ints));
assertType('*ERROR*', max($ints));
} else {
assertType('int', min($ints));
assertType('int', max($ints));
}
if (count($ints, COUNT_RECURSIVE) < 2) {
assertType('int', min($ints));
assertType('int', max($ints));
} else {
assertType('int', min($ints));
assertType('int', max($ints));
}
}

0 comments on commit 39554a3

Please sign in to comment.