From 13b6747fee78e5208e08885c2afccd0572fa0563 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 19 Sep 2024 16:51:51 +0200 Subject: [PATCH 1/2] A list should only accept lists --- src/Type/Constant/ConstantArrayType.php | 4 ++++ .../Rules/Generators/YieldFromTypeRuleTest.php | 1 - .../Methods/CallStaticMethodsRuleTest.php | 11 +++++++++++ tests/PHPStan/Rules/Methods/data/bug-11600.php | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Methods/data/bug-11600.php diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 7d2d0c9bc1..75441e5216 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -303,6 +303,10 @@ public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult return AcceptsResult::createFromBoolean(count($type->keyTypes) === 0); } + if ($this->isList()->yes() && $type->isList()->no()) { + return AcceptsResult::createNo(); + } + $result = AcceptsResult::createYes(); foreach ($this->keyTypes as $i => $keyType) { $valueType = $this->valueTypes[$i]; diff --git a/tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php b/tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php index 681d9abab1..23582b17ef 100644 --- a/tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php +++ b/tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php @@ -39,7 +39,6 @@ public function testRule(): void [ 'Generator expects value type array{DateTime, DateTime, stdClass, DateTimeImmutable}, array{0: DateTime, 1: DateTime, 2: stdClass, 4: DateTimeImmutable} given.', 74, - 'Array does not have offset 3.', ], [ 'Result of yield from (void) is used.', diff --git a/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php index 27718dc9c7..9bceb666a9 100644 --- a/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php @@ -841,4 +841,15 @@ public function testClosureBind(): void ]); } + public function testBug11600(): void + { + $this->checkThisOnly = false; + $this->analyse([__DIR__ . '/data/bug-11600.php'], [ + [ + 'Parameter #1 $array of static method Bug10600\HelloWorld::sayHello() expects array{1, 2}, array{1: 2, 0: 1} given.', + 18, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-11600.php b/tests/PHPStan/Rules/Methods/data/bug-11600.php new file mode 100644 index 0000000000..b398820a82 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-11600.php @@ -0,0 +1,18 @@ + 1, 1 => 2]; +$b = [1 => 2, 0 => 1]; + +HelloWorld::sayHello($a); +HelloWorld::sayHello($b); From 70f4d0298d37004a512875a01d6b580542d5859c Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 19 Sep 2024 16:56:14 +0200 Subject: [PATCH 2/2] Fix SA --- .../Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php index 16529f3a74..14f2482883 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php @@ -1017,7 +1017,7 @@ static function (array $i): array { unset($i[2]); } - return $i; + return array_values($i); }, self::getLooseComparisonAgainsEnumsIssues(), );