Skip to content

array_fill_keys expects array of array-key#3110

Closed
staabm wants to merge 1 commit intophpstan:1.11.xfrom
staabm:arr-fill
Closed

array_fill_keys expects array of array-key#3110
staabm wants to merge 1 commit intophpstan:1.11.xfrom
staabm:arr-fill

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

'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