Skip to content

Commit 599ac2b

Browse files
vojtech-dobesondrejmirtes
authored andcommitted
Resolve ObjectType::getArraySize() with count() only if defined
1 parent 42eac28 commit 599ac2b

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Type/ObjectType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ public function getArraySize(): Type
946946
return new ErrorType();
947947
}
948948

949+
if ($this->hasMethod('count')->yes() === false) {
950+
return IntegerRangeType::fromInterval(0, null);
951+
}
952+
949953
return RecursionGuard::run($this, fn (): Type => $this->getMethod('count', new OutOfClassScope())->getOnlyVariant()->getReturnType());
950954
}
951955

tests/PHPStan/Analyser/nsrt/countable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ static public function doBar() {
3030
}
3131
}
3232

33+
interface Baz {
34+
}
35+
3336
class NonCountable {}
3437

3538
function doNonCountable() {
@@ -43,3 +46,7 @@ function doFoo() {
4346
function doBar() {
4447
assertType('-1', count(new Bar()));
4548
}
49+
50+
function doBaz(Baz $baz) {
51+
assertType('int<0, max>', count($baz));
52+
}

0 commit comments

Comments
 (0)