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)); +}