-
Notifications
You must be signed in to change notification settings - Fork 5
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 AssocValue flip and swap functions #60
base: master
Are you sure you want to change the base?
Conversation
src/AssocArray.php
Outdated
*/ | ||
public function flip(): IterableValue | ||
{ | ||
return $this->toIterableValue()->flip(); |
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.
Why not through GW\Value\Associable\Flip
? I don't think that AssocArray
should return IterableValue
.
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.
It is hard to flip AssocValue, because TKey must be int|string and TValue is mixed - therefore it produces phpstan type errors. I think it can be fully fixed only after rewrite AssocValue to some kind of map with mixed key and mixed value.
For now I've changed it to:
/**
* @phpstan-return AssocValue<int|string, TKey>
*/
public function flip(): AssocValue;
it will be AssocValue
- not AssocValue<TValue,TKey>
but AssocValue<int|string, TKey>
in other words - you loose type for key - from Wrap::assoc([0 => 'xxx'])
as AssocValue<int,string>
flip makes AssocValue<int|string,int>
, not AssocValue<string,int>
} | ||
|
||
/** | ||
* @phpstan-return AssocValue<int|string, TKey> |
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.
It can't be AssocValue<TValue, TKey>
?
I know it doesn't make sense if TValue isn't int|string, but AssocValue<int, string>
flipped should be rather AssocValue<string, int>
than AssocValue<int|string, int>
* @param TKey $keyB | ||
* @phpstan-return AssocArray<TKey, TValue> | ||
*/ | ||
public function swap($keyA, $keyB): AssocArray; |
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.
It can have type-hint int|string
for arguments, and return should be AssocValue
* @param TKey $keyA | ||
* @param TKey $keyB | ||
*/ | ||
public function __construct(Associable $associable, $keyA, $keyB) |
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.
int|string
here too :)
@@ -148,7 +148,7 @@ public function positionLast($needle): ?int; | |||
|
|||
/** | |||
* @param string|StringValue $pattern | |||
* @return ArrayValue<array<int|string, string>> | |||
* @return ArrayValue<array<string>> |
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.
It can be array<int|string, string>
when there's named subpattern
(?P<name>lorem)
=> [0 => 'lorem', 'name' => 'lorem', 1 => 'lorem']
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 I has similar problem on my branch and resolved it like this 😏
No description provided.