Skip to content

Array without 0 is not a list #4018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 1.12.x
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ public function isList(): TrinaryLogic
return TrinaryLogic::createNo();
}

if ($this->getKeyType()->isSuperTypeOf(new ConstantIntegerType(0))->no()) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::createMaybe();
}

Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3262,6 +3262,22 @@ public function testBug9009(): void
$this->analyse([__DIR__ . '/data/bug-9009.php'], []);
}

public function testBug9487(): void
{
$this->checkThisOnly = false;
$this->checkNullables = false;
$this->checkUnionTypes = false;
$this->checkExplicitMixed = false;

$this->analyse([__DIR__ . '/data/bug-9487.php'], [
[
'Parameter #1 $x of method Bug9487\HelloWorld::sayHello() expects list<string>, array<int<1, max>, string> given.',
15,
'array<int<1, max>, string> is not a list.',
],
]);
}

public function testBuSplObjectStorageRemove(): void
{
$this->checkThisOnly = false;
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-9487.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types = 1);

namespace Bug9487;

class HelloWorld
{
/** @param list<string> $x */
public function sayHello($x): void
{
}

/** @param array<positive-int, string> $x */
public function invoke($x): void
{
$this->sayHello($x);
}
}
Loading