From 27e1df4deaf1156bc957747039c26e66f630bc7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Dobe=C5=A1?= Date: Tue, 1 Jul 2025 01:05:46 +0200 Subject: [PATCH] Resolve ObjectType::getArraySize() with count() only if defined --- src/Type/ObjectType.php | 4 ++++ tests/PHPStan/Analyser/nsrt/countable.php | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 05705fe683..04441d0e03 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -946,6 +946,10 @@ public function getArraySize(): Type return new ErrorType(); } + if ($this->hasMethod('count')->yes() === false) { + return IntegerRangeType::fromInterval(0, null); + } + return RecursionGuard::run($this, fn (): Type => $this->getMethod('count', new OutOfClassScope())->getOnlyVariant()->getReturnType()); } diff --git a/tests/PHPStan/Analyser/nsrt/countable.php b/tests/PHPStan/Analyser/nsrt/countable.php index d2cafc3d4e..1399228135 100644 --- a/tests/PHPStan/Analyser/nsrt/countable.php +++ b/tests/PHPStan/Analyser/nsrt/countable.php @@ -30,6 +30,9 @@ static public function doBar() { } } +interface Baz { +} + class NonCountable {} function doNonCountable() { @@ -43,3 +46,7 @@ function doFoo() { function doBar() { assertType('-1', count(new Bar())); } + +function doBaz(Baz $baz) { + assertType('int<0, max>', count($baz)); +}