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

Add AssocValue flip and swap functions #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

bronek89
Copy link
Member

No description provided.

@bronek89 bronek89 requested review from dazet and dlakomski June 21, 2022 18:20
*/
public function flip(): IterableValue
{
return $this->toIterableValue()->flip();
Copy link
Member

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.

Copy link
Member Author

@bronek89 bronek89 Jun 21, 2022

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>

@bronek89 bronek89 requested review from dlakomski and dazet and removed request for dazet September 30, 2023 19:19
}

/**
* @phpstan-return AssocValue<int|string, TKey>
Copy link
Member

@dazet dazet Oct 1, 2023

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;
Copy link
Member

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)
Copy link
Member

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>>
Copy link
Member

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']

Copy link
Member

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 😏

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.

3 participants