From 39554a30e7f82a18a8b7bab6769e360e153245f8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 8 Dec 2023 15:37:30 +0100 Subject: [PATCH] fix inference with smaller/smaller equal on recursive count() --- src/Analyser/TypeSpecifier.php | 2 +- tests/PHPStan/Analyser/data/minmax-arrays.php | 21 +++++++++++++++++++ tests/PHPStan/Analyser/data/minmax-php8.php | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index a453bea1add..8e0a6bdae88 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -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) && ( diff --git a/tests/PHPStan/Analyser/data/minmax-arrays.php b/tests/PHPStan/Analyser/data/minmax-arrays.php index 1a68d50b56c..faeb20f9485 100644 --- a/tests/PHPStan/Analyser/data/minmax-arrays.php +++ b/tests/PHPStan/Analyser/data/minmax-arrays.php @@ -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)); + } +} diff --git a/tests/PHPStan/Analyser/data/minmax-php8.php b/tests/PHPStan/Analyser/data/minmax-php8.php index 3d738d4c389..d00f5877abc 100644 --- a/tests/PHPStan/Analyser/data/minmax-php8.php +++ b/tests/PHPStan/Analyser/data/minmax-php8.php @@ -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)); + } +}