Map::ksort TypeError on null comparator
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.
Good catch! I'll fix this asap. I'm curious - why wrap around?
@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 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 does the splat operator work if args is null/empty?