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

Improve return types for array_fill_keys and array_combine #1139

Merged

Conversation

canvural
Copy link
Contributor

This PR improves return types for array_fill_keys and array_combine functions. This two functions are kinda special, because they try to cast the key types to string. So true becomes 1, an object that has __toString becomes string etc. This PR adds that.

@canvural canvural force-pushed the improve-array-fill-keys-combine branch from 7542a89 to 572960f Compare March 29, 2022 09:48
@canvural canvural force-pushed the improve-array-fill-keys-combine branch from 572960f to af04640 Compare March 29, 2022 09:53
@canvural canvural force-pushed the improve-array-fill-keys-combine branch 5 times, most recently from f65641a to 419d8ef Compare March 29, 2022 11:15
@@ -65,8 +68,24 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}
}

if ($keysParamType instanceof ArrayType) {
Copy link
Member

Choose a reason for hiding this comment

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

With instanceof ArrayType condition, you're excluding:

  • non-empty-array (that's IntersectionType)
  • Unions of array shapes

Better solution is $keysParamType->isArray()->yes(). Instead of ->getItemType(), you should use ->getIterableValueType().

Feel free to fix other instances in this extension too - I see $valuesParamType instanceof ArrayType as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated and added tests.

@canvural canvural force-pushed the improve-array-fill-keys-combine branch from 419d8ef to 87f4058 Compare March 29, 2022 17:12
@canvural canvural force-pushed the improve-array-fill-keys-combine branch from 87f4058 to ecd0dcb Compare March 29, 2022 17:15
@ondrejmirtes ondrejmirtes merged commit 3bd1bac into phpstan:1.5.x Mar 29, 2022
@ondrejmirtes
Copy link
Member

Thank you!

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