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

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jun 1, 2024

refs phpstan/phpstan#11111


the also issue mentions

We could also check other invalid accesses:

Reading access: var_dump($array[Language::GER]); --> Cannot access offset of type Language on array
Write: $array[Language::ITA] = 'foobar'; --> Cannot access offset of type Language on array
Unset: unset($array[Language::GER]); --> Cannot unset offset of type Language on array

which makes sense, but a separate problem for a separate PR IMO

@@ -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

@ondrejmirtes
Copy link
Member

It's not okay to change the functionMap for this requirement. This needs a custom rule.

Some types outside of int|string can be cast to array key.

@ondrejmirtes
Copy link
Member

As for:

We could also check other invalid accesses:

I believe PHPStan already does all of that but if it does not, open a feature request.

@staabm
Copy link
Contributor Author

staabm commented Jun 1, 2024

I believe PHPStan already does all of that but if it does not, open a feature request.

https://phpstan.org/r/a2c1cff4-0464-4021-bb32-04680554d9bf

you are right, it works already

@staabm staabm closed this Jun 1, 2024
@staabm staabm deleted the arr-fill branch June 1, 2024 14:49
@staabm
Copy link
Contributor Author

staabm commented Jun 4, 2024

It's not okay to change the functionMap for this requirement. This needs a custom rule.

Some types outside of int|string can be cast to array key.

would it make sense to create a new "virtual type", like we have with __stringandstringable ?

case '__stringandstringable':
return new StringAlwaysAcceptingObjectWithToStringType();

@ondrejmirtes
Copy link
Member

I'd say creating a new custom rule is easier than modifying the type system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants