From c4a2e7bb095ee95c8b6421f038c793879fa5462f Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 25 May 2025 00:37:48 +0200 Subject: [PATCH 1/2] Array without 0 is not a list --- src/Type/ArrayType.php | 4 ++++ .../Rules/Methods/CallMethodsRuleTest.php | 16 ++++++++++++++++ tests/PHPStan/Rules/Methods/data/bug-9487.php | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-9487.php diff --git a/src/Type/ArrayType.php b/src/Type/ArrayType.php index 38d1e6741b..b6a16d4c18 100644 --- a/src/Type/ArrayType.php +++ b/src/Type/ArrayType.php @@ -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(); } diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 726542e93d..f26592f9d9 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -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, array, string> given.', + 15, + 'array, string> is not a list.' + ], + ]); + } + public function testBuSplObjectStorageRemove(): void { $this->checkThisOnly = false; diff --git a/tests/PHPStan/Rules/Methods/data/bug-9487.php b/tests/PHPStan/Rules/Methods/data/bug-9487.php new file mode 100644 index 0000000000..6bcf34c49d --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-9487.php @@ -0,0 +1,17 @@ + $x */ + public function sayHello($x): void + { + } + + /** @param array $x */ + public function invoke($x): void + { + $this->sayHello($x); + } +} From ddced9ad98deb3331a4c2fdd97060a4bd34f1b4e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 25 May 2025 12:17:01 +0200 Subject: [PATCH 2/2] Fix --- tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index f26592f9d9..2e6b5c084d 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3273,7 +3273,7 @@ public function testBug9487(): void [ 'Parameter #1 $x of method Bug9487\HelloWorld::sayHello() expects list, array, string> given.', 15, - 'array, string> is not a list.' + 'array, string> is not a list.', ], ]); }