Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public function getArraySize(): Type
return new ErrorType();
}

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

public function getIterableKeyType(): Type
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-7607.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function blank($value)
return false;
}

if ($value instanceof Countable) {
if ($value instanceof \Countable) {
return count($value) === 0;
}

Expand Down
24 changes: 23 additions & 1 deletion tests/PHPStan/Analyser/nsrt/countable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,31 @@ static public function doFoo() {
}
}

class Bar implements \Countable {

/**
* @return -1
*/
public function count() : int {
return -1;
}

static public function doBar() {
$bar = new Bar();
assertType('-1', $bar->count());
}
}

class NonCountable {}

function doNonCountable() {
assertType('*ERROR*', count(new NonCountable()));
}

function doFoo() {
assertType('int<0, max>', count(new Foo()));
assertType('*ERROR*', count(new NonCountable()));
}

function doBar() {
assertType('-1', count(new Bar()));
}
Loading