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

Map::ksort TypeError on null comparator #134

Open
ollieread opened this issue Feb 1, 2019 · 4 comments
Open

Map::ksort TypeError on null comparator #134

ollieread opened this issue Feb 1, 2019 · 4 comments

Comments

@ollieread
Copy link

ollieread commented Feb 1, 2019

When calling the ksort() method on Ds\Map I get the following error if the parameter is null or a variable that is null.

TypeError: Ds\Map::ksort() expects parameter 1 to be a valid callback, no array or string given

Omitting the parameter entirely works without issue.

The reason I'm running into this issue is that I actually have a few classes that basically wrap around the DS data structures. The particular method I have that causes this issue, is:

public function ksort(?callable $comparator = null): self
{
    $this->items->ksort($comparator);
    return $this;
}

In this case $this->items is an instance of Ds\Map.

@rtheunissen
Copy link
Member

Good catch! I'll fix this asap. I'm curious - why wrap around?

@ollieread
Copy link
Author

ollieread commented Feb 1, 2019

@rtheunissen I think there were a couple of other methods that did the same, but I just assumed they didn't accept a nullable argument.

In terms of wrapping, I'm adding a couple more methods that work with what people expect from 'collections'. I'm also adding little features such as enforced types and value/key normalisation. I've also added a few mutable and immutable versions of methods, so map()(mutable) & mapped() immutable, reverse()(mutable) & reversed()(immutable).

The actual class that I mentioned above is here: https://github.com/contraption/accumulator/blob/develop/src/Map.php

@rtheunissen
Copy link
Member

@ollieread we're going to rewrite parameter parsing for 2.0 that will fix this issue. I would recommend using func_num_args to determine whether to pass the callable, or perhaps splat ...$args forwarding? I'll leave this issue open in the meantime.

@ollieread
Copy link
Author

@rtheunissen does the splat operator work if args is null/empty?

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

No branches or pull requests

2 participants