Skip to content

Commit 42eac28

Browse files
vojtech-dobesondrejmirtes
authored andcommitted
Use count() return type in ObjectType::getArraySize()
1 parent c0f2e9e commit 42eac28

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Type/ObjectType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ public function getArraySize(): Type
946946
return new ErrorType();
947947
}
948948

949-
return IntegerRangeType::fromInterval(0, null);
949+
return RecursionGuard::run($this, fn (): Type => $this->getMethod('count', new OutOfClassScope())->getOnlyVariant()->getReturnType());
950950
}
951951

952952
public function getIterableKeyType(): Type

tests/PHPStan/Analyser/nsrt/bug-7607.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function blank($value)
3030
return false;
3131
}
3232

33-
if ($value instanceof Countable) {
33+
if ($value instanceof \Countable) {
3434
return count($value) === 0;
3535
}
3636

tests/PHPStan/Analyser/nsrt/countable.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,31 @@ static public function doFoo() {
1515
}
1616
}
1717

18+
class Bar implements \Countable {
19+
20+
/**
21+
* @return -1
22+
*/
23+
public function count() : int {
24+
return -1;
25+
}
26+
27+
static public function doBar() {
28+
$bar = new Bar();
29+
assertType('-1', $bar->count());
30+
}
31+
}
32+
1833
class NonCountable {}
1934

35+
function doNonCountable() {
36+
assertType('*ERROR*', count(new NonCountable()));
37+
}
38+
2039
function doFoo() {
2140
assertType('int<0, max>', count(new Foo()));
22-
assertType('*ERROR*', count(new NonCountable()));
41+
}
42+
43+
function doBar() {
44+
assertType('-1', count(new Bar()));
2345
}

0 commit comments

Comments
 (0)