-
Notifications
You must be signed in to change notification settings - Fork 530
Add Type::flip()
#1832
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
Add Type::flip()
#1832
Conversation
A name that makes sense in English would be best. So BTW I want to rename getIterableCount to getArraySize. Makes more sense to me 😊 |
makes sense. I was also unsure about naming there and found iterable-related things I just kept using. maybe the maybe- and non- implementations should then be moved from the iterable traits to the array traits. not sure if counting iterables makes much sense anyways 🤔 in that case it doesn't even change anything. I'll open a PR in a bit 😊 |
I want to add another test case |
now it's ok |
@@ -139,7 +139,7 @@ public function unsetOffset(Type $offsetType): Type | |||
|
|||
public function flipArray(): Type | |||
{ | |||
return $this; | |||
return new MixedType(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be more precise, like ArrayType($valueType, 0+$positiveInt)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that shouldn't be necessary because this type should come intersected with an array and intersecting with mixed then just removes it basically. the simpler the better, I think, but I'm also not sure and @rvanvelzen just recently improved performance by not being too precise in type specification. maybe he can confirm/explain it better :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it passes a non-constant-list test like
/**
* @param list<string> $array
*/
function foo7($array)
{
$flip = array_flip($array);
assertType('array<string int<0, max>>', $flip);
}
we are good to go :)
It depends... For example |
Beautiful, thank you :) |
This would be the first new array modification function moved to Type. Most likely one of the simplest :)
As you can see and know anyways: it is a very specific thing, but it allows us to get rid of conditional code and
getConstantArrays()
calls, that I don't like much and are also messy with intersections/unions.What about the name? There will be more to come, e.g.
pop
,shift
, ... Does a common prefix likearray
for all of them make sense maybe? I'd definitely group them together everywhere, my worry is just that all of them are only meant for arrays and some could suggest that e.g. strings can be flipped or something like that.