ext-ds icon indicating copy to clipboard operation
ext-ds copied to clipboard

Map::ksort TypeError on null comparator

Open ollieread opened this issue 6 years ago • 4 comments

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.

ollieread avatar Feb 01 '19 03:02 ollieread

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

rtheunissen avatar Feb 01 '19 17:02 rtheunissen

@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

ollieread avatar Feb 01 '19 19:02 ollieread

@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.

rtheunissen avatar May 12 '19 05:05 rtheunissen

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

ollieread avatar Jul 22 '19 17:07 ollieread