Skip to content
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

array_fill_keys expects array of array-key #3110

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 2 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
'array_change_key_case' => ['array', 'input'=>'array', 'case='=>'int'],
'array_chunk' => ['list<array>', 'input'=>'array', 'size'=>'positive-int', 'preserve_keys='=>'bool'],
'array_column' => ['array', 'array'=>'array', 'column_key'=>'mixed', 'index_key='=>'mixed'],
'array_combine' => ['array|false', 'keys'=>'array', 'values'=>'array'],
'array_combine' => ['array|false', 'keys'=>'array<array-key>', 'values'=>'array'],
Copy link
Contributor Author

@staabm staabm Jun 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while here I used the same fix here - I think array-key better conveys the idea of the type beeing required

this change is here just for consistency with array_fill_keys

'array_count_values' => ['array<positive-int>', 'input'=>'array'],
'array_diff' => ['array', 'arr1'=>'array', 'arr2'=>'array', '...args='=>'array'],
'array_diff_assoc' => ['array', 'arr1'=>'array', 'arr2'=>'array', '...args='=>'array'],
Expand All @@ -271,7 +271,7 @@
'array_diff_ukey' => ['array', 'arr1'=>'array', 'arr2'=>'array', 'key_comp_func'=>'callable(mixed,mixed):int'],
'array_diff_ukey\'1' => ['array', 'arr1'=>'array', 'arr2'=>'array', 'arr3'=>'array', 'arg4'=>'array|callable(mixed,mixed):int', '...rest='=>'array|callable(mixed,mixed):int'],
'array_fill' => ['array', 'start_key'=>'int', 'num'=>'int', 'val'=>'mixed'],
'array_fill_keys' => ['array', 'keys'=>'array', 'val'=>'mixed'],
'array_fill_keys' => ['array', 'keys'=>'array<array-key>', 'val'=>'mixed'],
staabm marked this conversation as resolved.
Show resolved Hide resolved
'array_filter' => ['array', 'input'=>'array', 'callback='=>'callable(mixed,mixed):bool|callable(mixed):bool', 'flag='=>'int'],
'array_flip' => ['array', 'input'=>'array<int|string>'],
'array_intersect' => ['array', 'arr1'=>'array', 'arr2'=>'array', '...args='=>'array'],
Expand Down
4 changes: 2 additions & 2 deletions resources/functionMap_php80delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
return [
'new' => [
'array_combine' => ['associative-array', 'keys'=>'string[]|int[]', 'values'=>'array'],
'array_combine' => ['associative-array', 'keys'=>'array<array-key>', 'values'=>'array'],
'base64_decode' => ['string', 'string'=>'string', 'strict='=>'false'],
'base64_decode\'1' => ['string|false', 'string'=>'string', 'strict='=>'true'],
'bcdiv' => ['string', 'dividend'=>'string', 'divisor'=>'string', 'scale='=>'int'],
Expand Down Expand Up @@ -161,7 +161,7 @@
'xmlwriter_write_raw' => ['bool', 'xmlwriter'=>'XMLWriter', 'content'=>'string'],
],
'old' => [
'array_combine' => ['associative-array|false', 'keys'=>'string[]|int[]', 'values'=>'array'],
'array_combine' => ['associative-array|false', 'keys'=>'array<array-key>', 'values'=>'array'],
'bcdiv' => ['?string', 'dividend'=>'string', 'divisor'=>'string', 'scale='=>'int'],
'bcmod' => ['?string', 'dividend'=>'string', 'divisor'=>'string', 'scale='=>'int'],
'bcpowmod' => ['?string', 'base'=>'string', 'exponent'=>'string', 'modulus'=>'string', 'scale='=>'int'],
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1687,4 +1687,22 @@ public function testBug10974(): void
$this->analyse([__DIR__ . '/data/bug-10974.php'], []);
}

public function testBug11111(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-11111.php'], [
[
'Parameter #1 $keys of function array_fill_keys expects array<(int|string)>, array<int, Bug11111\Language::DAN|Bug11111\Language::ENG|Bug11111\Language::GER> given.',
24,
],
[
'Parameter #1 $keys of function array_combine expects array<(int|string)>, array<int, Bug11111\Language::DAN|Bug11111\Language::ENG|Bug11111\Language::GER> given.',
28,
],
]);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11111.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Bug11111;

enum Language: string
{
case ENG = 'eng';
case FRE = 'fre';
case GER = 'ger';
case ITA = 'ita';
case SPA = 'spa';
case DUT = 'dut';
case DAN = 'dan';
}

function doFoo() {
$langs = [
Language::ENG,
Language::GER,
Language::DAN,
];

$array = array_fill_keys(
$langs,
null
);
$combined = array_combine(
$langs,
$langs
);
}

Loading